lumberjack 1.2.8 → 1.2.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +111 -58
- data/README.md +2 -2
- data/VERSION +1 -1
- data/lib/lumberjack/context.rb +13 -0
- data/lib/lumberjack/device/date_rolling_log_file.rb +9 -0
- data/lib/lumberjack/device/log_file.rb +7 -0
- data/lib/lumberjack/device/multi.rb +1 -0
- data/lib/lumberjack/device/rolling_log_file.rb +21 -21
- data/lib/lumberjack/device/writer.rb +20 -0
- data/lib/lumberjack/device.rb +15 -0
- data/lib/lumberjack/formatter/date_time_formatter.rb +1 -0
- data/lib/lumberjack/formatter/exception_formatter.rb +2 -0
- data/lib/lumberjack/formatter/id_formatter.rb +1 -0
- data/lib/lumberjack/formatter/pretty_print_formatter.rb +2 -0
- data/lib/lumberjack/formatter/structured_formatter.rb +2 -0
- data/lib/lumberjack/formatter/truncate_formatter.rb +27 -0
- data/lib/lumberjack/formatter.rb +54 -8
- data/lib/lumberjack/log_entry.rb +8 -0
- data/lib/lumberjack/logger.rb +131 -12
- data/lib/lumberjack/severity.rb +8 -0
- data/lib/lumberjack/tag_formatter.rb +19 -0
- data/lib/lumberjack/tagged_logger_support.rb +1 -1
- data/lib/lumberjack/tags.rb +6 -0
- data/lib/lumberjack/template.rb +13 -1
- data/lib/lumberjack.rb +18 -1
- metadata +8 -7
data/lib/lumberjack/formatter.rb
CHANGED
@@ -21,11 +21,14 @@ module Lumberjack
|
|
21
21
|
require_relative "formatter/string_formatter"
|
22
22
|
require_relative "formatter/strip_formatter"
|
23
23
|
require_relative "formatter/structured_formatter"
|
24
|
+
require_relative "formatter/truncate_formatter"
|
24
25
|
|
25
26
|
class << self
|
26
27
|
# Returns a new empty formatter with no mapping. For historical reasons, a formatter
|
27
28
|
# is initialized with mappings to help output objects as strings. This will return one
|
28
29
|
# without the default mappings.
|
30
|
+
#
|
31
|
+
# @return [Lumberjack::Formatter] a new empty formatter
|
29
32
|
def empty
|
30
33
|
new.clear
|
31
34
|
end
|
@@ -45,7 +48,17 @@ module Lumberjack
|
|
45
48
|
# that responds to the +call+ method or as a symbol representing one of the predefined
|
46
49
|
# formatters, or as a block to the method call.
|
47
50
|
#
|
48
|
-
# The predefined formatters are:
|
51
|
+
# The predefined formatters are:
|
52
|
+
# - :date_time
|
53
|
+
# - :exception
|
54
|
+
# - :id
|
55
|
+
# - :inspect
|
56
|
+
# - :object
|
57
|
+
# - :pretty_print
|
58
|
+
# - :string
|
59
|
+
# - :strip
|
60
|
+
# - :structured
|
61
|
+
# - :truncate
|
49
62
|
#
|
50
63
|
# You can add multiple classes at once by passing an array of classes.
|
51
64
|
#
|
@@ -53,7 +66,18 @@ module Lumberjack
|
|
53
66
|
# help avoid loading dependency issues. This applies only to classes; modules cannot be
|
54
67
|
# passed in as strings.
|
55
68
|
#
|
56
|
-
#
|
69
|
+
# @param [Class, Module, String, Array<Class, Module, String>] klass The class or module to add a formatter for.
|
70
|
+
# @param [Symbol, Class, String, #call] formatter The formatter to use for the class.
|
71
|
+
# If a symbol is passed in, it will be used to load one of the predefined formatters.
|
72
|
+
# If a class is passed in, it will be initialized with the args passed in.
|
73
|
+
# Otherwise, the object will be used as the formatter and must respond to call method.
|
74
|
+
# @param [Array] args Arguments to pass to the formatter when it is initialized.
|
75
|
+
# @yield [obj] A block that will be used as the formatter for the class.
|
76
|
+
# @yieldparam [Object] obj The object to format.
|
77
|
+
# @yieldreturn [String] The formatted string.
|
78
|
+
# @return [self] Returns itself so that add statements can be chained together.
|
79
|
+
#
|
80
|
+
# @example
|
57
81
|
#
|
58
82
|
# # Use a predefined formatter
|
59
83
|
# formatter.add(MyClass, :pretty_print)
|
@@ -66,18 +90,27 @@ module Lumberjack
|
|
66
90
|
#
|
67
91
|
# # Add statements can be chained together
|
68
92
|
# formatter.add(MyClass, :pretty_print).add(YourClass){|obj| obj.humanize}
|
69
|
-
def add(klass, formatter = nil, &block)
|
93
|
+
def add(klass, formatter = nil, *args, &block)
|
70
94
|
formatter ||= block
|
71
95
|
if formatter.nil?
|
72
96
|
remove(klass)
|
73
97
|
else
|
98
|
+
formatter_class_name = nil
|
74
99
|
if formatter.is_a?(Symbol)
|
75
100
|
formatter_class_name = "#{formatter.to_s.gsub(/(^|_)([a-z])/) { |m| $~[2].upcase }}Formatter"
|
76
|
-
|
101
|
+
elsif formatter.is_a?(String)
|
102
|
+
formatter_class_name = formatter
|
103
|
+
end
|
104
|
+
if formatter_class_name
|
105
|
+
formatter = Formatter.const_get(formatter_class_name)
|
106
|
+
end
|
107
|
+
|
108
|
+
if formatter.is_a?(Class)
|
109
|
+
formatter = formatter.new(*args)
|
77
110
|
end
|
78
111
|
|
79
112
|
Array(klass).each do |k|
|
80
|
-
if k.
|
113
|
+
if k.instance_of?(Module)
|
81
114
|
@module_formatters[k] = formatter
|
82
115
|
else
|
83
116
|
k = k.name if k.is_a?(Class)
|
@@ -95,9 +128,12 @@ module Lumberjack
|
|
95
128
|
# You can also pass class names as strings instead of the classes themselves. This can
|
96
129
|
# help avoid loading dependency issues. This applies only to classes; modules cannot be
|
97
130
|
# passed in as strings.
|
131
|
+
#
|
132
|
+
# @param [Class, Module, String, Array<Class, Module, String>] klass The class or module to remove the formatters for.
|
133
|
+
# @return [self] Returns itself so that remove statements can be chained together.
|
98
134
|
def remove(klass)
|
99
135
|
Array(klass).each do |k|
|
100
|
-
if k.
|
136
|
+
if k.instance_of?(Module)
|
101
137
|
@module_formatters.delete(k)
|
102
138
|
else
|
103
139
|
k = k.name if k.is_a?(Class)
|
@@ -108,13 +144,18 @@ module Lumberjack
|
|
108
144
|
end
|
109
145
|
|
110
146
|
# Remove all formatters including the default formatter. Can be chained to add method calls.
|
147
|
+
#
|
148
|
+
# @return [self] Returns itself so that clear statements can be chained together.
|
111
149
|
def clear
|
112
150
|
@class_formatters.clear
|
113
151
|
@module_formatters.clear
|
114
152
|
self
|
115
153
|
end
|
116
154
|
|
117
|
-
# Format a message object
|
155
|
+
# Format a message object by applying all formatters attached to it.
|
156
|
+
#
|
157
|
+
# @param [Object] message The message object to format.
|
158
|
+
# @return [Object] The formatted object.
|
118
159
|
def format(message)
|
119
160
|
formatter = formatter_for(message.class)
|
120
161
|
if formatter&.respond_to?(:call)
|
@@ -126,6 +167,11 @@ module Lumberjack
|
|
126
167
|
|
127
168
|
# Compatibility with the Logger::Formatter signature. This method will just convert the message
|
128
169
|
# object to a string and ignores the other parameters.
|
170
|
+
#
|
171
|
+
# @param [Integer, String, Symbol] severity The severity of the message.
|
172
|
+
# @param [Time] timestamp The time the message was logged.
|
173
|
+
# @param [String] progname The name of the program logging the message.
|
174
|
+
# @param [Object] msg The message object to format.
|
129
175
|
def call(severity, timestamp, progname, msg)
|
130
176
|
"#{format(msg)}#{Lumberjack::LINE_SEPARATOR}"
|
131
177
|
end
|
@@ -133,7 +179,7 @@ module Lumberjack
|
|
133
179
|
private
|
134
180
|
|
135
181
|
# Find the formatter for a class by looking it up using the class hierarchy.
|
136
|
-
def formatter_for(klass)
|
182
|
+
def formatter_for(klass) # :nodoc:
|
137
183
|
check_modules = true
|
138
184
|
until klass.nil?
|
139
185
|
formatter = @class_formatters[klass.name]
|
data/lib/lumberjack/log_entry.rb
CHANGED
@@ -10,6 +10,14 @@ module Lumberjack
|
|
10
10
|
|
11
11
|
UNIT_OF_WORK_ID = "unit_of_work_id"
|
12
12
|
|
13
|
+
# Create a new log entry.
|
14
|
+
#
|
15
|
+
# @param [Time] time The time the log entry was created.
|
16
|
+
# @param [Integer, String] severity The severity of the log entry.
|
17
|
+
# @param [String] message The message to log.
|
18
|
+
# @param [String] progname The name of the program that created the log entry.
|
19
|
+
# @param [Integer] pid The process id of the program that created the log entry.
|
20
|
+
# @param [Hash] tags A hash of tags to associate with the log entry.
|
13
21
|
def initialize(time, severity, message, progname, pid, tags)
|
14
22
|
@time = time
|
15
23
|
@severity = (severity.is_a?(Integer) ? severity : Severity.label_to_level(severity))
|
data/lib/lumberjack/logger.rb
CHANGED
@@ -63,6 +63,9 @@ module Lumberjack
|
|
63
63
|
# * :max_size - If the log device is a file path, it will be a Device::SizeRollingLogFile if this is set.
|
64
64
|
#
|
65
65
|
# All other options are passed to the device constuctor.
|
66
|
+
#
|
67
|
+
# @param [Lumberjack::Device, Object, Symbol, String] device The device to log to.
|
68
|
+
# @param [Hash] options The options for the logger.
|
66
69
|
def initialize(device = $stdout, options = {})
|
67
70
|
options = options.dup
|
68
71
|
self.level = options.delete(:level) || INFO
|
@@ -83,11 +86,16 @@ module Lumberjack
|
|
83
86
|
end
|
84
87
|
|
85
88
|
# Get the timestamp format on the device if it has one.
|
89
|
+
#
|
90
|
+
# @return [String, nil] The timestamp format or nil if the device doesn't support it.
|
86
91
|
def datetime_format
|
87
92
|
device.datetime_format if device.respond_to?(:datetime_format)
|
88
93
|
end
|
89
94
|
|
90
95
|
# Set the timestamp format on the device if it is supported.
|
96
|
+
#
|
97
|
+
# @param [String] format The timestamp format.
|
98
|
+
# @return [void]
|
91
99
|
def datetime_format=(format)
|
92
100
|
if device.respond_to?(:datetime_format=)
|
93
101
|
device.datetime_format = format
|
@@ -96,14 +104,19 @@ module Lumberjack
|
|
96
104
|
|
97
105
|
# Get the level of severity of entries that are logged. Entries with a lower
|
98
106
|
# severity level will be ignored.
|
107
|
+
#
|
108
|
+
# @return [Integer] The severity level.
|
99
109
|
def level
|
100
110
|
thread_local_value(:lumberjack_logger_level) || @level
|
101
111
|
end
|
102
112
|
|
103
|
-
|
113
|
+
alias_method :sev_threshold, :level
|
104
114
|
|
105
115
|
# Set the log level using either an integer level like Logger::INFO or a label like
|
106
116
|
# :info or "info"
|
117
|
+
#
|
118
|
+
# @param [Integer, Symbol, String] value The severity level.
|
119
|
+
# @return [void]
|
107
120
|
def level=(value)
|
108
121
|
@level = if value.is_a?(Integer)
|
109
122
|
value
|
@@ -112,14 +125,19 @@ module Lumberjack
|
|
112
125
|
end
|
113
126
|
end
|
114
127
|
|
115
|
-
|
128
|
+
alias_method :sev_threshold=, :level=
|
116
129
|
|
117
130
|
# Set the Lumberjack::Formatter used to format objects for logging as messages.
|
131
|
+
#
|
132
|
+
# @param [Lumberjack::Formatter, Object] value The formatter to use.
|
133
|
+
# @return [void]
|
118
134
|
def formatter=(value)
|
119
135
|
@_formatter = (value.is_a?(TaggedLoggerSupport::Formatter) ? value.__formatter : value)
|
120
136
|
end
|
121
137
|
|
122
138
|
# Get the Lumberjack::Formatter used to format objects for logging as messages.
|
139
|
+
#
|
140
|
+
# @return [Lumberjack::Formatter] The formatter.
|
123
141
|
def formatter
|
124
142
|
if respond_to?(:tagged)
|
125
143
|
# Wrap in an object that supports ActiveSupport::TaggedLogger API
|
@@ -136,6 +154,8 @@ module Lumberjack
|
|
136
154
|
# The tags added with this method are just strings so they are stored in the logger tags
|
137
155
|
# in an array under the "tagged" tag. So calling `logger.tagged("foo", "bar")` will result
|
138
156
|
# in tags `{"tagged" => ["foo", "bar"]}`.
|
157
|
+
#
|
158
|
+
# @return [Lumberjack::Logger] self.
|
139
159
|
def tagged_logger!
|
140
160
|
extend(TaggedLoggerSupport)
|
141
161
|
self
|
@@ -149,7 +169,13 @@ module Lumberjack
|
|
149
169
|
# The severity can be passed in either as one of the Severity constants,
|
150
170
|
# or as a Severity label.
|
151
171
|
#
|
152
|
-
#
|
172
|
+
# @param [Integer, Symbol, String] severity The severity of the message.
|
173
|
+
# @param [Object] message The message to log.
|
174
|
+
# @param [String] progname The name of the program that is logging the message.
|
175
|
+
# @param [Hash] tags The tags to add to the log entry.
|
176
|
+
# @return [void]
|
177
|
+
#
|
178
|
+
# @example
|
153
179
|
#
|
154
180
|
# logger.add_entry(Logger::ERROR, exception)
|
155
181
|
# logger.add_entry(Logger::INFO, "Request completed")
|
@@ -191,6 +217,11 @@ module Lumberjack
|
|
191
217
|
end
|
192
218
|
|
193
219
|
# ::Logger compatible method to add a log entry.
|
220
|
+
#
|
221
|
+
# @param [Integer, Symbol, String] severity The severity of the message.
|
222
|
+
# @param [Object] message The message to log.
|
223
|
+
# @param [String] progname The name of the program that is logging the message.
|
224
|
+
# @return [void]
|
194
225
|
def add(severity, message = nil, progname = nil, &block)
|
195
226
|
if message.nil?
|
196
227
|
if block
|
@@ -203,9 +234,11 @@ module Lumberjack
|
|
203
234
|
add_entry(severity, message, progname)
|
204
235
|
end
|
205
236
|
|
206
|
-
|
237
|
+
alias_method :log, :add
|
207
238
|
|
208
239
|
# Flush the logging device. Messages are not guaranteed to be written until this method is called.
|
240
|
+
#
|
241
|
+
# @return [void]
|
209
242
|
def flush
|
210
243
|
device.flush
|
211
244
|
@last_flushed_at = Time.now
|
@@ -213,102 +246,170 @@ module Lumberjack
|
|
213
246
|
end
|
214
247
|
|
215
248
|
# Close the logging device.
|
249
|
+
#
|
250
|
+
# @return [void]
|
216
251
|
def close
|
217
252
|
flush
|
218
253
|
device.close if device.respond_to?(:close)
|
219
254
|
@closed = true
|
220
255
|
end
|
221
256
|
|
257
|
+
# Returns +true+ if the logging device is closed.
|
258
|
+
#
|
259
|
+
# @return [Boolean] +true+ if the logging device is closed.
|
222
260
|
def closed?
|
223
261
|
@closed
|
224
262
|
end
|
225
263
|
|
264
|
+
# Reopen the logging device.
|
265
|
+
#
|
266
|
+
# @param [Object] logdev passed through to the logging device.
|
226
267
|
def reopen(logdev = nil)
|
227
268
|
@closed = false
|
228
269
|
device.reopen(logdev) if device.respond_to?(:reopen)
|
229
270
|
end
|
230
271
|
|
231
272
|
# Log a +FATAL+ message. The message can be passed in either the +message+ argument or in a block.
|
273
|
+
#
|
274
|
+
# @param [Object] message_or_progname_or_tags The message to log or progname
|
275
|
+
# if the message is passed in a block.
|
276
|
+
# @param [String, Hash] progname_or_tags The name of the program that is logging the message or tags
|
277
|
+
# if the message is passed in a block.
|
278
|
+
# @return [void]
|
232
279
|
def fatal(message_or_progname_or_tags = nil, progname_or_tags = nil, &block)
|
233
280
|
call_add_entry(FATAL, message_or_progname_or_tags, progname_or_tags, &block)
|
234
281
|
end
|
235
282
|
|
236
283
|
# Return +true+ if +FATAL+ messages are being logged.
|
284
|
+
#
|
285
|
+
# @return [Boolean]
|
237
286
|
def fatal?
|
238
287
|
level <= FATAL
|
239
288
|
end
|
240
289
|
|
241
290
|
# Set the log level to fatal.
|
291
|
+
#
|
292
|
+
# @return [void]
|
242
293
|
def fatal!
|
243
294
|
self.level = FATAL
|
244
295
|
end
|
245
296
|
|
246
297
|
# Log an +ERROR+ message. The message can be passed in either the +message+ argument or in a block.
|
298
|
+
#
|
299
|
+
# @param [Object] message_or_progname_or_tags The message to log or progname
|
300
|
+
# if the message is passed in a block.
|
301
|
+
# @param [String, Hash] progname_or_tags The name of the program that is logging the message or tags
|
302
|
+
# if the message is passed in a block.
|
303
|
+
# @return [void]
|
247
304
|
def error(message_or_progname_or_tags = nil, progname_or_tags = nil, &block)
|
248
305
|
call_add_entry(ERROR, message_or_progname_or_tags, progname_or_tags, &block)
|
249
306
|
end
|
250
307
|
|
251
308
|
# Return +true+ if +ERROR+ messages are being logged.
|
309
|
+
#
|
310
|
+
# @return [Boolean]
|
252
311
|
def error?
|
253
312
|
level <= ERROR
|
254
313
|
end
|
255
314
|
|
256
315
|
# Set the log level to error.
|
316
|
+
#
|
317
|
+
# @return [void]
|
257
318
|
def error!
|
258
319
|
self.level = ERROR
|
259
320
|
end
|
260
321
|
|
261
322
|
# Log a +WARN+ message. The message can be passed in either the +message+ argument or in a block.
|
323
|
+
#
|
324
|
+
# @param [Object] message_or_progname_or_tags The message to log or progname
|
325
|
+
# if the message is passed in a block.
|
326
|
+
# @param [String, Hash] progname_or_tags The name of the program that is logging the message or tags
|
327
|
+
# if the message is passed in a block.
|
328
|
+
# @return [void]
|
262
329
|
def warn(message_or_progname_or_tags = nil, progname_or_tags = nil, &block)
|
263
330
|
call_add_entry(WARN, message_or_progname_or_tags, progname_or_tags, &block)
|
264
331
|
end
|
265
332
|
|
266
333
|
# Return +true+ if +WARN+ messages are being logged.
|
334
|
+
#
|
335
|
+
# @return [Boolean]
|
267
336
|
def warn?
|
268
337
|
level <= WARN
|
269
338
|
end
|
270
339
|
|
271
340
|
# Set the log level to warn.
|
341
|
+
#
|
342
|
+
# @return [void]
|
272
343
|
def warn!
|
273
344
|
self.level = WARN
|
274
345
|
end
|
275
346
|
|
276
347
|
# Log an +INFO+ message. The message can be passed in either the +message+ argument or in a block.
|
348
|
+
#
|
349
|
+
# @param [Object] message_or_progname_or_tags The message to log or progname
|
350
|
+
# if the message is passed in a block.
|
351
|
+
# @param [String] progname_or_tags The name of the program that is logging the message or tags
|
352
|
+
# if the message is passed in a block.
|
353
|
+
# @return [void]
|
277
354
|
def info(message_or_progname_or_tags = nil, progname_or_tags = nil, &block)
|
278
355
|
call_add_entry(INFO, message_or_progname_or_tags, progname_or_tags, &block)
|
279
356
|
end
|
280
357
|
|
281
358
|
# Return +true+ if +INFO+ messages are being logged.
|
359
|
+
#
|
360
|
+
|
282
361
|
def info?
|
283
362
|
level <= INFO
|
284
363
|
end
|
285
364
|
|
286
365
|
# Set the log level to info.
|
366
|
+
#
|
367
|
+
# @return [void]
|
287
368
|
def info!
|
288
369
|
self.level = INFO
|
289
370
|
end
|
290
371
|
|
291
372
|
# Log a +DEBUG+ message. The message can be passed in either the +message+ argument or in a block.
|
373
|
+
#
|
374
|
+
# @param [Object] message_or_progname_or_tags The message to log or progname
|
375
|
+
# if the message is passed in a block.
|
376
|
+
# @param [String, Hash] progname_or_tags The name of the program that is logging the message or tags
|
377
|
+
# if the message is passed in a block.
|
378
|
+
# @return [void]
|
292
379
|
def debug(message_or_progname_or_tags = nil, progname_or_tags = nil, &block)
|
293
380
|
call_add_entry(DEBUG, message_or_progname_or_tags, progname_or_tags, &block)
|
294
381
|
end
|
295
382
|
|
296
383
|
# Return +true+ if +DEBUG+ messages are being logged.
|
384
|
+
#
|
385
|
+
# @return [Boolean]
|
297
386
|
def debug?
|
298
387
|
level <= DEBUG
|
299
388
|
end
|
300
389
|
|
301
390
|
# Set the log level to debug.
|
391
|
+
#
|
392
|
+
# @return [void]
|
302
393
|
def debug!
|
303
394
|
self.level = DEBUG
|
304
395
|
end
|
305
396
|
|
306
397
|
# Log a message when the severity is not known. Unknown messages will always appear in the log.
|
307
398
|
# The message can be passed in either the +message+ argument or in a block.
|
399
|
+
#
|
400
|
+
# @param [Object] message_or_progname_or_tags The message to log or progname
|
401
|
+
# if the message is passed in a block.
|
402
|
+
# @param [String, Hash] progname_or_tags The name of the program that is logging the message or tags
|
403
|
+
# if the message is passed in a block.
|
404
|
+
# @return [void]
|
308
405
|
def unknown(message_or_progname_or_tags = nil, progname_or_tags = nil, &block)
|
309
406
|
call_add_entry(UNKNOWN, message_or_progname_or_tags, progname_or_tags, &block)
|
310
407
|
end
|
311
408
|
|
409
|
+
# Add a message when the severity is not known.
|
410
|
+
#
|
411
|
+
# @param [Object] msg The message to log.
|
412
|
+
# @return [void]
|
312
413
|
def <<(msg)
|
313
414
|
add_entry(UNKNOWN, msg)
|
314
415
|
end
|
@@ -316,7 +417,10 @@ module Lumberjack
|
|
316
417
|
# Silence the logger by setting a new log level inside a block. By default, only +ERROR+ or +FATAL+
|
317
418
|
# messages will be logged.
|
318
419
|
#
|
319
|
-
#
|
420
|
+
# @param [Integer, String, Symbol] temporary_level The log level to use inside the block.
|
421
|
+
# @return [Object] The result of the block.
|
422
|
+
#
|
423
|
+
# @example
|
320
424
|
#
|
321
425
|
# logger.level = Logger::INFO
|
322
426
|
# logger.silence do
|
@@ -335,6 +439,9 @@ module Lumberjack
|
|
335
439
|
|
336
440
|
# Set the program name that is associated with log messages. If a block
|
337
441
|
# is given, the program name will be valid only within the block.
|
442
|
+
#
|
443
|
+
# @param [String] value The program name to use.
|
444
|
+
# @return [void]
|
338
445
|
def set_progname(value, &block)
|
339
446
|
if block
|
340
447
|
push_thread_local_value(:lumberjack_logger_progname, value, &block)
|
@@ -344,6 +451,8 @@ module Lumberjack
|
|
344
451
|
end
|
345
452
|
|
346
453
|
# Get the program name associated with log messages.
|
454
|
+
#
|
455
|
+
# @return [String]
|
347
456
|
def progname
|
348
457
|
thread_local_value(:lumberjack_logger_progname) || @progname
|
349
458
|
end
|
@@ -353,6 +462,9 @@ module Lumberjack
|
|
353
462
|
# the tags will only be defined on the tags in that block. When the parent block
|
354
463
|
# exits, all the tags will be reverted. If there is no block, then the tags will
|
355
464
|
# be defined as global and apply to all log statements.
|
465
|
+
#
|
466
|
+
# @param [Hash] tags The tags to set.
|
467
|
+
# @return [void]
|
356
468
|
def tag(tags, &block)
|
357
469
|
tags = Tags.stringify_keys(tags)
|
358
470
|
thread_tags = thread_local_value(:lumberjack_logger_tags)
|
@@ -371,6 +483,9 @@ module Lumberjack
|
|
371
483
|
# Remove a tag from the current tag context. If this is called inside a block to a
|
372
484
|
# call to `tag`, the tags will only be removed for the duration of that block. Otherwise
|
373
485
|
# they will be removed from the global tags.
|
486
|
+
#
|
487
|
+
# @param [Array<String, Symbol>] tag_names The tags to remove.
|
488
|
+
# @return [void]
|
374
489
|
def remove_tag(*tag_names)
|
375
490
|
thread_tags = thread_local_value(:lumberjack_logger_tags)
|
376
491
|
if thread_tags
|
@@ -382,6 +497,8 @@ module Lumberjack
|
|
382
497
|
|
383
498
|
# Return all tags in scope on the logger including global tags set on the Lumberjack
|
384
499
|
# context, tags set on the logger, and tags set on the current block for the logger.
|
500
|
+
#
|
501
|
+
# @return [Hash]
|
385
502
|
def tags
|
386
503
|
tags = {}
|
387
504
|
context_tags = Lumberjack.context_tags
|
@@ -395,6 +512,8 @@ module Lumberjack
|
|
395
512
|
# Remove all tags on the current logger and logging context within a block.
|
396
513
|
# You can still set new block scoped tags within theuntagged block and provide
|
397
514
|
# tags on individual log methods.
|
515
|
+
#
|
516
|
+
# @return [void]
|
398
517
|
def untagged(&block)
|
399
518
|
Lumberjack.use_context(nil) do
|
400
519
|
scope_tags = thread_local_value(:lumberjack_logger_tags)
|
@@ -413,7 +532,7 @@ module Lumberjack
|
|
413
532
|
private
|
414
533
|
|
415
534
|
# Dereference arguments to log calls so we can have methods with compatibility with ::Logger
|
416
|
-
def call_add_entry(severity, message_or_progname_or_tags, progname_or_tags, &block)
|
535
|
+
def call_add_entry(severity, message_or_progname_or_tags, progname_or_tags, &block) # :nodoc:
|
417
536
|
message = nil
|
418
537
|
progname = nil
|
419
538
|
tags = nil
|
@@ -438,7 +557,7 @@ module Lumberjack
|
|
438
557
|
end
|
439
558
|
|
440
559
|
# Set a local value for a thread tied to this object.
|
441
|
-
def set_thread_local_value(name, value)
|
560
|
+
def set_thread_local_value(name, value) # :nodoc:
|
442
561
|
values = Thread.current[name]
|
443
562
|
unless values
|
444
563
|
values = {}
|
@@ -453,13 +572,13 @@ module Lumberjack
|
|
453
572
|
end
|
454
573
|
|
455
574
|
# Get a local value for a thread tied to this object.
|
456
|
-
def thread_local_value(name)
|
575
|
+
def thread_local_value(name) # :nodoc:
|
457
576
|
values = Thread.current[name]
|
458
577
|
values[self] if values
|
459
578
|
end
|
460
579
|
|
461
580
|
# Set a local value for a thread tied to this object within a block.
|
462
|
-
def push_thread_local_value(name, value)
|
581
|
+
def push_thread_local_value(name, value) # :nodoc:
|
463
582
|
save_val = thread_local_value(name)
|
464
583
|
set_thread_local_value(name, value)
|
465
584
|
begin
|
@@ -470,7 +589,7 @@ module Lumberjack
|
|
470
589
|
end
|
471
590
|
|
472
591
|
# Open a logging device.
|
473
|
-
def open_device(device, options)
|
592
|
+
def open_device(device, options) # :nodoc:
|
474
593
|
if device.nil?
|
475
594
|
nil
|
476
595
|
elsif device.is_a?(Device)
|
@@ -491,7 +610,7 @@ module Lumberjack
|
|
491
610
|
end
|
492
611
|
end
|
493
612
|
|
494
|
-
def write_to_device(entry)
|
613
|
+
def write_to_device(entry) # :nodoc:
|
495
614
|
device.write(entry)
|
496
615
|
rescue => e
|
497
616
|
# rubocop:disable Style/StderrPuts
|
@@ -501,7 +620,7 @@ module Lumberjack
|
|
501
620
|
end
|
502
621
|
|
503
622
|
# Create a thread that will periodically call flush.
|
504
|
-
def create_flusher_thread(flush_seconds)
|
623
|
+
def create_flusher_thread(flush_seconds) # :nodoc:
|
505
624
|
if flush_seconds > 0
|
506
625
|
begin
|
507
626
|
logger = self
|
data/lib/lumberjack/severity.rb
CHANGED
@@ -14,10 +14,18 @@ module Lumberjack
|
|
14
14
|
SEVERITY_LABELS = %w[DEBUG INFO WARN ERROR FATAL UNKNOWN].freeze
|
15
15
|
|
16
16
|
class << self
|
17
|
+
# Convert a severity level to a label.
|
18
|
+
#
|
19
|
+
# @param [Integer] severity The severity level to convert.
|
20
|
+
# @return [String] The severity label.
|
17
21
|
def level_to_label(severity)
|
18
22
|
SEVERITY_LABELS[severity] || SEVERITY_LABELS.last
|
19
23
|
end
|
20
24
|
|
25
|
+
# Convert a severity label to a level.
|
26
|
+
#
|
27
|
+
# @param [String, Symbol] label The severity label to convert.
|
28
|
+
# @return [Integer] The severity level.
|
21
29
|
def label_to_level(label)
|
22
30
|
SEVERITY_LABELS.index(label.to_s.upcase) || UNKNOWN
|
23
31
|
end
|
@@ -16,6 +16,10 @@ module Lumberjack
|
|
16
16
|
|
17
17
|
# Add a default formatter applied to all tag values. This can either be a Lumberjack::Formatter
|
18
18
|
# or an object that responds to `call` or a block.
|
19
|
+
#
|
20
|
+
# @param [Lumberjack::Formatter, #call, nil] formatter The formatter to use.
|
21
|
+
# If this is nil, then the block will be used as the formatter.
|
22
|
+
# @return [Lumberjack::TagFormatter] self
|
19
23
|
def default(formatter = nil, &block)
|
20
24
|
formatter ||= block
|
21
25
|
formatter = dereference_formatter(formatter)
|
@@ -24,6 +28,8 @@ module Lumberjack
|
|
24
28
|
end
|
25
29
|
|
26
30
|
# Remove the default formatter.
|
31
|
+
#
|
32
|
+
# @return [Lumberjack::TagFormatter] self
|
27
33
|
def remove_default
|
28
34
|
@default_formatter = nil
|
29
35
|
self
|
@@ -32,6 +38,11 @@ module Lumberjack
|
|
32
38
|
# Add a formatter for specific tag names. This can either be a Lumberjack::Formatter
|
33
39
|
# or an object that responds to `call` or a block. The default formatter will not be
|
34
40
|
# applied.
|
41
|
+
#
|
42
|
+
# @param [String, Array<String>] names The tag names to apply the formatter to.
|
43
|
+
# @param [Lumberjack::Formatter, #call, nil] formatter The formatter to use.
|
44
|
+
# If this is nil, then the block will be used as the formatter.
|
45
|
+
# @return [Lumberjack::TagFormatter] self
|
35
46
|
def add(names, formatter = nil, &block)
|
36
47
|
formatter ||= block
|
37
48
|
formatter = dereference_formatter(formatter)
|
@@ -46,6 +57,9 @@ module Lumberjack
|
|
46
57
|
end
|
47
58
|
|
48
59
|
# Remove formatters for specific tag names. The default formatter will still be applied.
|
60
|
+
#
|
61
|
+
# @param [String, Array<String>] names The tag names to remove the formatter from.
|
62
|
+
# @return [Lumberjack::TagFormatter] self
|
49
63
|
def remove(names)
|
50
64
|
Array(names).each do |name|
|
51
65
|
@formatters.delete(name.to_s)
|
@@ -54,6 +68,8 @@ module Lumberjack
|
|
54
68
|
end
|
55
69
|
|
56
70
|
# Remove all formatters.
|
71
|
+
#
|
72
|
+
# @return [Lumberjack::TagFormatter] self
|
57
73
|
def clear
|
58
74
|
@default_formatter = nil
|
59
75
|
@formatters.clear
|
@@ -61,6 +77,9 @@ module Lumberjack
|
|
61
77
|
end
|
62
78
|
|
63
79
|
# Format a hash of tags using the formatters
|
80
|
+
#
|
81
|
+
# @param [Hash] tags The tags to format.
|
82
|
+
# @return [Hash] The formatted tags.
|
64
83
|
def format(tags)
|
65
84
|
return nil if tags.nil?
|
66
85
|
if @default_formatter.nil? && (@formatters.empty? || (@formatters.keys & tags.keys).empty?)
|
@@ -55,7 +55,7 @@ module Lumberjack
|
|
55
55
|
|
56
56
|
def pop_tags(size = 1)
|
57
57
|
tagged_values = Array(@tags["tagged"])
|
58
|
-
tagged_values = (tagged_values.size > size ? tagged_values[0, tagged_values.size - size] : nil)
|
58
|
+
tagged_values = ((tagged_values.size > size) ? tagged_values[0, tagged_values.size - size] : nil)
|
59
59
|
tag("tagged" => tagged_values)
|
60
60
|
end
|
61
61
|
|
data/lib/lumberjack/tags.rb
CHANGED
@@ -5,6 +5,9 @@ module Lumberjack
|
|
5
5
|
class << self
|
6
6
|
# Transform hash keys to strings. This method exists for optimization and backward compatibility.
|
7
7
|
# If a hash already has string keys, it will be returned as is.
|
8
|
+
#
|
9
|
+
# @param [Hash] hash The hash to transform.
|
10
|
+
# @return [Hash] The hash with string keys.
|
8
11
|
def stringify_keys(hash)
|
9
12
|
return nil if hash.nil?
|
10
13
|
if hash.keys.all? { |key| key.is_a?(String) }
|
@@ -22,6 +25,9 @@ module Lumberjack
|
|
22
25
|
|
23
26
|
# Ensure keys are strings and expand any values in a hash that are Proc's by calling them and replacing
|
24
27
|
# the value with the result. This allows setting global tags with runtime values.
|
28
|
+
#
|
29
|
+
# @param [Hash] hash The hash to transform.
|
30
|
+
# @return [Hash] The hash with string keys and expanded values.
|
25
31
|
def expand_runtime_values(hash)
|
26
32
|
return nil if hash.nil?
|
27
33
|
if hash.all? { |key, value| key.is_a?(String) && !value.is_a?(Proc) }
|