lumberjack 1.2.8 → 1.2.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +152 -58
- data/README.md +35 -6
- data/VERSION +1 -1
- data/lib/lumberjack/context.rb +13 -0
- data/lib/lumberjack/device/date_rolling_log_file.rb +10 -1
- data/lib/lumberjack/device/log_file.rb +8 -1
- data/lib/lumberjack/device/multi.rb +2 -1
- data/lib/lumberjack/device/null.rb +1 -1
- data/lib/lumberjack/device/rolling_log_file.rb +22 -22
- data/lib/lumberjack/device/size_rolling_log_file.rb +1 -1
- data/lib/lumberjack/device/writer.rb +21 -1
- data/lib/lumberjack/device.rb +16 -1
- data/lib/lumberjack/formatter/date_time_formatter.rb +2 -1
- data/lib/lumberjack/formatter/exception_formatter.rb +4 -2
- data/lib/lumberjack/formatter/id_formatter.rb +2 -1
- data/lib/lumberjack/formatter/inspect_formatter.rb +1 -1
- data/lib/lumberjack/formatter/object_formatter.rb +1 -1
- data/lib/lumberjack/formatter/pretty_print_formatter.rb +3 -1
- data/lib/lumberjack/formatter/string_formatter.rb +1 -1
- data/lib/lumberjack/formatter/strip_formatter.rb +1 -1
- data/lib/lumberjack/formatter/structured_formatter.rb +3 -1
- data/lib/lumberjack/formatter/truncate_formatter.rb +27 -0
- data/lib/lumberjack/formatter.rb +55 -9
- data/lib/lumberjack/log_entry.rb +10 -2
- data/lib/lumberjack/logger.rb +141 -18
- data/lib/lumberjack/rack/context.rb +1 -1
- data/lib/lumberjack/rack/request_id.rb +1 -1
- data/lib/lumberjack/rack/unit_of_work.rb +1 -1
- data/lib/lumberjack/rack.rb +1 -1
- data/lib/lumberjack/severity.rb +21 -1
- 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 +15 -3
- data/lib/lumberjack.rb +20 -2
- metadata +8 -7
data/lib/lumberjack/logger.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Lumberjack
|
4
4
|
# Logger is a thread safe logging object. It has a compatible API with the Ruby
|
@@ -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,30 +104,44 @@ 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
|
-
@level =
|
109
|
-
value
|
110
|
-
else
|
111
|
-
Severity.label_to_level(value)
|
112
|
-
end
|
121
|
+
@level = Severity.coerce(value)
|
113
122
|
end
|
114
123
|
|
115
|
-
|
124
|
+
alias_method :sev_threshold=, :level=
|
125
|
+
|
126
|
+
# Adjust the log level during the block execution for the current Fiber only.
|
127
|
+
#
|
128
|
+
# @param [Integer, Symbol, String] severity The severity level.
|
129
|
+
# @return [Object] The result of the block.
|
130
|
+
def with_level(severity, &block)
|
131
|
+
push_thread_local_value(:lumberjack_logger_level, Severity.coerce(severity), &block)
|
132
|
+
end
|
116
133
|
|
117
134
|
# Set the Lumberjack::Formatter used to format objects for logging as messages.
|
135
|
+
#
|
136
|
+
# @param [Lumberjack::Formatter, Object] value The formatter to use.
|
137
|
+
# @return [void]
|
118
138
|
def formatter=(value)
|
119
139
|
@_formatter = (value.is_a?(TaggedLoggerSupport::Formatter) ? value.__formatter : value)
|
120
140
|
end
|
121
141
|
|
122
142
|
# Get the Lumberjack::Formatter used to format objects for logging as messages.
|
143
|
+
#
|
144
|
+
# @return [Lumberjack::Formatter] The formatter.
|
123
145
|
def formatter
|
124
146
|
if respond_to?(:tagged)
|
125
147
|
# Wrap in an object that supports ActiveSupport::TaggedLogger API
|
@@ -136,6 +158,8 @@ module Lumberjack
|
|
136
158
|
# The tags added with this method are just strings so they are stored in the logger tags
|
137
159
|
# in an array under the "tagged" tag. So calling `logger.tagged("foo", "bar")` will result
|
138
160
|
# in tags `{"tagged" => ["foo", "bar"]}`.
|
161
|
+
#
|
162
|
+
# @return [Lumberjack::Logger] self.
|
139
163
|
def tagged_logger!
|
140
164
|
extend(TaggedLoggerSupport)
|
141
165
|
self
|
@@ -149,7 +173,13 @@ module Lumberjack
|
|
149
173
|
# The severity can be passed in either as one of the Severity constants,
|
150
174
|
# or as a Severity label.
|
151
175
|
#
|
152
|
-
#
|
176
|
+
# @param [Integer, Symbol, String] severity The severity of the message.
|
177
|
+
# @param [Object] message The message to log.
|
178
|
+
# @param [String] progname The name of the program that is logging the message.
|
179
|
+
# @param [Hash] tags The tags to add to the log entry.
|
180
|
+
# @return [void]
|
181
|
+
#
|
182
|
+
# @example
|
153
183
|
#
|
154
184
|
# logger.add_entry(Logger::ERROR, exception)
|
155
185
|
# logger.add_entry(Logger::INFO, "Request completed")
|
@@ -191,6 +221,11 @@ module Lumberjack
|
|
191
221
|
end
|
192
222
|
|
193
223
|
# ::Logger compatible method to add a log entry.
|
224
|
+
#
|
225
|
+
# @param [Integer, Symbol, String] severity The severity of the message.
|
226
|
+
# @param [Object] message The message to log.
|
227
|
+
# @param [String] progname The name of the program that is logging the message.
|
228
|
+
# @return [void]
|
194
229
|
def add(severity, message = nil, progname = nil, &block)
|
195
230
|
if message.nil?
|
196
231
|
if block
|
@@ -203,9 +238,11 @@ module Lumberjack
|
|
203
238
|
add_entry(severity, message, progname)
|
204
239
|
end
|
205
240
|
|
206
|
-
|
241
|
+
alias_method :log, :add
|
207
242
|
|
208
243
|
# Flush the logging device. Messages are not guaranteed to be written until this method is called.
|
244
|
+
#
|
245
|
+
# @return [void]
|
209
246
|
def flush
|
210
247
|
device.flush
|
211
248
|
@last_flushed_at = Time.now
|
@@ -213,102 +250,170 @@ module Lumberjack
|
|
213
250
|
end
|
214
251
|
|
215
252
|
# Close the logging device.
|
253
|
+
#
|
254
|
+
# @return [void]
|
216
255
|
def close
|
217
256
|
flush
|
218
257
|
device.close if device.respond_to?(:close)
|
219
258
|
@closed = true
|
220
259
|
end
|
221
260
|
|
261
|
+
# Returns +true+ if the logging device is closed.
|
262
|
+
#
|
263
|
+
# @return [Boolean] +true+ if the logging device is closed.
|
222
264
|
def closed?
|
223
265
|
@closed
|
224
266
|
end
|
225
267
|
|
268
|
+
# Reopen the logging device.
|
269
|
+
#
|
270
|
+
# @param [Object] logdev passed through to the logging device.
|
226
271
|
def reopen(logdev = nil)
|
227
272
|
@closed = false
|
228
273
|
device.reopen(logdev) if device.respond_to?(:reopen)
|
229
274
|
end
|
230
275
|
|
231
276
|
# Log a +FATAL+ message. The message can be passed in either the +message+ argument or in a block.
|
277
|
+
#
|
278
|
+
# @param [Object] message_or_progname_or_tags The message to log or progname
|
279
|
+
# if the message is passed in a block.
|
280
|
+
# @param [String, Hash] progname_or_tags The name of the program that is logging the message or tags
|
281
|
+
# if the message is passed in a block.
|
282
|
+
# @return [void]
|
232
283
|
def fatal(message_or_progname_or_tags = nil, progname_or_tags = nil, &block)
|
233
284
|
call_add_entry(FATAL, message_or_progname_or_tags, progname_or_tags, &block)
|
234
285
|
end
|
235
286
|
|
236
287
|
# Return +true+ if +FATAL+ messages are being logged.
|
288
|
+
#
|
289
|
+
# @return [Boolean]
|
237
290
|
def fatal?
|
238
291
|
level <= FATAL
|
239
292
|
end
|
240
293
|
|
241
294
|
# Set the log level to fatal.
|
295
|
+
#
|
296
|
+
# @return [void]
|
242
297
|
def fatal!
|
243
298
|
self.level = FATAL
|
244
299
|
end
|
245
300
|
|
246
301
|
# Log an +ERROR+ message. The message can be passed in either the +message+ argument or in a block.
|
302
|
+
#
|
303
|
+
# @param [Object] message_or_progname_or_tags The message to log or progname
|
304
|
+
# if the message is passed in a block.
|
305
|
+
# @param [String, Hash] progname_or_tags The name of the program that is logging the message or tags
|
306
|
+
# if the message is passed in a block.
|
307
|
+
# @return [void]
|
247
308
|
def error(message_or_progname_or_tags = nil, progname_or_tags = nil, &block)
|
248
309
|
call_add_entry(ERROR, message_or_progname_or_tags, progname_or_tags, &block)
|
249
310
|
end
|
250
311
|
|
251
312
|
# Return +true+ if +ERROR+ messages are being logged.
|
313
|
+
#
|
314
|
+
# @return [Boolean]
|
252
315
|
def error?
|
253
316
|
level <= ERROR
|
254
317
|
end
|
255
318
|
|
256
319
|
# Set the log level to error.
|
320
|
+
#
|
321
|
+
# @return [void]
|
257
322
|
def error!
|
258
323
|
self.level = ERROR
|
259
324
|
end
|
260
325
|
|
261
326
|
# Log a +WARN+ message. The message can be passed in either the +message+ argument or in a block.
|
327
|
+
#
|
328
|
+
# @param [Object] message_or_progname_or_tags The message to log or progname
|
329
|
+
# if the message is passed in a block.
|
330
|
+
# @param [String, Hash] progname_or_tags The name of the program that is logging the message or tags
|
331
|
+
# if the message is passed in a block.
|
332
|
+
# @return [void]
|
262
333
|
def warn(message_or_progname_or_tags = nil, progname_or_tags = nil, &block)
|
263
334
|
call_add_entry(WARN, message_or_progname_or_tags, progname_or_tags, &block)
|
264
335
|
end
|
265
336
|
|
266
337
|
# Return +true+ if +WARN+ messages are being logged.
|
338
|
+
#
|
339
|
+
# @return [Boolean]
|
267
340
|
def warn?
|
268
341
|
level <= WARN
|
269
342
|
end
|
270
343
|
|
271
344
|
# Set the log level to warn.
|
345
|
+
#
|
346
|
+
# @return [void]
|
272
347
|
def warn!
|
273
348
|
self.level = WARN
|
274
349
|
end
|
275
350
|
|
276
351
|
# Log an +INFO+ message. The message can be passed in either the +message+ argument or in a block.
|
352
|
+
#
|
353
|
+
# @param [Object] message_or_progname_or_tags The message to log or progname
|
354
|
+
# if the message is passed in a block.
|
355
|
+
# @param [String] progname_or_tags The name of the program that is logging the message or tags
|
356
|
+
# if the message is passed in a block.
|
357
|
+
# @return [void]
|
277
358
|
def info(message_or_progname_or_tags = nil, progname_or_tags = nil, &block)
|
278
359
|
call_add_entry(INFO, message_or_progname_or_tags, progname_or_tags, &block)
|
279
360
|
end
|
280
361
|
|
281
362
|
# Return +true+ if +INFO+ messages are being logged.
|
363
|
+
#
|
364
|
+
|
282
365
|
def info?
|
283
366
|
level <= INFO
|
284
367
|
end
|
285
368
|
|
286
369
|
# Set the log level to info.
|
370
|
+
#
|
371
|
+
# @return [void]
|
287
372
|
def info!
|
288
373
|
self.level = INFO
|
289
374
|
end
|
290
375
|
|
291
376
|
# Log a +DEBUG+ message. The message can be passed in either the +message+ argument or in a block.
|
377
|
+
#
|
378
|
+
# @param [Object] message_or_progname_or_tags The message to log or progname
|
379
|
+
# if the message is passed in a block.
|
380
|
+
# @param [String, Hash] progname_or_tags The name of the program that is logging the message or tags
|
381
|
+
# if the message is passed in a block.
|
382
|
+
# @return [void]
|
292
383
|
def debug(message_or_progname_or_tags = nil, progname_or_tags = nil, &block)
|
293
384
|
call_add_entry(DEBUG, message_or_progname_or_tags, progname_or_tags, &block)
|
294
385
|
end
|
295
386
|
|
296
387
|
# Return +true+ if +DEBUG+ messages are being logged.
|
388
|
+
#
|
389
|
+
# @return [Boolean]
|
297
390
|
def debug?
|
298
391
|
level <= DEBUG
|
299
392
|
end
|
300
393
|
|
301
394
|
# Set the log level to debug.
|
395
|
+
#
|
396
|
+
# @return [void]
|
302
397
|
def debug!
|
303
398
|
self.level = DEBUG
|
304
399
|
end
|
305
400
|
|
306
401
|
# Log a message when the severity is not known. Unknown messages will always appear in the log.
|
307
402
|
# The message can be passed in either the +message+ argument or in a block.
|
403
|
+
#
|
404
|
+
# @param [Object] message_or_progname_or_tags The message to log or progname
|
405
|
+
# if the message is passed in a block.
|
406
|
+
# @param [String, Hash] progname_or_tags The name of the program that is logging the message or tags
|
407
|
+
# if the message is passed in a block.
|
408
|
+
# @return [void]
|
308
409
|
def unknown(message_or_progname_or_tags = nil, progname_or_tags = nil, &block)
|
309
410
|
call_add_entry(UNKNOWN, message_or_progname_or_tags, progname_or_tags, &block)
|
310
411
|
end
|
311
412
|
|
413
|
+
# Add a message when the severity is not known.
|
414
|
+
#
|
415
|
+
# @param [Object] msg The message to log.
|
416
|
+
# @return [void]
|
312
417
|
def <<(msg)
|
313
418
|
add_entry(UNKNOWN, msg)
|
314
419
|
end
|
@@ -316,7 +421,10 @@ module Lumberjack
|
|
316
421
|
# Silence the logger by setting a new log level inside a block. By default, only +ERROR+ or +FATAL+
|
317
422
|
# messages will be logged.
|
318
423
|
#
|
319
|
-
#
|
424
|
+
# @param [Integer, String, Symbol] temporary_level The log level to use inside the block.
|
425
|
+
# @return [Object] The result of the block.
|
426
|
+
#
|
427
|
+
# @example
|
320
428
|
#
|
321
429
|
# logger.level = Logger::INFO
|
322
430
|
# logger.silence do
|
@@ -335,6 +443,9 @@ module Lumberjack
|
|
335
443
|
|
336
444
|
# Set the program name that is associated with log messages. If a block
|
337
445
|
# is given, the program name will be valid only within the block.
|
446
|
+
#
|
447
|
+
# @param [String] value The program name to use.
|
448
|
+
# @return [void]
|
338
449
|
def set_progname(value, &block)
|
339
450
|
if block
|
340
451
|
push_thread_local_value(:lumberjack_logger_progname, value, &block)
|
@@ -344,6 +455,8 @@ module Lumberjack
|
|
344
455
|
end
|
345
456
|
|
346
457
|
# Get the program name associated with log messages.
|
458
|
+
#
|
459
|
+
# @return [String]
|
347
460
|
def progname
|
348
461
|
thread_local_value(:lumberjack_logger_progname) || @progname
|
349
462
|
end
|
@@ -353,6 +466,9 @@ module Lumberjack
|
|
353
466
|
# the tags will only be defined on the tags in that block. When the parent block
|
354
467
|
# exits, all the tags will be reverted. If there is no block, then the tags will
|
355
468
|
# be defined as global and apply to all log statements.
|
469
|
+
#
|
470
|
+
# @param [Hash] tags The tags to set.
|
471
|
+
# @return [void]
|
356
472
|
def tag(tags, &block)
|
357
473
|
tags = Tags.stringify_keys(tags)
|
358
474
|
thread_tags = thread_local_value(:lumberjack_logger_tags)
|
@@ -371,6 +487,9 @@ module Lumberjack
|
|
371
487
|
# Remove a tag from the current tag context. If this is called inside a block to a
|
372
488
|
# call to `tag`, the tags will only be removed for the duration of that block. Otherwise
|
373
489
|
# they will be removed from the global tags.
|
490
|
+
#
|
491
|
+
# @param [Array<String, Symbol>] tag_names The tags to remove.
|
492
|
+
# @return [void]
|
374
493
|
def remove_tag(*tag_names)
|
375
494
|
thread_tags = thread_local_value(:lumberjack_logger_tags)
|
376
495
|
if thread_tags
|
@@ -382,6 +501,8 @@ module Lumberjack
|
|
382
501
|
|
383
502
|
# Return all tags in scope on the logger including global tags set on the Lumberjack
|
384
503
|
# context, tags set on the logger, and tags set on the current block for the logger.
|
504
|
+
#
|
505
|
+
# @return [Hash]
|
385
506
|
def tags
|
386
507
|
tags = {}
|
387
508
|
context_tags = Lumberjack.context_tags
|
@@ -395,6 +516,8 @@ module Lumberjack
|
|
395
516
|
# Remove all tags on the current logger and logging context within a block.
|
396
517
|
# You can still set new block scoped tags within theuntagged block and provide
|
397
518
|
# tags on individual log methods.
|
519
|
+
#
|
520
|
+
# @return [void]
|
398
521
|
def untagged(&block)
|
399
522
|
Lumberjack.use_context(nil) do
|
400
523
|
scope_tags = thread_local_value(:lumberjack_logger_tags)
|
@@ -413,7 +536,7 @@ module Lumberjack
|
|
413
536
|
private
|
414
537
|
|
415
538
|
# 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)
|
539
|
+
def call_add_entry(severity, message_or_progname_or_tags, progname_or_tags, &block) # :nodoc:
|
417
540
|
message = nil
|
418
541
|
progname = nil
|
419
542
|
tags = nil
|
@@ -438,7 +561,7 @@ module Lumberjack
|
|
438
561
|
end
|
439
562
|
|
440
563
|
# Set a local value for a thread tied to this object.
|
441
|
-
def set_thread_local_value(name, value)
|
564
|
+
def set_thread_local_value(name, value) # :nodoc:
|
442
565
|
values = Thread.current[name]
|
443
566
|
unless values
|
444
567
|
values = {}
|
@@ -453,13 +576,13 @@ module Lumberjack
|
|
453
576
|
end
|
454
577
|
|
455
578
|
# Get a local value for a thread tied to this object.
|
456
|
-
def thread_local_value(name)
|
579
|
+
def thread_local_value(name) # :nodoc:
|
457
580
|
values = Thread.current[name]
|
458
581
|
values[self] if values
|
459
582
|
end
|
460
583
|
|
461
584
|
# Set a local value for a thread tied to this object within a block.
|
462
|
-
def push_thread_local_value(name, value)
|
585
|
+
def push_thread_local_value(name, value) # :nodoc:
|
463
586
|
save_val = thread_local_value(name)
|
464
587
|
set_thread_local_value(name, value)
|
465
588
|
begin
|
@@ -470,7 +593,7 @@ module Lumberjack
|
|
470
593
|
end
|
471
594
|
|
472
595
|
# Open a logging device.
|
473
|
-
def open_device(device, options)
|
596
|
+
def open_device(device, options) # :nodoc:
|
474
597
|
if device.nil?
|
475
598
|
nil
|
476
599
|
elsif device.is_a?(Device)
|
@@ -491,7 +614,7 @@ module Lumberjack
|
|
491
614
|
end
|
492
615
|
end
|
493
616
|
|
494
|
-
def write_to_device(entry)
|
617
|
+
def write_to_device(entry) # :nodoc:
|
495
618
|
device.write(entry)
|
496
619
|
rescue => e
|
497
620
|
# rubocop:disable Style/StderrPuts
|
@@ -501,7 +624,7 @@ module Lumberjack
|
|
501
624
|
end
|
502
625
|
|
503
626
|
# Create a thread that will periodically call flush.
|
504
|
-
def create_flusher_thread(flush_seconds)
|
627
|
+
def create_flusher_thread(flush_seconds) # :nodoc:
|
505
628
|
if flush_seconds > 0
|
506
629
|
begin
|
507
630
|
logger = self
|
data/lib/lumberjack/rack.rb
CHANGED
data/lib/lumberjack/severity.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Lumberjack
|
4
4
|
# The standard severity levels for logging messages.
|
@@ -14,13 +14,33 @@ 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
|
32
|
+
|
33
|
+
# Coerce a value to a severity level.
|
34
|
+
#
|
35
|
+
# @param [Integer, String, Symbol] value The value to coerce.
|
36
|
+
# @return [Integer] The severity level.
|
37
|
+
def coerce(value)
|
38
|
+
if value.is_a?(Integer)
|
39
|
+
value
|
40
|
+
else
|
41
|
+
label_to_level(value)
|
42
|
+
end
|
43
|
+
end
|
24
44
|
end
|
25
45
|
end
|
26
46
|
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) }
|
data/lib/lumberjack/template.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Lumberjack
|
4
4
|
# A template converts entries to strings. Templates can contain the following place holders to
|
@@ -30,6 +30,9 @@ module Lumberjack
|
|
30
30
|
# specified precision.
|
31
31
|
#
|
32
32
|
# Messages will have white space stripped from both ends.
|
33
|
+
#
|
34
|
+
# @param [String] first_line The template to use to format the first line of a message.
|
35
|
+
# @param [Hash] options The options for the template.
|
33
36
|
def initialize(first_line, options = {})
|
34
37
|
@first_line_template, @first_line_tags = compile(first_line)
|
35
38
|
additional_lines = options[:additional_lines] || "#{Lumberjack::LINE_SEPARATOR}:message"
|
@@ -39,6 +42,9 @@ module Lumberjack
|
|
39
42
|
self.datetime_format = (options[:time_format] || :milliseconds)
|
40
43
|
end
|
41
44
|
|
45
|
+
# Set the format used to format the time.
|
46
|
+
#
|
47
|
+
# @param [String] format The format to use to format the time.
|
42
48
|
def datetime_format=(format)
|
43
49
|
if format == :milliseconds
|
44
50
|
format = MILLISECOND_TIME_FORMAT
|
@@ -48,11 +54,17 @@ module Lumberjack
|
|
48
54
|
@time_formatter = Formatter::DateTimeFormatter.new(format)
|
49
55
|
end
|
50
56
|
|
57
|
+
# Get the format used to format the time.
|
58
|
+
#
|
59
|
+
# @return [String]
|
51
60
|
def datetime_format
|
52
61
|
@time_formatter.format
|
53
62
|
end
|
54
63
|
|
55
64
|
# Convert an entry into a string using the template.
|
65
|
+
#
|
66
|
+
# @param [Lumberjack::LogEntry] entry The entry to convert to a string.
|
67
|
+
# @return [String] The entry converted to a string.
|
56
68
|
def call(entry)
|
57
69
|
return entry unless entry.is_a?(LogEntry)
|
58
70
|
|
@@ -86,7 +98,7 @@ module Lumberjack
|
|
86
98
|
def tag_args(tags, tag_vars)
|
87
99
|
return [nil] * (tag_vars.size + 1) if tags.nil? || tags.size == 0
|
88
100
|
|
89
|
-
tags_string = ""
|
101
|
+
tags_string = +""
|
90
102
|
tags.each do |name, value|
|
91
103
|
unless value.nil? || tag_vars.include?(name)
|
92
104
|
value = value.to_s
|
@@ -103,7 +115,7 @@ module Lumberjack
|
|
103
115
|
end
|
104
116
|
|
105
117
|
# Compile the template string into a value that can be used with sprintf.
|
106
|
-
def compile(template)
|
118
|
+
def compile(template) # :nodoc:
|
107
119
|
tag_vars = []
|
108
120
|
template = template.gsub(PLACEHOLDER_PATTERN) do |match|
|
109
121
|
var_name = match.sub("{", "").sub("}", "")
|