dlogger 0.0.6 → 0.1.0

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: c8ce05b6a57259faf6e47835802be45e10c0d6bc
4
- data.tar.gz: f7b1f2ee6802c792d1212892c2c16ac4c8d728e5
3
+ metadata.gz: 83c18123e6a73b53e44d03320171e283956614e7
4
+ data.tar.gz: 1af099b572aee8487cbf46dbc8cbba76659d47ba
5
5
  SHA512:
6
- metadata.gz: 3f3b68c66d3ad642610c55518bbd1c4dd54185a3d4ae326952966d7df5574999bfe19eab3e13d82a8609db9c08859885eaff3ecfdd95482bc75fc54634207702
7
- data.tar.gz: fa3477c2d0b3889beef5a437837b921a287910faf961a5888ed818ce5b2fe00ed0b5dbde08a96872f74997ef78d17a7037796537a711d6b65e6fa4dbd9174f83
6
+ metadata.gz: 892de0a86b92e6c98e59168c613fafa7b6f24061ab9bf37a30e3c23aa1532d619fbcc7de0f9d111e71002a01006fa8f1647d2828bcbcbc7436891341010b463b
7
+ data.tar.gz: 02d049557506e790790b407c4ac87c94ba945541ae81f159a10e1906b2b1f2f4309a075122a3c3ec979a02682ebe568f3246c42f94e7d479b129f4d6752a502d
@@ -8,7 +8,7 @@ module DLogger
8
8
 
9
9
  def initialize(name = "_default")
10
10
  @name = name
11
- @outputs = []
11
+ @outputs = {}
12
12
  @global_context = {}
13
13
  @level = 0
14
14
  end
@@ -21,22 +21,16 @@ module DLogger
21
21
  end
22
22
  end
23
23
 
24
- LOG_LEVELS.each.with_index do |name, index|
25
- class_eval(%{
26
- def #{name}?
27
- #{index} >= @level
28
- end
29
- })
30
- end
31
-
32
24
  ##
33
25
  # Main entry point, log a message with
34
26
  # its metadata.
35
27
  #
36
28
  # @param [String] msg the message
37
29
  # @param [Hash] metadata Additional data
30
+ # @param [Array] restrict If set the output will only be sent to these
31
+ # outputs.
38
32
  #
39
- def log(msg, metadata = {})
33
+ def log(msg, metadata: {}, restrict: [])
40
34
  # clearing a small hash is slightly faster than creating a new
41
35
  # one each time.
42
36
  merged_metadata.clear
@@ -66,18 +60,26 @@ module DLogger
66
60
  end
67
61
  end
68
62
 
69
- dispatch(msg, merged_metadata)
63
+ dispatch(msg, metadata: merged_metadata, restrict: restrict)
64
+ end
65
+
66
+ def self.level_as_sym(level)
67
+ LOG_LEVELS[level]
70
68
  end
71
69
 
72
70
  # Helper methods to mimic the standard ruby logger interface.
73
- %w(debug info error warn).each do |level|
74
- class_eval %{
75
- def #{level}(msg, metadata = {})
76
- # metadata << [:severity, :#{level}]
77
- metadata[:severity] = :#{level}
78
- log(msg, metadata)
79
- end
80
- }
71
+ LOG_LEVELS.each.with_index do |name, index|
72
+ class_eval(%{
73
+ def #{name}?
74
+ #{index} >= @level
75
+ end
76
+
77
+ def #{name}(msg, metadata: {}, restrict: [])
78
+ # metadata << [:severity, :#{name}]
79
+ metadata[:severity] = :#{name}
80
+ log(msg, metadata: metadata, restrict: restrict)
81
+ end
82
+ }, __FILE__, __LINE__)
81
83
  end
82
84
 
83
85
  ##
@@ -86,8 +88,8 @@ module DLogger
86
88
  #
87
89
  # @param [Object] handler the handler
88
90
  #
89
- def add_output(handler)
90
- @outputs << handler
91
+ def add_output(name, handler)
92
+ @outputs[name] = handler
91
93
  end
