gem_logger 0.2.0 → 0.3.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: cac9834fee2a3e0b443c0385601b22831093517f
4
- data.tar.gz: 312e92da1a677e310c80b02b42ccb94cce8cfa52
3
+ metadata.gz: f73265de272376624ed589aab1173d5f0bfa042f
4
+ data.tar.gz: fb9fe431795c83e4ee312c817916d1c8b734d973
5
5
  SHA512:
6
- metadata.gz: 31460132ab3adec8fa05f3457cdf1b39629b7870aa86a5f3a619e43661afeb66f11b0f94cb55e7a13a50089e8e0c51637c3112456b64201ea6ef4f652eb5db53
7
- data.tar.gz: 09e7ed4a79a2ee5ab9e5cb45e8781b021cfd249148a21f6d99883a1dd98fe453b41f83275058de4f6092596850b5107613070b28e805cfdd352c890a250c85bb
6
+ metadata.gz: 2d62371c1f40cee4688ea2933b8274a68e9935a4bed324537018f695960d1de004a5cfb83f563670df6dcb26cf4a0c808a3b890246be8821fef421329e346c1c
7
+ data.tar.gz: af39d1bcea345d28ce9fe564efe344381bdbbcf67bd7b0462503d200a77577908bafacd99ec4d2eb29612dbdb1ea1eff9b50bc292ee912c3605c2fca0ec441e3
@@ -0,0 +1,14 @@
1
+ 0.3.0 (10/29/2014)
2
+ ------------------
3
+
4
+ Log4r handler. <2496da6> [Kenneth Hoffmann]
5
+ Merge pull request #2 from KenBackupify/context_update <fe65c3c> [Matt Conway]
6
+ Merge pull request #3 from ANorwell/universal_context_logging <b69aa83> [Matt Conway]
7
+ README fix. <3cd4c03> [Kenneth Hoffmann]
8
+ allow the context handler to be set after LoggerSupport is defined <f0b1dbe> [Arron Norwell]
9
+ make LogContext methods available for all logger concerns, not just the basic logger <608d61e> [Arron Norwell]
10
+
11
+ 0.2.0 (10/08/2014)
12
+ ------------------
13
+
14
+ Add context logging. <55283fa> [Kenneth Hoffmann]
data/README.md CHANGED
@@ -37,7 +37,7 @@ By default, log_exception uses the ERROR level, but also accepts a level as an o
37
37
 
38
38
  Unless otherwise specified, the context is added to the beginning of the log message in a basic key = value format. You can, however, define your own context handler, and pass it in on initialization:
39
39
 
40
- GemLogger.configure do |config| do
40
+ GemLogger.configure do |config|
41
41
  config.context_handler = MyHandler
42
42
  end
43
43
 
@@ -25,6 +25,7 @@ Gem::Specification.new do |spec|
25
25
  spec.add_development_dependency "minitest-reporters"
26
26
  spec.add_development_dependency "factory_girl"
27
27
  spec.add_development_dependency "faker"
28
+ spec.add_development_dependency "log4r"
28
29
 
29
30
  spec.add_dependency "activesupport"
30
31
  end
@@ -9,3 +9,4 @@ require "gem_logger/basic_logger"
9
9
  require "gem_logger/context_handler"
10
10
  require "gem_logger/gem_logger"
11
11
  require "gem_logger/logger_support"
12
+ require "gem_logger/log4r_handler/context_handler"
@@ -29,7 +29,7 @@ module GemLogger
29
29
 
30
30
  # The default_logger to use with GemLogger::BasicLogger
31
31
  def self.default_logger
32
- @default_logger ||= ::Logger.new(STDOUT).extend(LoggerSupport::LogContext)
32
+ @default_logger ||= ::Logger.new(STDOUT)
33
33
  end
34
34
 
35
35
  # Set the default_logger to use with GemLogger::BasicLogger
