vx-lib-logger 0.3.6 → 0.3.7
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 15da1aa320c8738cfa0e523bbebe859987594ec3
|
4
|
+
data.tar.gz: b807b4761bd2dccce15e79769fc685f7676c2c26
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3143fc65c2f6c2deb2ddbbe79e9ec00092355d8a518ec4d63f0af5a7cc425671fa28b2ae1b8cece4799db18c10969c76494140ee6f7c2ebbfb97e32dd3099870
|
7
|
+
data.tar.gz: fda5e4ee713cd2f4941c7afcd41db81a428a05ec99da21ab318606b7ed631bb84e751f9cc6ea7e2fafcc67028bf1c4169b2ad9ec3404f7c5fcbc8c3a044eab08
|
@@ -8,13 +8,15 @@ module Vx ; module Lib ; module Logger
|
|
8
8
|
|
9
9
|
def initialize
|
10
10
|
@mutex = Mutex.new
|
11
|
+
@queue = Queue.new
|
11
12
|
end
|
12
13
|
|
13
14
|
def uri
|
14
|
-
@uri ||=
|
15
|
-
|
16
|
-
|
17
|
-
|
15
|
+
@uri ||=
|
16
|
+
begin
|
17
|
+
h = ENV['LOGSTASH_HOST']
|
18
|
+
URI("logstash://#{h}") if h
|
19
|
+
end
|
18
20
|
end
|
19
21
|
|
20
22
|
def enabled?
|
@@ -39,9 +41,8 @@ module Vx ; module Lib ; module Logger
|
|
39
41
|
|
40
42
|
def write(message)
|
41
43
|
if enabled?
|
42
|
-
|
43
|
-
|
44
|
-
end
|
44
|
+
logger_thread
|
45
|
+
@queue.push message
|
45
46
|
end
|
46
47
|
end
|
47
48
|
|
@@ -51,40 +52,50 @@ module Vx ; module Lib ; module Logger
|
|
51
52
|
end
|
52
53
|
end
|
53
54
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
uri.host
|
55
|
+
def wait
|
56
|
+
while !@queue.empty?
|
57
|
+
sleep 0.1
|
58
58
|
end
|
59
|
+
end
|
59
60
|
|
60
|
-
|
61
|
-
|
61
|
+
def logger_thread
|
62
|
+
@logger_loop ||= Thread.new do
|
63
|
+
loop do
|
64
|
+
m = @queue.pop
|
65
|
+
with_connection do
|
66
|
+
@io.write m
|
67
|
+
end
|
68
|
+
end
|
62
69
|
end
|
70
|
+
end
|
63
71
|
|
64
|
-
|
65
|
-
$stderr.puts "[warn ] #{msg}"
|
66
|
-
end
|
72
|
+
private
|
67
73
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
end
|
72
|
-
yield
|
73
|
-
rescue Exception => e
|
74
|
-
warn "#{self.class} - #{e.class} - #{e.message}"
|
75
|
-
close
|
76
|
-
end
|
74
|
+
def host
|
75
|
+
uri.host
|
76
|
+
end
|
77
77
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
end
|
78
|
+
def port
|
79
|
+
@port ||= uri.port || 514
|
80
|
+
end
|
82
81
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
82
|
+
def warn(msg)
|
83
|
+
$stderr.puts "[warn ] #{msg}"
|
84
|
+
end
|
85
|
+
|
86
|
+
def with_connection(&block)
|
87
|
+
connect unless connected?
|
88
|
+
yield
|
89
|
+
rescue Exception => e
|
90
|
+
warn "#{self.class} - #{e.class} - #{e.message}"
|
91
|
+
close
|
92
|
+
end
|
93
|
+
|
94
|
+
def connect
|
95
|
+
@io = TCPSocket.new(host, port).tap do |socket|
|
96
|
+
socket.sync = true
|
87
97
|
end
|
98
|
+
end
|
88
99
|
|
89
100
|
end
|
90
101
|
|
@@ -16,7 +16,7 @@ describe Vx::Lib::Logger::LogstashDevice do
|
|
16
16
|
log.write("Hello\n")
|
17
17
|
log.close
|
18
18
|
end
|
19
|
-
assert_equal
|
19
|
+
assert_equal "Hello\n", re
|
20
20
|
end
|
21
21
|
|
22
22
|
it "should successfuly lost connection" do
|
@@ -24,18 +24,20 @@ describe Vx::Lib::Logger::LogstashDevice do
|
|
24
24
|
|
25
25
|
re = with_socket do
|
26
26
|
log.write("Hello\n")
|
27
|
-
log.
|
27
|
+
with_timeout { log.wait }
|
28
28
|
end
|
29
|
+
log.close
|
29
30
|
|
30
31
|
log.write("Lost\n")
|
32
|
+
with_timeout { log.wait }
|
31
33
|
|
32
34
|
re << with_socket do
|
33
35
|
log.write("World\n")
|
36
|
+
with_timeout { log.wait }
|
34
37
|
end
|
35
|
-
|
36
38
|
log.close
|
37
39
|
|
38
|
-
assert_equal
|
40
|
+
assert_equal "Hello\nWorld\n", re
|
39
41
|
end
|
40
42
|
|
41
43
|
|
@@ -17,6 +17,7 @@ describe Vx::Lib::Logger::LogstashLogger do
|
|
17
17
|
it "should write #{m} message" do
|
18
18
|
re = with_socket do
|
19
19
|
@log.public_send(m, "send #{m}")
|
20
|
+
@log.wait
|
20
21
|
@log.close
|
21
22
|
end
|
22
23
|
assert_match(/send #{m}/, re)
|
@@ -26,6 +27,7 @@ describe Vx::Lib::Logger::LogstashLogger do
|
|
26
27
|
it "should write message with params" do
|
27
28
|
re = with_socket do
|
28
29
|
@log.info "text message", param: :value
|
30
|
+
@log.wait
|
29
31
|
@log.close
|
30
32
|
end
|
31
33
|
assert_match(/text message/, re)
|
@@ -36,6 +38,7 @@ describe Vx::Lib::Logger::LogstashLogger do
|
|
36
38
|
it "should write message with exception in params" do
|
37
39
|
re = with_socket do
|
38
40
|
@log.info "text message", exception: Exception.new("got!")
|
41
|
+
@log.wait
|
39
42
|
@log.close
|
40
43
|
end
|
41
44
|
assert_match(/text message/, re)
|
@@ -46,6 +49,7 @@ describe Vx::Lib::Logger::LogstashLogger do
|
|
46
49
|
it "should dump invalid unicode key" do
|
47
50
|
re = with_socket do
|
48
51
|
@log.info "Le Caf\xc3\xa9 \xa9", key: "Le Caf\xc3\xa9 \xa9"
|
52
|
+
@log.wait
|
49
53
|
@log.close
|
50
54
|
end
|
51
55
|
assert_match(/Le Caf/, re)
|
data/spec/spec_helper.rb
CHANGED
@@ -2,6 +2,11 @@ require File.expand_path("../../lib/vx/lib/logger", __FILE__)
|
|
2
2
|
|
3
3
|
require 'minitest/spec'
|
4
4
|
require 'minitest/autorun'
|
5
|
+
require 'timeout'
|
6
|
+
|
7
|
+
def with_timeout(tm=3)
|
8
|
+
Timeout.timeout(tm) { yield }
|
9
|
+
end
|
5
10
|
|
6
11
|
def with_socket
|
7
12
|
out = ""
|
@@ -9,9 +14,10 @@ def with_socket
|
|
9
14
|
|
10
15
|
th = Thread.new do
|
11
16
|
loop do
|
12
|
-
|
13
|
-
|
14
|
-
|
17
|
+
Thread.fork(server.accept) do |client|
|
18
|
+
out << client.gets
|
19
|
+
client.close
|
20
|
+
end
|
15
21
|
end
|
16
22
|
end
|
17
23
|
|