92
94
 
93
95
 
@@ -160,8 +162,14 @@ module DLogger
160
162
  # @param [Hash] metadata a hash including all the
161
163
  # additional informations you want to make available
162
164
  #
163
- def dispatch(msg, metadata)
164
- @outputs.each do |out|
165
+ def dispatch(msg, metadata: metadata, restrict:)
166
+ outputs = @outputs
167
+
168
+ if restrict && (restrict.size > 0)
169
+ outputs = outputs.reject{|name, out| !restrict.include?(name) }
170
+ end
171
+
172
+ outputs.each do |name, out|
165
173
  out.dispatch(msg, metadata)
166
174
  end
167
175
  end
@@ -16,7 +16,7 @@ module DLogger
16
16
 
17
17
  @socket = EM::open_datagram_socket(bind_address, 0)
18
18
  end
19
-
19
+
20
20
  def dispatch(msg, metadata)
21
21
  metadata = metadata.merge(message: msg)
22
22
 
@@ -1,3 +1,3 @@
1
1
  module Dlogger
2
- VERSION = "0.0.6"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -3,29 +3,41 @@ require_relative "../common"
3
3
  describe "Logger" do
4
4
  before do
5
5
  @logger = DLogger::Logger.new("logger#{rand(200)}#{rand(200)}")
6
+ @output1 = mock('out1')
7
+ @logger.add_output(:out1, @output1)
6
8
  end
7
9
 
8
10
  should "dispatch msg with its to registered outputs" do
9
11
 
10
- handler = mock('Handler')
11
- handler.expects(:dispatch).with("the message", id: 43, user: 'bob')
12
+ @output1.expects(:dispatch).with("the message", id: 43, user: 'bob')
13
+ @logger.log("the message", metadata: { id: 43, user: "bob" })
14
+ end
15
+
16
+ should 'dispatch only to specified output' do
17
+ @output1.expects(:dispatch).never
18
+
19
+ out2 = mock('Output2')
20
+ out2.expects(:dispatch).with("the message", id: 43, user: 'bob')
12
21
 
13
- @logger.add_output(handler)
22
+ @logger.add_output(:out2, out2)
14
23
 
15
- @logger.log("the message", id: 43, user: "bob")
24
+ @logger.log("the message",
25
+ metadata: {id: 43, user: "bob"},
26
+ restrict: [:out2]
27
+ )
16
28
  end
17
29
 
18
30
  should 'mimic standard ruby logger interface' do
19
- @logger.expects(:dispatch).with('the message', :severity => :debug)
31
+ @output1.expects(:dispatch).with('the message', :severity => :debug)
20
32
  @logger.debug("the message")
21
33
 
22
- @logger.expects(:dispatch).with('the message', :severity => :info)
34
+ @output1.expects(:dispatch).with('the message', :severity => :info)
23
35
  @logger.info("the message")
24
36
 
25
- @logger.expects(:dispatch).with('the message', :severity => :warn)
37
+ @output1.expects(:dispatch).with('the message', :severity => :warn)
26
38
  @logger.warn("the message")
27
39
 
28
- @logger.expects(:dispatch).with('the message', :severity => :error)
40
+ @output1.expects(:dispatch).with('the message', :severity => :error)
29
41
  @logger.error("the message")
30
42
  end
31
43
 
@@ -45,35 +57,35 @@ describe "Logger" do
45
57
 
46
58
  describe 'with hash context' do
47
59
  should 'dispatch msg and merged data' do
48
- @logger.expects(:dispatch).with('msg', id: 43, user: 'bob')
60
+ @output1.expects(:dispatch).with('msg', id: 43, user: 'bob')
49
61
 
50
62
  @logger.with_context(:user => 'bob') do
51
- @logger.log('msg', id: 43)
63
+ @logger.log('msg', metadata: {id: 43})
52
64
  end
53
65
  end
54
66
 
55
67
  should 'allow proc as value' do