@@ -0,0 +1,30 @@
1
+ module GemLogger
2
+ module Log4rHandler
3
+ module ContextHandler
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ require 'log4r'
8
+ end
9
+
10
+ # Log4r::MDC.get_context returns a copy of the log context that is not
11
+ # modified when we remove the context variables added
12
+ def get_context
13
+ Log4r::MDC.get_context
14
+ end
15
+
16
+ def add_to_context(key, value)
17
+ Log4r::MDC.put(key.to_s, value.to_s)
18
+ end
19
+
20
+ def remove_from_context(key)
21
+ Log4r::MDC.remove(key.to_s)
22
+ end
23
+
24
+ # No formatting needed, Log4r will handle it.
25
+ def format_msg_with_context(msg)
26
+ msg
27
+ end
28
+ end
29
+ end
30
+ end
@@ -12,6 +12,14 @@ module GemLogger
12
12
  include GemLogger.logger_concern
13
13
 
14
14
  delegate :logger, :log_exception, :log_warning, :log_message, :to => "self.class"
15
+
16
+ class << self
17
+ alias_method :_original_logger, :logger
18
+
19
+ def logger
20
+ self._original_logger.extend(LogContext)
21
+ end
22
+ end
15
23
  end
16
24
 
17
25
  module ClassMethods
@@ -47,13 +55,12 @@ module GemLogger
47
55
 
48
56
  end
49
57
 
50
-
51
58
  module ContextLoggerCommon
52
59
  # @param [Hash] added_context - A hash containing context that will be added to the log messages produced by the
53
60
  # returned logger
54
61
  # @returns [LogContextLogger] - logger with the added context
55
62
  def context(added_context)
56
- LogContextLogger.new(self.logger, self.log_context.merge(added_context))
63
+ LogContextLogger.new(self.logger, self.log_context.merge(added_context)).extend(GemLogger.context_handler)
57
64
  end
58
65
 
59
66
  # Adds an event_type to the context
@@ -94,7 +101,6 @@ module GemLogger
94
101
 
95
102
  LogContextLogger = Struct.new(:logger, :log_context) do
96
103
  include ContextLoggerCommon
97
- include GemLogger.context_handler
98
104
 
99
105
  [:debug, :info, :warn, :error, :fatal].each do |method|
100
106
  define_method(method) { |msg| self.log(method, msg) }
@@ -1,3 +1,3 @@
1
1
  module GemLogger
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -0,0 +1,40 @@
1
+ require_relative '../../../test_helper'
2
+
3
+ module GemLogger
4
+ class GemLogger::Log4rHandler::ContextHandlerTest < Minitest::Should::TestCase
5
+
6
+ setup do
7
+ class MyClass
8
+ include GemLogger::Log4rHandler::ContextHandler
9
+ end
10
+ end
11
+
12
+ should "pass add_to_context to Log4r put" do
13
+ Log4r::MDC.expects(:put).with('key', 'value')
14
+ MyClass.new.add_to_context('key', 'value')
15
+ end
16
+
17
+ should "convert add_to_context args to strings" do
18
+ Log4r::MDC.expects(:put).with('key', 'value')
19
+ MyClass.new.add_to_context(:key, :value)
20
+ end
21
+
22
+ should 'get existing Log4r context' do
23
+ Log4r::MDC.put('some', 'thing')
24
+ context = MyClass.new.get_context
25
+ assert_equal 'thing', context['some']
26
+ # Clean up.
27
+ Log4r::MDC.remove('some')
28
+ end
29
+
30
+ should "pass remove_from_context to Log4r remove" do
31
+ Log4r::MDC.expects(:remove).with('key')
32
+ MyClass.new.remove_from_context('key')
33
+ end
34
+
35
+ should "convert remove_from_context args to strings" do
36
+ Log4r::MDC.expects(:remove).with('key')
37
+ MyClass.new.remove_from_context(:key)
38
+ end
39
+ end
40
+ end
@@ -1,4 +1,5 @@
1
1
  require_relative '../../test_helper'
2
+ require 'active_support/core_ext/module/delegation'
2
3
 
3
4
  module GemLogger
