dlogger 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a66b797653f4bdb8bd82f224a9c5db694a67b935
4
- data.tar.gz: cbd43d32888668b98b1a1e2f7abb9b03df6208bb
3
+ metadata.gz: c8ce05b6a57259faf6e47835802be45e10c0d6bc
4
+ data.tar.gz: f7b1f2ee6802c792d1212892c2c16ac4c8d728e5
5
5
  SHA512:
6
- metadata.gz: 310fc64c152285818a836014ca4a54ff4eed34f953beab9a3f922d06329d2c4b9185fd42e518c381db63ea944eb604af51d723dd9563d31052abe1a435078faf
7
- data.tar.gz: 829ce6c67279160ededa757b2befc52693223bd2ccb7fc179421afefe8d08e8073e05f5b5338f23b57923616c577329b066be69b4ee5e8b2c2c2e6c68017c5c1
6
+ metadata.gz: 3f3b68c66d3ad642610c55518bbd1c4dd54185a3d4ae326952966d7df5574999bfe19eab3e13d82a8609db9c08859885eaff3ecfdd95482bc75fc54634207702
7
+ data.tar.gz: fa3477c2d0b3889beef5a437837b921a287910faf961a5888ed818ce5b2fe00ed0b5dbde08a96872f74997ef78d17a7037796537a711d6b65e6fa4dbd9174f83
@@ -2,12 +2,31 @@ require 'thread'
2
2
 
3
3
  module DLogger
4
4
  class Logger
5
+ LOG_LEVELS = [:debug, :info, :warn, :error].freeze
6
+
7
+ attr_accessor :level
8
+
5
9
  def initialize(name = "_default")
6
10
  @name = name
7
11
  @outputs = []
8
-
9
- # initialize context
10
- init_context
12
+ @global_context = {}
13
+ @level = 0
14
+ end
15
+
16
+ def level=(what)
17
+ if what.is_a?(Symbol)
18
+ @level = LOG_LEVELS.index(what)
19
+ else
20
+ rais "symbol expected"
21
+ end
22
+ end
23
+
24
+ LOG_LEVELS.each.with_index do |name, index|
25
+ class_eval(%{
26
+ def #{name}?
27
+ #{index} >= @level
28
+ end
29
+ })
11
30
  end
12
31
 
13
32
  ##
@@ -21,6 +40,7 @@ module DLogger
21
40
  # clearing a small hash is slightly faster than creating a new
22
41
  # one each time.
23
42
  merged_metadata.clear
43
+ merged_metadata.merge!(@global_context)
24
44
 
25
45
  # first load context data
26
46
  context.each do |ctx_data|
@@ -70,6 +90,15 @@ module DLogger
70
90
  @outputs << handler
71
91
  end
72
92
 
93
+
94
+ ##
95
+ # This will get merged in every context no matter which fiber
96
+ # they are in
97
+ #
98
+ def add_to_global_context(h)
99
+ @global_context.merge!(h)
100
+ end
101
+
73
102
  ##
74
103
  # Usually called by #with_context method but you can use
75
104
  # it directly to push a context on the stack, it may be useful
@@ -109,16 +138,12 @@ module DLogger
109
138
 
110
139
 
111
140
  private
112
-
113
- def init_context
114
- Thread.current["#{@name}_dlogger_contexts"] = []
115
- end
116
-
141
+
117
142
  ##
118
143
  # Store the context in fiber local variables, each
119
144
  # Thread/Fiber gets its own.
120
145
  def context
121
- Thread.current["#{@name}_dlogger_contexts"]
146
+ Thread.current["#{@name}_dlogger_contexts"] ||= []
122
147
  end
123
148
 
124
149
  ##
@@ -27,7 +27,11 @@ module DLogger
27
27
  p.tag = "app"
28
28
  p.content = MultiJson.encode(metadata)
29
29
 
30
- @socket.send_datagram(p.to_s, @host, @port)
30
+ # do not raise an error if eventmachine is not running
31
+ # it might happen when shutting down
32
+ if EM.reactor_running?
33
+ @socket.send_datagram(p.to_s, @host, @port)
34
+ end
31
35
  end
32
36
 
33
37
  end
@@ -1,3 +1,3 @@
1
1
  module Dlogger
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -2,17 +2,17 @@ require_relative "../common"
2
2
 
3
3
  describe "Logger" do
4
4
  before do
5
- @logger = DLogger::Logger.new
5
+ @logger = DLogger::Logger.new("logger#{rand(200)}#{rand(200)}")
6
6
  end