56
- @logger.expects(:dispatch).with('msg', id: 43, user: 'john')
68
+ @output1.expects(:dispatch).with('msg', id: 43, user: 'john')
57
69
 
58
70
  @logger.with_context(user: ->{ "john" } ) do
59
- @logger.log('msg', id: 43)
71
+ @logger.log('msg', metadata: {id: 43})
60
72
  end
61
73
 
62
74
  end
63
75
 
64
76
  should 'keep pushed context' do
65
- @logger.expects(:dispatch).with('msg1', id: 43, global: 'A')
66
- @logger.expects(:dispatch).with('msg2', id: 43, user: 'john', global: 'A')
67
- @logger.expects(:dispatch).with('msg3', id: 43, global: 'A')
77
+ @output1.expects(:dispatch).with('msg1', id: 41, global: 'A')
78
+ @output1.expects(:dispatch).with('msg2', id: 42, user: 'john', global: 'A')
79
+ @output1.expects(:dispatch).with('msg3', id: 43, global: 'A')
68
80
 
69
81
  @logger.push_context(global: 'A')
70
- @logger.log('msg1', id: 43)
82
+ @logger.log('msg1', metadata: {id: 41})
71
83
 
72
84
  @logger.with_context(user: ->{ "john" } ) do
73
- @logger.log('msg2', id: 43)
85
+ @logger.log('msg2', metadata: {id: 42})
74
86
  end
75
87
 
76
- @logger.log('msg3', id: 43)
88
+ @logger.log('msg3', metadata: {id: 43})
77
89
  end
78
90
 
79
91
  end
@@ -86,10 +98,10 @@ describe "Logger" do
86
98
  end
87
99
  end
88
100
 
89
- @logger.expects(:dispatch).with('msg', id: 56, user: 'alice')
101
+ @output1.expects(:dispatch).with('msg', id: 56, user: 'alice')
90
102
 
91
103
  @logger.with_context(ext) do
92
- @logger.log('msg', id: 56)
104
+ @logger.log('msg', metadata: {id: 56})
93
105
  end
94
106
  end
95
107
 
@@ -97,15 +109,15 @@ describe "Logger" do
97
109
 
98
110
  describe 'with multiple contexts' do
99
111
  should 'merge the contexts in defined order (last defined has greater priority)' do
100
- @logger.expects(:dispatch).with('msg', operation: 'destroy', user: 'billy')
112
+ @output1.expects(:dispatch).with('msg', operation: 'destroy', user: 'billy')
101
113
 
102
114
  @logger.with_context(user: 'bob') do
103
115
  @logger.with_context(user: 'billy') do
104
- @logger.log('msg', operation: "destroy")
116
+ @logger.log('msg', metadata: {operation: "destroy"})
105
117
  end
106
118
  end
107
119
 
108
- @logger.expects(:dispatch).with('some text', {})
120
+ @output1.expects(:dispatch).with('some text', {})
109
121
  @logger.log('some text')
110
122
  end
111
123
 
@@ -118,18 +130,18 @@ describe "Logger" do
118
130
  end
119
131
 
120
132
  should 'include global context in log' do
121
- @logger.expects(:dispatch).with('msg1', app: "back1", op: "add", fiber: 1)
122
- @logger.expects(:dispatch).with('msg2', app: "back1", op: "sub", fiber: 2)
133
+ @output1.expects(:dispatch).with('msg1', app: "back1", op: "add", fiber: 1)
134
+ @output1.expects(:dispatch).with('msg2', app: "back1", op: "sub", fiber: 2)
123
135
 
124
136
  Fiber.new{
125
137
  @logger.with_context(fiber: 1) do
126
- @logger.log('msg1', op: 'add')
138
+ @logger.log('msg1', metadata: {op: 'add'})
127
139
  end
128
140
  }.resume
129
141
 
130
142
  Fiber.new{
131
143
  @logger.with_context(fiber: 2) do
132
- @logger.log('msg2', op: 'sub')
144
+ @logger.log('msg2', metadata: {op: 'sub'})
133
145
  end
134
146
  }.resume
135
147
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dlogger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julien Ammous