4
5
  class LoggerSupportTest < Minitest::Should::TestCase
@@ -35,7 +36,13 @@ module GemLogger
35
36
 
36
37
  setup do
37
38
  @old_default_logger = GemLogger.default_logger
38
- GemLogger.default_logger = Minitest::Mock.new
39
+
40
+ mock = Minitest::Mock.new
41
+ def mock.extend(mod)
42
+ self
43
+ end
44
+
45
+ GemLogger.default_logger = mock
39
46
  end
40
47
 
41
48
  teardown do
@@ -118,56 +125,85 @@ module GemLogger
118
125
 
119
126
  context "logger with added context" do
120
127
 
128
+ class Context
129
+ class << self
130
+ attr_accessor :values
131
+ end
132
+
133
+ def self.get_context
134
+ self.values ||= {}
135
+ end
136
+
137
+ def self.add_to_context(key, value)
138
+ get_context[key] = value
139
+ end
140
+
141
+ def self.remove_from_context(key)
142
+ get_context.delete(key)
143
+ end
144
+ end
145
+
146
+ module TestContextHandler
147
+ extend ActiveSupport::Concern
148
+
149
+ delegate :get_context, :add_to_context, :remove_from_context, :to => Context
150
+
151
+ def format_msg_with_context(msg)
152
+ msg
153
+ end
154
+
155
+ end
156
+
121
157
  setup do
122
158
  class Foo; include GemLogger::LoggerSupport; end
159
+ GemLogger.context_handler = TestContextHandler
123
160
  end
124
161
 
125
162
  should "add the context to generated messages" do
126
163
 
127
- GemLogger::LoggerSupport::LogContextLogger.any_instance.expects(:add_to_context).with("ctx", "1")
128
- GemLogger::LoggerSupport::LogContextLogger.any_instance.expects(:remove_from_context).with('ctx')
164
+ Context.expects(:add_to_context).with("ctx", "1")
165
+ Context.expects(:remove_from_context).with('ctx')
129
166
  Foo.logger.expects(:info).with("msg")
130
-
131
167
  Foo.logger.context("ctx" => "1").info("msg")
132
168
  end
133
169
 
134
170
  should "allow symbols as contexts" do
135
- GemLogger::LoggerSupport::LogContextLogger.any_instance.expects(:add_to_context).with(:ctx, "1")
136
- GemLogger::LoggerSupport::LogContextLogger.any_instance.expects(:remove_from_context).with(:ctx)
171
+ Context.expects(:add_to_context).with(:ctx, "1")
172
+ Context.expects(:remove_from_context).with(:ctx)
137
173
 
138
174
  Foo.logger.context(:ctx => "1").info("msg")
139
175
  end
140
176
 
141
177
  should "implement debug" do
142
- Foo.logger.expects(:debug).with("[ctx=1 ] msg")
178
+ Foo.logger.expects(:debug).with("msg")
143
179
  Foo.logger.context(:ctx => "1").debug("msg")
144
180
  end
145
181
 
146
182
  should "implement info" do
147
- Foo.logger.expects(:info).with("[ctx=1 ] msg")
183
+ Foo.logger.expects(:info).with("msg")
148
184
  Foo.logger.context(:ctx => "1").info("msg")
149
185
  end
150
186
 
151
187
  should "implement warn" do
152
- Foo.logger.expects(:warn).with("[ctx=1 ] msg")
188
+ Foo.logger.expects(:warn).with("msg")
153
189
  Foo.logger.context(:ctx => "1").warn("msg")
154
190
  end
155
191
 
156
192
  should "implement error" do
157
- Foo.logger.expects(:error).with("[ctx=1 ] msg")
193
+ Foo.logger.expects(:error).with("msg")
158
194
  Foo.logger.context(:ctx => "1").error("msg")
159
195
  end
160
196
 
161
197
  should "implement fatal" do
162
- Foo.logger.expects(:fatal).with("[ctx=1 ] msg")
198
+ Foo.logger.expects(:fatal).with("msg")
163
199
  Foo.logger.context(:ctx => "1").fatal("msg")
164
200
  end
165
201
 
166
202
  should 'allow context to be chained' do
167
- GemLogger::LoggerSupport::LogContextLogger.any_instance.expects(:add_to_context).with('ctx', '1')
168
- GemLogger::LoggerSupport::LogContextLogger.any_instance.expects(:remove_from_context).with('ctx')
169
- GemLogger::LoggerSupport::LogContextLogger.any_instance.expects(:add_to_context).with('ctx2', '2')
170
- GemLogger::LoggerSupport::LogContextLogger.any_instance.expects(:remove_from_context).with('ctx2')
203
+ Context.expects(:add_to_context).with('ctx', '1')
204
+ Context.expects(:remove_from_context).with('ctx')
205
+ Context.expects(:add_to_context).with('ctx2', '2')
206
+ Context.expects(:remove_from_context).with('ctx2')
171
207
  Foo.logger.expects(:info).with("msg")
172
208
 
173
209
  Foo.logger.context("ctx" => "1").context("ctx2" => "2").info("msg")
@@ -175,29 +211,28 @@ module GemLogger
175
211
 
176
212
  context "event_context" do
177
213
  should "add the event_type as context" do
178
- GemLogger::LoggerSupport::LogContextLogger.any_instance.expects(:add_to_context).with(:event_type, :test_event)
179
- GemLogger::LoggerSupport::LogContextLogger.any_instance.expects(:remove_from_context).with(:event_type)
214
+ Context.expects(:add_to_context).with(:event_type, :test_event)
215
+ Context.expects(:remove_from_context).with(:event_type)
180
216
 
181
217
  Foo.logger.expects(:info).with("msg")
182
218
  Foo.logger.event_context(:test_event).info("msg")
183
219
  end
184
220
 
185
221
  should "include the context of the logger used" do
186
- GemLogger::LoggerSupport::LogContextLogger.any_instance.expects(:add_to_context).with(:event_type, :test_event)
187
- GemLogger::LoggerSupport::LogContextLogger.any_instance.expects(:remove_from_context).with(:event_type)
188
- GemLogger::LoggerSupport::LogContextLogger.any_instance.expects(:add_to_context).with(:ctx, '1')
189
- GemLogger::LoggerSupport::LogContextLogger.any_instance.expects(:remove_from_context).with(:ctx)
222
+ Context.expects(:add_to_context).with(:event_type, :test_event)
223
+ Context.expects(:remove_from_context).with(:event_type)
224
+ Context.expects(:add_to_context).with(:ctx, '1')
225
+ Context.expects(:remove_from_context).with(:ctx)
190
226
 
191
227
  Foo.logger.expects(:error).with("msg")
192
228
  Foo.logger.context(:ctx => "1").event_context(:test_event).error("msg")
193
229
  end
194
230
 
195
231
  should 'restore previous log context after logging' do
196
- GemLogger::LoggerSupport::LogContextLogger.any_instance.stubs(:get_context).returns({'foo' => 'bar'})
197
- GemLogger::LoggerSupport::LogContextLogger.any_instance.expects(:add_to_context).with(:event_type, {'foo' => 'baz'})
198
- GemLogger::LoggerSupport::LogContextLogger.any_instance.expects(:remove_from_context).with(:event_type)
199
- GemLogger::LoggerSupport::LogContextLogger.any_instance.expects(:format_msg_with_context).with('msg')
200
- GemLogger::LoggerSupport::LogContextLogger.any_instance.expects(:add_to_context).with('foo', 'bar')
232
+ Context.stubs(:get_context).returns({'foo' => 'bar'})
233
+ Context.expects(:add_to_context).with(:event_type, {'foo' => 'baz'})
234
+ Context.expects(:remove_from_context).with(:event_type)
235
+ Context.expects(:add_to_context).with('foo', 'bar')
201
236
 
202
237
  Foo.logger.event_context('foo' => 'baz').info("msg")
203
238
  end
@@ -205,8 +240,8 @@ module GemLogger
205
240
 
206
241
  context "exception_context" do
207
242
  should "add the exception class as context" do
208
- GemLogger::LoggerSupport::LogContextLogger.any_instance.expects(:add_to_context).with(:exception, 'StandardError')
209
- GemLogger::LoggerSupport::LogContextLogger.any_instance.expects(:remove_from_context).with(:exception)
243
+ Context.expects(:add_to_context).with(:exception, 'StandardError')
244
+ Context.expects(:remove_from_context).with(:exception)
210
245
 
211
246
  Foo.logger.expects(:info).with("msg")
212
247
  Foo.logger.exception_context(StandardError.new).info("msg")
@@ -215,20 +250,20 @@ module GemLogger
215
250
 
216
251
  context "log_exception" do
217
252
  should "add the exception class as context" do
218
- GemLogger::LoggerSupport::LogContextLogger.any_instance.expects(:add_to_context).with(:exception, 'StandardError')
219
- GemLogger::LoggerSupport::LogContextLogger.any_instance.expects(:remove_from_context).with(:exception)
253
+ Context.expects(:add_to_context).with(:exception, 'StandardError')
254
+ Context.expects(:remove_from_context).with(:exception)
220
255
 
221
256
  Foo.logger.expects(:error)
222
257
  Foo.logger.log_exception(StandardError.new, "msg")
223
258
  end
224
259
 
225
260
  should "log the backtrace" do
226
- Foo.logger.expects(:error).with("[exception=StandardError ] msg: err (no backtrace)")
261
+ Foo.logger.expects(:error).with("msg: err (no backtrace)")
227
262
  Foo.logger.log_exception(StandardError.new("err"), "msg")
228
263
  end
229
264
 
230
265
  should 'allow the level to be changed as an option' do
231
- Foo.logger.expects(:warn).with("[exception=StandardError ] msg: err (no backtrace)")
266
+ Foo.logger.expects(:warn).with("msg: err (no backtrace)")
232
267
  Foo.logger.log_exception(StandardError.new("err"), "msg", :level => :warn)
233
268
  end
234
269
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gem_logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Conway
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-08 00:00:00.000000000 Z
11
+ date: 2014-10-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -108,6 +108,20 @@ dependencies:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: log4r
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
111
125
  - !ruby/object:Gem::Dependency
112
126
  name: activesupport
113
127
  requirement: !ruby/object:Gem::Requirement
@@ -133,6 +147,7 @@ files:
133
147
  - ".coveralls.yml"
134
148
  - ".gitignore"
135
149
  - ".travis.yml"
150
+ - CHANGELOG
136
151
  - Gemfile
137
152
  - LICENSE.txt
138
153
  - README.md
@@ -142,11 +157,13 @@ files:
142
157
  - lib/gem_logger/basic_logger.rb
143
158
  - lib/gem_logger/context_handler.rb
144
159
  - lib/gem_logger/gem_logger.rb
160
+ - lib/gem_logger/log4r_handler/context_handler.rb
145
161
  - lib/gem_logger/logger_support.rb
146
162
  - lib/gem_logger/version.rb
147
163
  - test/test_helper.rb
148
164
  - test/unit/gem_logger/basic_logger_test.rb
149
165
  - test/unit/gem_logger/context_handler_test.rb
166
+ - test/unit/gem_logger/log4r_handler/context_handler_test.rb
150
167
  - test/unit/gem_logger/logger_support_test.rb
151
168
  - test/unit/gem_logger_test.rb
152
169
  homepage: ''
@@ -178,6 +195,7 @@ test_files:
178
195
  - test/test_helper.rb
179
196
  - test/unit/gem_logger/basic_logger_test.rb
180
197
  - test/unit/gem_logger/context_handler_test.rb
198
+ - test/unit/gem_logger/log4r_handler/context_handler_test.rb
181
199
  - test/unit/gem_logger/logger_support_test.rb
182
200
  - test/unit/gem_logger_test.rb
183
201
  has_rdoc: