lumberjack 1.2.7 → 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 +114 -56
- data/README.md +6 -5
- data/VERSION +1 -1
- data/lib/lumberjack/context.rb +13 -0
- data/lib/lumberjack/device/date_rolling_log_file.rb +16 -7
- data/lib/lumberjack/device/log_file.rb +13 -6
- data/lib/lumberjack/device/multi.rb +7 -6
- data/lib/lumberjack/device/null.rb +1 -1
- data/lib/lumberjack/device/rolling_log_file.rb +44 -20
- data/lib/lumberjack/device/size_rolling_log_file.rb +9 -9
- data/lib/lumberjack/device/writer.rb +32 -12
- data/lib/lumberjack/device.rb +27 -12
- data/lib/lumberjack/formatter/date_time_formatter.rb +3 -3
- data/lib/lumberjack/formatter/exception_formatter.rb +2 -2
- data/lib/lumberjack/formatter/id_formatter.rb +3 -2
- data/lib/lumberjack/formatter/pretty_print_formatter.rb +6 -4
- data/lib/lumberjack/formatter/structured_formatter.rb +2 -0
- data/lib/lumberjack/formatter/truncate_formatter.rb +27 -0
- data/lib/lumberjack/formatter.rb +69 -23
- data/lib/lumberjack/log_entry.rb +16 -10
- data/lib/lumberjack/logger.rb +167 -29
- data/lib/lumberjack/rack/request_id.rb +1 -1
- data/lib/lumberjack/rack/unit_of_work.rb +1 -1
- data/lib/lumberjack/rack.rb +3 -3
- data/lib/lumberjack/severity.rb +9 -2
- data/lib/lumberjack/tag_formatter.rb +20 -3
- data/lib/lumberjack/tagged_logger_support.rb +1 -2
- data/lib/lumberjack/tagged_logging.rb +1 -1
- data/lib/lumberjack/tags.rb +7 -1
- data/lib/lumberjack/template.rb +15 -3
- data/lib/lumberjack.rb +51 -30
- data/lumberjack.gemspec +11 -14
- metadata +9 -50
data/lib/lumberjack/logger.rb
CHANGED
@@ -63,7 +63,10 @@ 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
|
-
|
66
|
+
#
|
67
|
+
# @param [Lumberjack::Device, Object, Symbol, String] device The device to log to.
|
68
|
+
# @param [Hash] options The options for the logger.
|
69
|
+
def initialize(device = $stdout, options = {})
|
67
70
|
options = options.dup
|
68
71
|
self.level = options.delete(:level) || INFO
|
69
72
|
self.progname = options.delete(:progname)
|
@@ -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,6 +104,8 @@ 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
|
@@ -104,22 +114,30 @@ module Lumberjack
|
|
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
|
-
if value.is_a?(Integer)
|
109
|
-
|
121
|
+
@level = if value.is_a?(Integer)
|
122
|
+
value
|
110
123
|
else
|
111
|
-
|
124
|
+
Severity.label_to_level(value)
|
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,8 +154,10 @@ 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
|
142
162
|
end
|
143
163
|
|
@@ -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")
|
@@ -173,10 +199,10 @@ module Lumberjack
|
|
173
199
|
if current_tags.empty?
|
174
200
|
tags = Tags.stringify_keys(tags) unless tags.nil?
|
175
201
|
else
|
176
|
-
if tags.nil?
|
177
|
-
|
202
|
+
tags = if tags.nil?
|
203
|
+
current_tags.dup
|
178
204
|
else
|
179
|
-
|
205
|
+
current_tags.merge(Tags.stringify_keys(tags))
|
180
206
|
end
|
181
207
|
end
|
182
208
|
tags = Tags.expand_runtime_values(tags)
|
@@ -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
|
@@ -206,6 +237,8 @@ module Lumberjack
|
|
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
|
@@ -325,7 +429,7 @@ module Lumberjack
|
|
325
429
|
def silence(temporary_level = ERROR, &block)
|
326
430
|
if silencer
|
327
431
|
unless temporary_level.is_a?(Integer)
|
328
|
-
temporary_level = Severity
|
432
|
+
temporary_level = Severity.label_to_level(temporary_level)
|
329
433
|
end
|
330
434
|
push_thread_local_value(:lumberjack_logger_level, temporary_level, &block)
|
331
435
|
else
|
@@ -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)
|
@@ -361,15 +473,19 @@ module Lumberjack
|
|
361
473
|
push_thread_local_value(:lumberjack_logger_tags, merged_tags, &block)
|
362
474
|
elsif thread_tags
|
363
475
|
thread_tags.merge!(tags)
|
476
|
+
nil
|
364
477
|
else
|
365
478
|
@tags.merge!(tags)
|
479
|
+
nil
|
366
480
|
end
|
367
|
-
nil
|
368
481
|
end
|
369
482
|
|
370
483
|
# Remove a tag from the current tag context. If this is called inside a block to a
|
371
484
|
# call to `tag`, the tags will only be removed for the duration of that block. Otherwise
|
372
485
|
# they will be removed from the global tags.
|
486
|
+
#
|
487
|
+
# @param [Array<String, Symbol>] tag_names The tags to remove.
|
488
|
+
# @return [void]
|
373
489
|
def remove_tag(*tag_names)
|
374
490
|
thread_tags = thread_local_value(:lumberjack_logger_tags)
|
375
491
|
if thread_tags
|
@@ -380,21 +496,43 @@ module Lumberjack
|
|
380
496
|
end
|
381
497
|
|
382
498
|
# Return all tags in scope on the logger including global tags set on the Lumberjack
|
383
|
-
# context, tags set on the logger, and tags set on the current block for the logger
|
499
|
+
# context, tags set on the logger, and tags set on the current block for the logger.
|
500
|
+
#
|
501
|
+
# @return [Hash]
|
384
502
|
def tags
|
385
503
|
tags = {}
|
386
504
|
context_tags = Lumberjack.context_tags
|
387
505
|
tags.merge!(context_tags) if context_tags && !context_tags.empty?
|
388
|
-
tags.merge!(@tags) if !@tags.empty?
|
506
|
+
tags.merge!(@tags) if !@tags.empty? && !thread_local_value(:lumberjack_logger_untagged)
|
389
507
|
scope_tags = thread_local_value(:lumberjack_logger_tags)
|
390
508
|
tags.merge!(scope_tags) if scope_tags && !scope_tags.empty?
|
391
509
|
tags
|
392
510
|
end
|
393
511
|
|
512
|
+
# Remove all tags on the current logger and logging context within a block.
|
513
|
+
# You can still set new block scoped tags within theuntagged block and provide
|
514
|
+
# tags on individual log methods.
|
515
|
+
#
|
516
|
+
# @return [void]
|
517
|
+
def untagged(&block)
|
518
|
+
Lumberjack.use_context(nil) do
|
519
|
+
scope_tags = thread_local_value(:lumberjack_logger_tags)
|
520
|
+
untagged = thread_local_value(:lumberjack_logger_untagged)
|
521
|
+
begin
|
522
|
+
set_thread_local_value(:lumberjack_logger_untagged, true)
|
523
|
+
set_thread_local_value(:lumberjack_logger_tags, nil)
|
524
|
+
tag({}, &block)
|
525
|
+
ensure
|
526
|
+
set_thread_local_value(:lumberjack_logger_untagged, untagged)
|
527
|
+
set_thread_local_value(:lumberjack_logger_tags, scope_tags)
|
528
|
+
end
|
529
|
+
end
|
530
|
+
end
|
531
|
+
|
394
532
|
private
|
395
533
|
|
396
534
|
# Dereference arguments to log calls so we can have methods with compatibility with ::Logger
|
397
|
-
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:
|
398
536
|
message = nil
|
399
537
|
progname = nil
|
400
538
|
tags = nil
|
@@ -419,7 +557,7 @@ module Lumberjack
|
|
419
557
|
end
|
420
558
|
|
421
559
|
# Set a local value for a thread tied to this object.
|
422
|
-
def set_thread_local_value(name, value)
|
560
|
+
def set_thread_local_value(name, value) # :nodoc:
|
423
561
|
values = Thread.current[name]
|
424
562
|
unless values
|
425
563
|
values = {}
|
@@ -434,13 +572,13 @@ module Lumberjack
|
|
434
572
|
end
|
435
573
|
|
436
574
|
# Get a local value for a thread tied to this object.
|
437
|
-
def thread_local_value(name)
|
575
|
+
def thread_local_value(name) # :nodoc:
|
438
576
|
values = Thread.current[name]
|
439
577
|
values[self] if values
|
440
578
|
end
|
441
579
|
|
442
580
|
# Set a local value for a thread tied to this object within a block.
|
443
|
-
def push_thread_local_value(name, value)
|
581
|
+
def push_thread_local_value(name, value) # :nodoc:
|
444
582
|
save_val = thread_local_value(name)
|
445
583
|
set_thread_local_value(name, value)
|
446
584
|
begin
|
@@ -451,7 +589,7 @@ module Lumberjack
|
|
451
589
|
end
|
452
590
|
|
453
591
|
# Open a logging device.
|
454
|
-
def open_device(device, options)
|
592
|
+
def open_device(device, options) # :nodoc:
|
455
593
|
if device.nil?
|
456
594
|
nil
|
457
595
|
elsif device.is_a?(Device)
|
@@ -472,27 +610,27 @@ module Lumberjack
|
|
472
610
|
end
|
473
611
|
end
|
474
612
|
|
475
|
-
def write_to_device(entry)
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
613
|
+
def write_to_device(entry) # :nodoc:
|
614
|
+
device.write(entry)
|
615
|
+
rescue => e
|
616
|
+
# rubocop:disable Style/StderrPuts
|
617
|
+
$stderr.puts("#{e.class.name}: #{e.message}#{" at " + e.backtrace.first if e.backtrace}")
|
618
|
+
$stderr.puts(entry.to_s)
|
619
|
+
# rubocop:enable Style/StderrPuts
|
482
620
|
end
|
483
621
|
|
484
622
|
# Create a thread that will periodically call flush.
|
485
|
-
def create_flusher_thread(flush_seconds)
|
623
|
+
def create_flusher_thread(flush_seconds) # :nodoc:
|
486
624
|
if flush_seconds > 0
|
487
625
|
begin
|
488
626
|
logger = self
|
489
627
|
Thread.new do
|
490
|
-
|
628
|
+
until closed?
|
491
629
|
begin
|
492
630
|
sleep(flush_seconds)
|
493
631
|
logger.flush if Time.now - logger.last_flushed_at >= flush_seconds
|
494
632
|
rescue => e
|
495
|
-
|
633
|
+
warn("Error flushing log: #{e.inspect}")
|
496
634
|
end
|
497
635
|
end
|
498
636
|
end
|
@@ -16,7 +16,7 @@ module Lumberjack
|
|
16
16
|
def call(env)
|
17
17
|
request_id = env[REQUEST_ID]
|
18
18
|
if request_id && @abbreviated
|
19
|
-
request_id = request_id.split(
|
19
|
+
request_id = request_id.split("-", 2).first
|
20
20
|
end
|
21
21
|
Lumberjack.unit_of_work(request_id) do
|
22
22
|
@app.call(env)
|
data/lib/lumberjack/rack.rb
CHANGED
@@ -2,8 +2,8 @@
|
|
2
2
|
|
3
3
|
module Lumberjack
|
4
4
|
module Rack
|
5
|
-
require_relative "rack/unit_of_work
|
6
|
-
require_relative "rack/request_id
|
7
|
-
require_relative "rack/context
|
5
|
+
require_relative "rack/unit_of_work"
|
6
|
+
require_relative "rack/request_id"
|
7
|
+
require_relative "rack/context"
|
8
8
|
end
|
9
9
|
end
|
data/lib/lumberjack/severity.rb
CHANGED
@@ -11,17 +11,24 @@ module Lumberjack
|
|
11
11
|
FATAL = ::Logger::Severity::FATAL
|
12
12
|
UNKNOWN = ::Logger::Severity::UNKNOWN
|
13
13
|
|
14
|
-
SEVERITY_LABELS = %w
|
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
|
24
32
|
end
|
25
|
-
|
26
33
|
end
|
27
34
|
end
|
@@ -9,7 +9,6 @@ module Lumberjack
|
|
9
9
|
# tag_formatter.add(["password", "email"]) { |value| "***" }
|
10
10
|
# tag_formatter.add("finished_at", Lumberjack::Formatter::DateTimeFormatter.new("%Y-%m-%dT%H:%m:%S%z"))
|
11
11
|
class TagFormatter
|
12
|
-
|
13
12
|
def initialize
|
14
13
|
@formatters = {}
|
15
14
|
@default_formatter = nil
|
@@ -17,6 +16,10 @@ module Lumberjack
|
|
17
16
|
|
18
17
|
# Add a default formatter applied to all tag values. This can either be a Lumberjack::Formatter
|
19
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
|
20
23
|
def default(formatter = nil, &block)
|
21
24
|
formatter ||= block
|
22
25
|
formatter = dereference_formatter(formatter)
|
@@ -25,6 +28,8 @@ module Lumberjack
|
|
25
28
|
end
|
26
29
|
|
27
30
|
# Remove the default formatter.
|
31
|
+
#
|
32
|
+
# @return [Lumberjack::TagFormatter] self
|
28
33
|
def remove_default
|
29
34
|
@default_formatter = nil
|
30
35
|
self
|
@@ -33,6 +38,11 @@ module Lumberjack
|
|
33
38
|
# Add a formatter for specific tag names. This can either be a Lumberjack::Formatter
|
34
39
|
# or an object that responds to `call` or a block. The default formatter will not be
|
35
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
|
36
46
|
def add(names, formatter = nil, &block)
|
37
47
|
formatter ||= block
|
38
48
|
formatter = dereference_formatter(formatter)
|
@@ -47,6 +57,9 @@ module Lumberjack
|
|
47
57
|
end
|
48
58
|
|
49
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
|
50
63
|
def remove(names)
|
51
64
|
Array(names).each do |name|
|
52
65
|
@formatters.delete(name.to_s)
|
@@ -55,6 +68,8 @@ module Lumberjack
|
|
55
68
|
end
|
56
69
|
|
57
70
|
# Remove all formatters.
|
71
|
+
#
|
72
|
+
# @return [Lumberjack::TagFormatter] self
|
58
73
|
def clear
|
59
74
|
@default_formatter = nil
|
60
75
|
@formatters.clear
|
@@ -62,6 +77,9 @@ module Lumberjack
|
|
62
77
|
end
|
63
78
|
|
64
79
|
# Format a hash of tags using the formatters
|
80
|
+
#
|
81
|
+
# @param [Hash] tags The tags to format.
|
82
|
+
# @return [Hash] The formatted tags.
|
65
83
|
def format(tags)
|
66
84
|
return nil if tags.nil?
|
67
85
|
if @default_formatter.nil? && (@formatters.empty? || (@formatters.keys & tags.keys).empty?)
|
@@ -87,12 +105,11 @@ module Lumberjack
|
|
87
105
|
if formatter.is_a?(TaggedLoggerSupport::Formatter)
|
88
106
|
formatter.__formatter
|
89
107
|
elsif formatter.is_a?(Symbol)
|
90
|
-
formatter_class_name = "#{formatter.to_s.gsub(/(^|_)([a-z])/){|m| $~[2].upcase}}Formatter"
|
108
|
+
formatter_class_name = "#{formatter.to_s.gsub(/(^|_)([a-z])/) { |m| $~[2].upcase }}Formatter"
|
91
109
|
Formatter.const_get(formatter_class_name).new
|
92
110
|
else
|
93
111
|
formatter
|
94
112
|
end
|
95
113
|
end
|
96
|
-
|
97
114
|
end
|
98
115
|
end
|
@@ -6,7 +6,6 @@ require "forwardable"
|
|
6
6
|
module Lumberjack
|
7
7
|
# Methods to make Lumberjack::Logger API compatible with ActiveSupport::TaggedLogger.
|
8
8
|
module TaggedLoggerSupport
|
9
|
-
|
10
9
|
class Formatter < DelegateClass(Lumberjack::Formatter)
|
11
10
|
extend Forwardable
|
12
11
|
def_delegators :@logger, :tagged, :push_tags, :pop_tags, :clear_tags!
|
@@ -56,7 +55,7 @@ module Lumberjack
|
|
56
55
|
|
57
56
|
def pop_tags(size = 1)
|
58
57
|
tagged_values = Array(@tags["tagged"])
|
59
|
-
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)
|
60
59
|
tag("tagged" => tagged_values)
|
61
60
|
end
|
62
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,12 +25,15 @@ 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) }
|
28
34
|
return hash
|
29
35
|
end
|
30
|
-
|
36
|
+
|
31
37
|
copy = {}
|
32
38
|
hash.each do |key, value|
|
33
39
|
if value.is_a?(Proc) && (value.arity == 0 || value.arity == -1)
|