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: 3340d7bb77543987056799ba4fcc7e4dbae644e0
4
- data.tar.gz: 6796b3c63a27940229fbabaca2a0635fc85fa076
3
+ metadata.gz: 15da1aa320c8738cfa0e523bbebe859987594ec3
4
+ data.tar.gz: b807b4761bd2dccce15e79769fc685f7676c2c26
5
5
  SHA512:
6
- metadata.gz: 5dc46049feeeeed35209d04eb75eedc967a437c34d12ae3b1d0f0c428463860f5275c04eb11674d4ba9dfbdce3b788be2d6e862672a8ad9499f95be417e377e5
7
- data.tar.gz: 6144d060bf2b467c589d632f70c33887655afca47667cf1898370992cc80b51c9ec08363c75e302ba64213f5f9a07bddc398aeaf6a13a81cae5fcc41829756e0
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 ||= begin
15
- h = ENV['LOGSTASH_HOST']
16
- URI("logstash://#{h}") if h
17
- end
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
- with_connection do
43
- @io.write message
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
- private
55
-
56
- def host
57
- uri.host
55
+ def wait
56
+ while !@queue.empty?
57
+ sleep 0.1
58
58
  end
59
+ end
59
60
 
60
- def port
61
- @port ||= uri.port || 514
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
- def warn(msg)
65
- $stderr.puts "[warn ] #{msg}"
66
- end
72
+ private
67
73
 
68
- def with_connection(&block)
69
- @mutex.synchronize do
70
- connect unless connected?
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
- def reconnect
79
- @io = nil
80
- connect
81
- end
78
+ def port
79
+ @port ||= uri.port || 514
80
+ end
82
81
 
83
- def connect
84
- @io = TCPSocket.new(host, port).tap do |socket|
85
- socket.sync = true
86
- end
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
 
@@ -20,6 +20,10 @@ module Vx ; module Lib ; module Logger
20
20
  logstash_device.close
21
21
  end
22
22
 
23
+ def wait
24
+ logstash_device.wait
25
+ end
26
+
23
27
  private
24
28
 
25
29
  def logstash_device
@@ -1,7 +1,7 @@
1
1
  module Vx
2
2
  module Lib
3
3
  module Logger
4
- VERSION = "0.3.6"
4
+ VERSION = "0.3.7"
5
5
  end
6
6
  end
7
7
  end
@@ -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 re, "Hello\n"
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.close
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 re, "Hello\nWorld\n"
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
- client = server.accept
13
- out << client.gets
14
- client.close
17
+ Thread.fork(server.accept) do |client|
18
+ out << client.gets
19
+ client.close
20
+ end
15
21
  end
16
22
  end
17
23
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vx-lib-logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.6
4
+ version: 0.3.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Galinsky