7
7
 
8
8
  should "dispatch msg with its to registered outputs" do
9
9
 
10
10
  handler = mock('Handler')
11
- handler.expects(:dispatch).with("the message", :id => 43, :user => 'bob')
11
+ handler.expects(:dispatch).with("the message", id: 43, user: 'bob')
12
12
 
13
13
  @logger.add_output(handler)
14
14
 
15
- @logger.log("the message", :id => 43, :user => "bob")
15
+ @logger.log("the message", id: 43, user: "bob")
16
16
  end
17
17
 
18
18
  should 'mimic standard ruby logger interface' do
@@ -29,20 +29,34 @@ describe "Logger" do
29
29
  @logger.error("the message")
30
30
  end
31
31
 
32
+ should 'implement level interface' do
33
+ @logger.level = :debug
34
+ @logger.debug?.should == true
35
+ @logger.info?.should == true
36
+ @logger.warn?.should == true
37
+ @logger.error?.should == true
38
+
39
+ @logger.level = :warn
40
+ @logger.debug?.should == false
41
+ @logger.info?.should == false
42
+ @logger.warn?.should == true
43
+ @logger.error?.should == true
44
+ end
45
+
32
46
  describe 'with hash context' do
33
47
  should 'dispatch msg and merged data' do
34
- @logger.expects(:dispatch).with('msg', :id => 43, :user => 'bob')
48
+ @logger.expects(:dispatch).with('msg', id: 43, user: 'bob')
35
49
 
36
50
  @logger.with_context(:user => 'bob') do
37
- @logger.log('msg', :id => 43)
51
+ @logger.log('msg', id: 43)
38
52
  end
39
53
  end
40
54
 
41
55
  should 'allow proc as value' do
42
- @logger.expects(:dispatch).with('msg', :id => 43, :user => 'john')
56
+ @logger.expects(:dispatch).with('msg', id: 43, user: 'john')
43
57
 
44
- @logger.with_context(:user => ->{ "john" } ) do
45
- @logger.log('msg', :id => 43)
58
+ @logger.with_context(user: ->{ "john" } ) do
59
+ @logger.log('msg', id: 43)
46
60
  end
47
61
 
48
62
  end
@@ -72,10 +86,10 @@ describe "Logger" do
72
86
  end
73
87
  end
74
88
 
75
- @logger.expects(:dispatch).with('msg', :id => 56, :user => 'alice')
89
+ @logger.expects(:dispatch).with('msg', id: 56, user: 'alice')
76
90
 
77
91
  @logger.with_context(ext) do
78
- @logger.log('msg', :id => 56)
92
+ @logger.log('msg', id: 56)
79
93
  end
80
94
  end
81
95
 
@@ -83,11 +97,11 @@ describe "Logger" do
83
97
 
84
98
  describe 'with multiple contexts' do
85
99
  should 'merge the contexts in defined order (last defined has greater priority)' do
86
- @logger.expects(:dispatch).with('msg', :operation => 'destroy', :user => 'billy')
100
+ @logger.expects(:dispatch).with('msg', operation: 'destroy', user: 'billy')
87
101
 
88
- @logger.with_context(:user => 'bob') do
89
- @logger.with_context(:user => 'billy') do
90
- @logger.log('msg', :operation => "destroy")
102
+ @logger.with_context(user: 'bob') do
103
+ @logger.with_context(user: 'billy') do
104
+ @logger.log('msg', operation: "destroy")
91
105
  end
92
106
  end
93
107
 
@@ -97,4 +111,29 @@ describe "Logger" do
97
111
 
98
112
  end
99
113
 
114
+
115
+ describe 'multiple concurrent contexts' do
116
+ before do
117
+ @logger.add_to_global_context(app: "back1")
118
+ end
119
+
120
+ 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)
123
+
124
+ Fiber.new{
125
+ @logger.with_context(fiber: 1) do
126
+ @logger.log('msg1', op: 'add')
127
+ end
128
+ }.resume
129
+
130
+ Fiber.new{
131
+ @logger.with_context(fiber: 2) do
132
+ @logger.log('msg2', op: 'sub')
133
+ end
134
+ }.resume
135
+
136
+ end
137
+ end
138
+
100
139
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dlogger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julien Ammous
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-15 00:00:00.000000000 Z
11
+ date: 2015-04-16 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Advanced logger allowing you to include metadata with your messages
14
14
  email: