lumber 1.2.1 → 1.2.2
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 +4 -4
- data/.travis.yml +1 -1
- data/CHANGELOG +7 -0
- data/README.md +25 -6
- data/lib/lumber.rb +1 -1
- data/lib/lumber/inheritance_registry.rb +2 -2
- data/lib/lumber/lumber.rb +11 -4
- data/lib/lumber/version.rb +1 -1
- data/spec/inheritance_registry_spec.rb +8 -8
- data/spec/lumber_spec.rb +55 -29
- data/spec/spec_helper.rb +7 -1
- metadata +2 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: af1ccbaa821fdc83ded7a553efcaeb418d6f4614
|
4
|
+
data.tar.gz: ab1025e6e0560a6b8cf7432a55ef497ac60fdfcf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 87932480d522328bca72bc535ef8ff0024756ba85ba4ee20425bd0dd654e68677150241eee817a137b66e7d982df89b74c2522b478646a05df5875901203c500
|
7
|
+
data.tar.gz: 5d8c600adb7541d9822ae8a7b3efec363d6bec494c7faec2675f6d1f89832a43f1ff37fb940154889b66d3691802d3e148cfda72542e8f0e35e568e41db93b2c
|
data/.travis.yml
CHANGED
data/CHANGELOG
CHANGED
@@ -1,3 +1,10 @@
|
|
1
|
+
1.2.2 (08/29/2014)
|
2
|
+
------------------
|
3
|
+
|
4
|
+
Pluggable logger concerns - pull request #11 from ANorwell/logger_concerns <03d0724> [Matt Conway]
|
5
|
+
fix tests for newer rspec <033c3d6> [Matt Conway]
|
6
|
+
test against 2.1.2 <8c40685> [Matt Conway]
|
7
|
+
|
1
8
|
1.2.1 (03/13/2014)
|
2
9
|
------------------
|
3
10
|
|
data/README.md
CHANGED
@@ -19,7 +19,7 @@ To enable lumber in your rails project, add to your config/application.rb:
|
|
19
19
|
|
20
20
|
# To expose custom variables in log4r.yml
|
21
21
|
# config.lumber.some_option = "some_value
|
22
|
-
# you can set default_log_level here, config/environments/*.rb or in config/log4r.yml
|
22
|
+
# you can set default_log_level here, config/environments/*.rb or in config/log4r.yml
|
23
23
|
# config.log_level = :debug
|
24
24
|
# enabling lumber sets config.logger to Log4r::Logger['rails']
|
25
25
|
config.lumber.enabled = true
|
@@ -30,25 +30,44 @@ You should be able to use lumber in a non-rails project too, but you will have t
|
|
30
30
|
#
|
31
31
|
require 'lumber'
|
32
32
|
Lumber.init(:root => "/my/project", :env => "development")
|
33
|
-
|
33
|
+
|
34
34
|
# Setup parent loggers for some known rails Base classes. Classes that inherit
|
35
35
|
# from these will have their logger as a parent so you can configure logging for
|
36
36
|
# subtrees of classes in log4r.yml
|
37
37
|
Lumber.setup_logger_hierarchy("ActiveRecord::Base", "rails::models")
|
38
38
|
Lumber.setup_logger_hierarchy("ActionController::Base", "rails::controllers")
|
39
39
|
Lumber.setup_logger_hierarchy("ActionMailer::Base", "rails::mailers")
|
40
|
-
|
40
|
+
|
41
41
|
# If you really want, you can make all classes have a logger
|
42
42
|
# Lumber.setup_logger_hierarchy("Object", "root::object")
|
43
43
|
|
44
|
-
Additionally, you can also add loggers to individual classes by including the
|
44
|
+
Additionally, you can also add loggers to individual classes by including the Lumber::LoggerSupport module
|
45
45
|
|
46
46
|
class Foo
|
47
47
|
include Lumber::LoggerSupport
|
48
48
|
end
|
49
49
|
|
50
|
-
and Foo.logger/Foo.new.logger will log to a logger named "rails::Foo". This creates a
|
51
|
-
nested within modules, so you can use the namespace to enable/disable loggers
|
50
|
+
and Foo.logger/Foo.new.logger will log to a logger named "rails::Foo". This creates a hierarchy of loggers for classes
|
51
|
+
nested within modules, so you can use the namespace to enable/disable loggers.
|
52
|
+
|
53
|
+
The logger concern used by Lumber is customizeable:
|
54
|
+
|
55
|
+
module MyLoggerConcern
|
56
|
+
extend ActiveSupport::Concern
|
57
|
+
include Lumber::LoggerSupport
|
58
|
+
|
59
|
+
# an example custom logging function
|
60
|
+
def log_error(error)
|
61
|
+
logger.error("ERROR: #{error}")
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
This custom logger concern should be registered in an initializer so that it is available dynamically:
|
66
|
+
|
67
|
+
# before Rails::Initializer.run
|
68
|
+
#
|
69
|
+
require 'lumber'
|
70
|
+
Lumber.logger_concern = MyLoggerConcern
|
52
71
|
|
53
72
|
If you want to change the log level for a different environment, you can do so in log4r.yml or by using the standard rails "config.log_level" setting in config/environments/<env>.rb
|
54
73
|
|
data/lib/lumber.rb
CHANGED
@@ -2,9 +2,9 @@ $:.unshift(File.dirname(__FILE__))
|
|
2
2
|
|
3
3
|
# before config block
|
4
4
|
require "monitor"
|
5
|
+
require "lumber/logger_support"
|
5
6
|
require "lumber/lumber"
|
6
7
|
require "lumber/inheritance_registry"
|
7
|
-
require "lumber/logger_support"
|
8
8
|
require "lumber/log4r"
|
9
9
|
require "lumber/level_util"
|
10
10
|
require "lumber/json_formatter"
|
@@ -66,7 +66,7 @@ module Lumber
|
|
66
66
|
# Also prevent rails from subsequently overriding our logger when rails
|
67
67
|
# is loaded after registering logger inheritance
|
68
68
|
if Lumber::InheritanceRegistry[subclass.name]
|
69
|
-
subclass.send(:include, Lumber
|
69
|
+
subclass.send(:include, Lumber.logger_concern)
|
70
70
|
end
|
71
71
|
end
|
72
72
|
|
@@ -81,4 +81,4 @@ module Lumber
|
|
81
81
|
|
82
82
|
end
|
83
83
|
|
84
|
-
end
|
84
|
+
end
|
data/lib/lumber/lumber.rb
CHANGED
@@ -28,6 +28,10 @@ module Lumber
|
|
28
28
|
extend MonitorMixin
|
29
29
|
extend self
|
30
30
|
|
31
|
+
# The logger concern (ActiveSupport::Concern) to include in each class.
|
32
|
+
attr_accessor :logger_concern
|
33
|
+
self.logger_concern = Lumber::LoggerSupport
|
34
|
+
|
31
35
|
# Initializes log4r system. Needs to happen in
|
32
36
|
# config/environment.rb before Rails::Initializer.run
|
33
37
|
#
|
@@ -40,6 +44,7 @@ module Lumber
|
|
40
44
|
# * :monitor_enabled - defaults to true
|
41
45
|
# * :monitor_interval - defaults to 60
|
42
46
|
# * :monitor_store - defaults to Rails.cache if defined, memory otherwise, see Lumber::LevelUtil::MemoryCacheProvider for interface
|
47
|
+
# * :logger_concern - the logger concern to include, defaults to Lumber::LoggerConcern
|
43
48
|
#
|
44
49
|
# All config options get passed through to the log4r
|
45
50
|
# configurator for use in defining outputters
|
@@ -51,7 +56,7 @@ module Lumber
|
|
51
56
|
opts[:log_file] ||= "#{opts[:root]}/log/#{opts[:env]}.log"
|
52
57
|
opts[:monitor_enabled] = true unless opts[:monitor_enabled] == false
|
53
58
|
opts[:monitor_interval] ||= 60
|
54
|
-
|
59
|
+
|
55
60
|
raise "Lumber.init missing one of :root, :env" unless opts[:root] && opts[:env]
|
56
61
|
|
57
62
|
cfg = Log4r::YamlConfigurator
|
@@ -62,6 +67,8 @@ module Lumber
|
|
62
67
|
|
63
68
|
cfg.load_yaml_file(opts[:config_file])
|
64
69
|
|
70
|
+
self.logger_concern = opts[:logger_concern] if opts[:logger_concern]
|
71
|
+
|
65
72
|
# Workaround for rails bug: http://dev.rubyonrails.org/ticket/8665
|
66
73
|
if defined?(RAILS_DEFAULT_LOGGER)
|
67
74
|
Object.send(:remove_const, :RAILS_DEFAULT_LOGGER)
|
@@ -123,7 +130,7 @@ module Lumber
|
|
123
130
|
logger
|
124
131
|
end
|
125
132
|
end
|
126
|
-
|
133
|
+
|
127
134
|
# Makes :logger exist independently for subclasses and sets that logger
|
128
135
|
# to one that inherits from base_class for each subclass as it is created.
|
129
136
|
# This allows you to have a finer level of control over logging, for example,
|
@@ -144,7 +151,7 @@ module Lumber
|
|
144
151
|
|
145
152
|
begin
|
146
153
|
clazz = class_name.constantize
|
147
|
-
clazz.send(:include, Lumber
|
154
|
+
clazz.send(:include, Lumber.logger_concern)
|
148
155
|
rescue NameError
|
149
156
|
# The class hasn't been defined yet. No problem, we register
|
150
157
|
# the logger for when the class is created below
|
@@ -155,7 +162,7 @@ module Lumber
|
|
155
162
|
Lumber::InheritanceRegistry[class_name] = class_logger_fullname
|
156
163
|
end
|
157
164
|
|
158
|
-
# Helper to make it easier to log context through log4r.yml
|
165
|
+
# Helper to make it easier to log context through log4r.yml
|
159
166
|
def format_mdc()
|
160
167
|
ctx = Log4r::MDC.get_context.collect {|k, v| k.to_s + "=" + v.to_s }.join(" ")
|
161
168
|
ctx.gsub!('%', '%%')
|
data/lib/lumber/version.rb
CHANGED
@@ -61,11 +61,11 @@ describe Lumber::InheritanceRegistry do
|
|
61
61
|
describe "#remove_inheritance_handler" do
|
62
62
|
|
63
63
|
it "should remove the handler" do
|
64
|
-
defined?(Object.inherited_with_lumber_registry).should
|
64
|
+
defined?(Object.inherited_with_lumber_registry).should be_falsey
|
65
65
|
InheritanceRegistry.register_inheritance_handler
|
66
|
-
defined?(Object.inherited_with_lumber_registry).should
|
66
|
+
defined?(Object.inherited_with_lumber_registry).should be_truthy
|
67
67
|
InheritanceRegistry.remove_inheritance_handler
|
68
|
-
defined?(Object.inherited_with_lumber_registry).should
|
68
|
+
defined?(Object.inherited_with_lumber_registry).should be_falsey
|
69
69
|
end
|
70
70
|
|
71
71
|
end
|
@@ -77,18 +77,18 @@ describe Lumber::InheritanceRegistry do
|
|
77
77
|
end
|
78
78
|
|
79
79
|
it "adds an inheritance handler" do
|
80
|
-
defined?(Object.inherited_with_lumber_registry).should
|
80
|
+
defined?(Object.inherited_with_lumber_registry).should be_falsey
|
81
81
|
InheritanceRegistry.register_inheritance_handler
|
82
|
-
defined?(Object.inherited_with_lumber_registry).should
|
82
|
+
defined?(Object.inherited_with_lumber_registry).should be_truthy
|
83
83
|
end
|
84
84
|
|
85
85
|
it "doesn't add an inheritance handler multiple times" do
|
86
86
|
Object.singleton_class.should_receive(:alias_method_chain).once.and_call_original
|
87
|
-
defined?(Object.inherited_with_lumber_registry).should
|
87
|
+
defined?(Object.inherited_with_lumber_registry).should be_falsey
|
88
88
|
InheritanceRegistry.register_inheritance_handler
|
89
|
-
defined?(Object.inherited_with_lumber_registry).should
|
89
|
+
defined?(Object.inherited_with_lumber_registry).should be_truthy
|
90
90
|
InheritanceRegistry.register_inheritance_handler
|
91
|
-
defined?(Object.inherited_with_lumber_registry).should
|
91
|
+
defined?(Object.inherited_with_lumber_registry).should be_truthy
|
92
92
|
end
|
93
93
|
|
94
94
|
it "doesn't change classes that aren't registered" do
|
data/spec/lumber_spec.rb
CHANGED
@@ -81,16 +81,16 @@ describe Lumber do
|
|
81
81
|
describe "#setup_logger_hierarchy" do
|
82
82
|
|
83
83
|
it "should allow registering logger for a class before the class is defined" do
|
84
|
-
defined?(Foo1).should
|
84
|
+
defined?(Foo1).should be_falsey
|
85
85
|
Lumber.setup_logger_hierarchy("Foo1", "root::foo1")
|
86
86
|
new_class('Foo1')
|
87
87
|
assert_valid_logger('Foo1', "root::foo1")
|
88
88
|
end
|
89
89
|
|
90
90
|
it "should not register new logger for subclasses of classes that delegate logger" do
|
91
|
-
defined?(Foo1).should
|
92
|
-
defined?(Foo2).should
|
93
|
-
defined?(Foo3).should
|
91
|
+
defined?(Foo1).should be_falsey # ActionController::Base
|
92
|
+
defined?(Foo2).should be_falsey # ActionView::Base
|
93
|
+
defined?(Foo3).should be_falsey # Subclass of ActionView::Base
|
94
94
|
Lumber.setup_logger_hierarchy("Foo1", "root::foo1")
|
95
95
|
eval "class ::Foo1; end"
|
96
96
|
eval "class ::Foo2; delegate :logger, :to => Foo1; end"
|
@@ -101,20 +101,20 @@ describe Lumber do
|
|
101
101
|
end
|
102
102
|
|
103
103
|
it "should no logger when parent is via delegate class" do
|
104
|
-
defined?(Foo1).should
|
105
|
-
defined?(Foo2).should
|
106
|
-
defined?(Foo3).should
|
104
|
+
defined?(Foo1).should be_falsey
|
105
|
+
defined?(Foo2).should be_falsey
|
106
|
+
defined?(Foo3).should be_falsey
|
107
107
|
Lumber.setup_logger_hierarchy("Foo1", "root::foo1")
|
108
108
|
eval "class ::Foo1; end"
|
109
109
|
eval "class ::Foo2 < DelegateClass(Foo1); end"
|
110
110
|
eval "class ::Foo3 < Foo2; end"
|
111
111
|
assert_valid_logger('Foo1', "root::foo1")
|
112
|
-
defined?(Foo3.logger).should
|
112
|
+
defined?(Foo3.logger).should be_falsey
|
113
113
|
end
|
114
114
|
|
115
115
|
it "should allow registering independent loggers for classes in a hierarchy" do
|
116
|
-
defined?(Foo1).should
|
117
|
-
defined?(Foo2).should
|
116
|
+
defined?(Foo1).should be_falsey
|
117
|
+
defined?(Foo2).should be_falsey
|
118
118
|
Lumber.setup_logger_hierarchy("Foo1", "root::foo1")
|
119
119
|
Lumber.setup_logger_hierarchy("Foo2", "root::foo2")
|
120
120
|
new_class('Foo1')
|
@@ -124,38 +124,38 @@ describe Lumber do
|
|
124
124
|
end
|
125
125
|
|
126
126
|
it "should allow registering logger for a nested class before the class is defined" do
|
127
|
-
defined?(Bar1::Foo1).should
|
127
|
+
defined?(Bar1::Foo1).should be_falsey
|
128
128
|
Lumber.setup_logger_hierarchy("Bar1::Foo1", "root::foo1")
|
129
129
|
new_class('Foo1', nil, 'Bar1')
|
130
130
|
assert_valid_logger('Bar1::Foo1', "root::foo1")
|
131
131
|
end
|
132
132
|
|
133
133
|
it "should allow registering logger for a class after the class is defined" do
|
134
|
-
defined?(Foo1).should
|
134
|
+
defined?(Foo1).should be_falsey
|
135
135
|
new_class('Foo1')
|
136
|
-
defined?(Foo1).should
|
136
|
+
defined?(Foo1).should be_truthy
|
137
137
|
|
138
138
|
Lumber.setup_logger_hierarchy("Foo1", "root::Foo1")
|
139
139
|
assert_valid_logger('Foo1', "root::Foo1")
|
140
140
|
end
|
141
141
|
|
142
142
|
it "should register loggers for subclasses of registered classes" do
|
143
|
-
defined?(Foo1).should
|
144
|
-
defined?(Foo2).should
|
145
|
-
defined?(Foo3).should
|
143
|
+
defined?(Foo1).should be_falsey
|
144
|
+
defined?(Foo2).should be_falsey
|
145
|
+
defined?(Foo3).should be_falsey
|
146
146
|
Lumber.setup_logger_hierarchy("Foo1", "root::Foo1")
|
147
147
|
new_class('Foo1')
|
148
148
|
new_class('Foo2', 'Foo1')
|
149
149
|
new_class('Foo3')
|
150
150
|
assert_valid_logger('Foo1', "root::Foo1")
|
151
151
|
assert_valid_logger('Foo2', "root::Foo1::Foo2")
|
152
|
-
defined?(Foo3.logger).should
|
152
|
+
defined?(Foo3.logger).should be_falsey
|
153
153
|
end
|
154
154
|
|
155
155
|
it "should register loggers for sub-subclasses of registered classes" do
|
156
|
-
defined?(Foo1).should
|
157
|
-
defined?(Foo2).should
|
158
|
-
defined?(Foo3).should
|
156
|
+
defined?(Foo1).should be_falsey
|
157
|
+
defined?(Foo2).should be_falsey
|
158
|
+
defined?(Foo3).should be_falsey
|
159
159
|
Lumber.setup_logger_hierarchy("Foo1", "root::Foo1")
|
160
160
|
new_class('Foo1')
|
161
161
|
new_class('Foo2', 'Foo1')
|
@@ -166,9 +166,9 @@ describe Lumber do
|
|
166
166
|
end
|
167
167
|
|
168
168
|
it "should register loggers for sub-subclasses of registered classes even when middle class not a logger" do
|
169
|
-
defined?(Foo1).should
|
170
|
-
defined?(Foo2).should
|
171
|
-
defined?(Foo3).should
|
169
|
+
defined?(Foo1).should be_falsey
|
170
|
+
defined?(Foo2).should be_falsey
|
171
|
+
defined?(Foo3).should be_falsey
|
172
172
|
new_class('Foo1')
|
173
173
|
new_class('Foo2', 'Foo1')
|
174
174
|
Lumber.setup_logger_hierarchy("Foo1", "root::Foo1")
|
@@ -181,26 +181,52 @@ describe Lumber do
|
|
181
181
|
end
|
182
182
|
|
183
183
|
context "formatted MDC context" do
|
184
|
-
|
184
|
+
|
185
185
|
before(:each) do
|
186
186
|
Log4r::MDC.get_context.keys.each {|k| Log4r::MDC.remove(k) }
|
187
187
|
end
|
188
|
-
|
188
|
+
|
189
189
|
it "is empty for no context" do
|
190
190
|
Lumber.format_mdc.should == ""
|
191
191
|
end
|
192
|
-
|
192
|
+
|
193
193
|
it "has context vars" do
|
194
194
|
Log4r::MDC.put("baz", "boo")
|
195
195
|
Log4r::MDC.put("foo", "bar")
|
196
196
|
Lumber.format_mdc.should == "baz=boo foo=bar"
|
197
197
|
end
|
198
|
-
|
198
|
+
|
199
199
|
it "escapes %" do
|
200
200
|
Log4r::MDC.put("%foo", "%bar")
|
201
201
|
Lumber.format_mdc.should == "%%foo=%%bar"
|
202
202
|
end
|
203
|
-
|
203
|
+
|
204
|
+
end
|
205
|
+
|
206
|
+
context "using other Logging Concerns" do
|
207
|
+
|
208
|
+
before(:each) do
|
209
|
+
Log4r::MDC.get_context.keys.each {|k| Log4r::MDC.remove(k) }
|
210
|
+
end
|
211
|
+
|
212
|
+
# An example extension to LoggerSupport
|
213
|
+
module TestLoggingConcern
|
214
|
+
extend ActiveSupport::Concern
|
215
|
+
include Lumber::LoggerSupport
|
216
|
+
|
217
|
+
def custom_log_context
|
218
|
+
Log4r::MDC.put("baz", "boo")
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
it "should allow the logger concern to be changed" do
|
223
|
+
Lumber.logger_concern = TestLoggingConcern
|
224
|
+
Lumber.setup_logger_hierarchy("Foo1", "root::foo1")
|
225
|
+
new_class('Foo1')
|
226
|
+
|
227
|
+
assert_valid_logger('Foo1', "root::foo1")
|
228
|
+
Foo1.new.custom_log_context
|
229
|
+
Lumber.format_mdc.should == "baz=boo"
|
230
|
+
end
|
204
231
|
end
|
205
|
-
|
206
232
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -55,13 +55,19 @@ end
|
|
55
55
|
def assert_valid_logger(class_name, logger_name)
|
56
56
|
clazz = eval class_name
|
57
57
|
clazz.should_not be_nil
|
58
|
-
clazz.respond_to?(:logger).should
|
58
|
+
clazz.respond_to?(:logger).should be_truthy
|
59
59
|
lgr = clazz.logger
|
60
60
|
lgr.should be_an_instance_of(Log4r::Logger)
|
61
61
|
lgr.fullname.should == logger_name
|
62
62
|
end
|
63
63
|
|
64
64
|
RSpec.configure do |config|
|
65
|
+
config.mock_with :rspec do |c|
|
66
|
+
c.syntax = [:should, :expect]
|
67
|
+
end
|
68
|
+
config.expect_with :rspec do |c|
|
69
|
+
c.syntax = [:should, :expect]
|
70
|
+
end
|
65
71
|
config.before(:each) do
|
66
72
|
Object.constants.grep(/^(Foo|Bar)/).each do |c|
|
67
73
|
Object.send(:remove_const, c)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lumber
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.2
|
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-
|
11
|
+
date: 2014-08-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: log4r
|
@@ -255,4 +255,3 @@ test_files:
|
|
255
255
|
- spec/server_spec.rb
|
256
256
|
- spec/spec.opts
|
257
257
|
- spec/spec_helper.rb
|
258
|
-
has_rdoc:
|