lumberjack 1.2.10 → 1.4.0

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.
@@ -16,12 +16,16 @@ module Lumberjack
16
16
  require_relative "formatter/exception_formatter"
17
17
  require_relative "formatter/id_formatter"
18
18
  require_relative "formatter/inspect_formatter"
19
+ require_relative "formatter/multiply_formatter"
19
20
  require_relative "formatter/object_formatter"
20
21
  require_relative "formatter/pretty_print_formatter"
22
+ require_relative "formatter/redact_formatter"
23
+ require_relative "formatter/round_formatter"
21
24
  require_relative "formatter/string_formatter"
22
25
  require_relative "formatter/strip_formatter"
23
26
  require_relative "formatter/structured_formatter"
24
27
  require_relative "formatter/truncate_formatter"
28
+ require_relative "formatter/tagged_message"
25
29
 
26
30
  class << self
27
31
  # Returns a new empty formatter with no mapping. For historical reasons, a formatter
@@ -66,12 +70,12 @@ module Lumberjack
66
70
  # help avoid loading dependency issues. This applies only to classes; modules cannot be
67
71
  # passed in as strings.
68
72
  #
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.
73
+ # @param klass [Class, Module, String, Array<Class, Module, String>] The class or module to add a formatter for.
74
+ # @param formatter [Symbol, Class, String, #call] The formatter to use for the class.
71
75
  # If a symbol is passed in, it will be used to load one of the predefined formatters.
72
76
  # If a class is passed in, it will be initialized with the args passed in.
73
77
  # 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.
78
+ # @param args [Array] Arguments to pass to the formatter when it is initialized.
75
79
  # @yield [obj] A block that will be used as the formatter for the class.
76
80
  # @yieldparam [Object] obj The object to format.
77
81
  # @yieldreturn [String] The formatted string.
@@ -129,7 +133,7 @@ module Lumberjack
129
133
  # help avoid loading dependency issues. This applies only to classes; modules cannot be
130
134
  # passed in as strings.
131
135
  #
132
- # @param [Class, Module, String, Array<Class, Module, String>] klass The class or module to remove the formatters for.
136
+ # @param klass [Class, Module, String, Array<Class, Module, String>] The class or module to remove the formatters for.
133
137
  # @return [self] Returns itself so that remove statements can be chained together.
134
138
  def remove(klass)
135
139
  Array(klass).each do |k|
@@ -152,9 +156,16 @@ module Lumberjack
152
156
  self
153
157
  end
154
158
 
159
+ # Return true if their are no registered formatters.
160
+ #
161
+ # @return [Boolean] true if there are no registered formatters, false otherwise.
162
+ def empty?
163
+ @class_formatters.empty? && @module_formatters.empty?
164
+ end
165
+
155
166
  # Format a message object by applying all formatters attached to it.
156
167
  #
157
- # @param [Object] message The message object to format.
168
+ # @param message [Object] The message object to format.
158
169
  # @return [Object] The formatted object.
159
170
  def format(message)
160
171
  formatter = formatter_for(message.class)
@@ -168,18 +179,22 @@ module Lumberjack
168
179
  # Compatibility with the Logger::Formatter signature. This method will just convert the message
169
180
  # object to a string and ignores the other parameters.
170
181
  #
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.
182
+ # @param severity [Integer, String, Symbol] The severity of the message.
183
+ # @param timestamp [Time] The time the message was logged.
184
+ # @param progname [String] The name of the program logging the message.
185
+ # @param msg [Object] The message object to format.
175
186
  def call(severity, timestamp, progname, msg)
176
- "#{format(msg)}#{Lumberjack::LINE_SEPARATOR}"
187
+ formatted_message = format(msg)
188
+ formatted_message = formatted_message.message if formatted_message.is_a?(TaggedMessage)
189
+ "#{formatted_message}#{Lumberjack::LINE_SEPARATOR}"
177
190
  end
178
191
 
179
- private
180
-
181
192
  # Find the formatter for a class by looking it up using the class hierarchy.
182
- def formatter_for(klass) # :nodoc:
193
+ #
194
+ # @api private
195
+ def formatter_for(klass)
196
+ return nil if empty?
197
+
183
198
  check_modules = true
184
199
  until klass.nil?
185
200
  formatter = @class_formatters[klass.name]
@@ -8,16 +8,17 @@ module Lumberjack
8
8
 
9
9
  TIME_FORMAT = "%Y-%m-%dT%H:%M:%S"
10
10
 
11
+ # @deprecated Will be removed in version 2.0.
11
12
  UNIT_OF_WORK_ID = "unit_of_work_id"
12
13
 
13
14
  # Create a new log entry.
14
15
  #
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.
16
+ # @param time [Time] The time the log entry was created.
17
+ # @param severity [Integer, String] The severity of the log entry.
18
+ # @param message [String] The message to log.
19
+ # @param progname [String] The name of the program that created the log entry.
20
+ # @param pid [Integer] The process id of the program that created the log entry.
21
+ # @param tags [Hash<String, Object>] A hash of tags to associate with the log entry.
21
22
  def initialize(time, severity, message, progname, pid, tags)
22
23
  @time = time
23
24
  @severity = (severity.is_a?(Integer) ? severity : Severity.label_to_level(severity))
@@ -25,9 +26,9 @@ module Lumberjack
25
26
  @progname = progname
26
27
  @pid = pid
27
28
  # backward compatibility with 1.0 API where the last argument was the unit of work id
28
- @tags = if tags.nil? || tags.is_a?(Hash)
29
- tags
30
- else
29
+ @tags = if tags.is_a?(Hash)
30
+ compact_tags(tags)
31
+ elsif !tags.nil?
31
32
  {UNIT_OF_WORK_ID => tags}
32
33
  end
33
34
  end
@@ -44,23 +45,49 @@ module Lumberjack
44
45
  to_s
45
46
  end
46
47
 
47
- # Deprecated - backward compatibility with 1.0 API
48
+ # @deprecated - backward compatibility with 1.0 API. Will be removed in version 2.0.
48
49
  def unit_of_work_id
49
- tags[UNIT_OF_WORK_ID] if tags
50
+ Lumberjack::Utils.deprecated("Lumberjack::LogEntry#unit_of_work_id", "Lumberjack::LogEntry#unit_of_work_id will be removed in version 2.0") do
51
+ tags[UNIT_OF_WORK_ID] if tags
52
+ end
50
53
  end
51
54
 
52
- # Deprecated - backward compatibility with 1.0 API
55
+ # @deprecated - backward compatibility with 1.0 API. Will be removed in version 2.0.
53
56
  def unit_of_work_id=(value)
54
- if tags
55
- tags[UNIT_OF_WORK_ID] = value
56
- else
57
- @tags = {UNIT_OF_WORK_ID => value}
57
+ Lumberjack::Utils.deprecated("Lumberjack::LogEntry#unit_of_work_id=", "Lumberjack::LogEntry#unit_of_work_id= will be removed in version 2.0") do
58
+ if tags
59
+ tags[UNIT_OF_WORK_ID] = value
60
+ else
61
+ @tags = {UNIT_OF_WORK_ID => value}
62
+ end
58
63
  end
59
64
  end
60
65
 
61
66
  # Return the tag with the specified name.
67
+ #
68
+ # @param name [String, Symbol] The tag name.
69
+ # @return [Object, nil] The tag value or nil if the tag does not exist.
62
70
  def tag(name)
63
- tags[name.to_s] if tags
71
+ TagContext.new(tags)[name]
72
+ end
73
+
74
+ # Helper method to expand the tags into a nested structure. Tags with dots in the name
75
+ # will be expanded into nested hashes.
76
+ #
77
+ # @return [Hash] The tags expanded into a nested structure.
78
+ #
79
+ # @example
80
+ # entry = Lumberjack::LogEntry.new(Time.now, Logger::INFO, "test", "app", 1500, "a.b.c" => 1, "a.b.d" => 2)
81
+ # entry.nested_tags # => {"a" => {"b" => {"c" => 1, "d" => 2}}}
82
+ def nested_tags
83
+ Utils.expand_tags(tags)
84
+ end
85
+
86
+ # Return true if the log entry has no message and no tags.
87
+ #
88
+ # @return [Boolean] True if the log entry is empty, false otherwise.
89
+ def empty?
90
+ (message.nil? || message == "") && (tags.nil? || tags.empty?)
64
91
  end
65
92
 
66
93
  private
@@ -70,5 +97,37 @@ module Lumberjack
70
97
  tags&.each { |name, value| tags_string << " #{name}:#{value.inspect}" }
71
98
  tags_string
72
99
  end
100
+
101
+ def compact_tags(tags)
102
+ delete_keys = nil
103
+ compacted_keys = nil
104
+
105
+ tags.each do |key, value|
106
+ if value.nil? || value == ""
107
+ delete_keys ||= []
108
+ delete_keys << key
109
+ elsif value.is_a?(Hash)
110
+ compacted_value = compact_tags(value)
111
+ if compacted_value.empty?
112
+ delete_keys ||= []
113
+ delete_keys << key
114
+ elsif !value.equal?(compacted_value)
115
+ compacted_keys ||= []
116
+ compacted_keys << [key, compacted_value]
117
+ end
118
+ elsif value.is_a?(Array) && value.empty?
119
+ delete_keys ||= []
120
+ delete_keys << key
121
+ end
122
+ end
123
+
124
+ return tags if delete_keys.nil? && compacted_keys.nil?
125
+
126
+ tags = tags.dup
127
+ delete_keys&.each { |key| tags.delete(key) }
128
+ compacted_keys&.each { |key, value| tags[key] = value }
129
+
130
+ tags
131
+ end
73
132
  end
74
133
  end
@@ -21,7 +21,7 @@ module Lumberjack
21
21
  # monitoring thread, but its use is highly recommended.
22
22
  #
23
23
  # Each log entry records the log message and severity along with the time it was logged, the
24
- # program name, process id, and unit of work id. The message will be converted to a string, but
24
+ # program name, process id, and an optional hash of tags. The message will be converted to a string, but
25
25
  # otherwise, it is up to the device how these values are recorded. Messages are converted to strings
26
26
  # using a Formatter associated with the logger.
27
27
  class Logger
@@ -36,8 +36,8 @@ module Lumberjack
36
36
  # Set the name of the program to attach to log entries.
37
37
  attr_writer :progname
38
38
 
39
- # The device being written to
40
- attr_accessor :device
39
+ # The Formatter used only for log entry messages.
40
+ attr_accessor :message_formatter
41
41
 
42
42
  # The TagFormatter used for formatting tags for output
43
43
  attr_accessor :tag_formatter
@@ -51,31 +51,30 @@ module Lumberjack
51
51
  # If it is :null, it will be a Null device that won't record any output.
52
52
  # Otherwise, it will be assumed to be file path and wrapped in a Device::LogFile class.
53
53
  #
54
- # This method can take the following options:
55
- #
56
- # * :level - The logging level below which messages will be ignored.
57
- # * :formatter - The formatter to use for outputting messages to the log.
58
- # * :datetime_format - The format to use for log timestamps.
59
- # * :tag_formatter - The TagFormatter to use for formatting tags.
60
- # * :progname - The name of the program that will be recorded with each log entry.
61
- # * :flush_seconds - The maximum number of seconds between flush calls.
62
- # * :roll - If the log device is a file path, it will be a Device::DateRollingLogFile if this is set.
63
- # * :max_size - If the log device is a file path, it will be a Device::SizeRollingLogFile if this is set.
64
- #
65
54
  # All other options are passed to the device constuctor.
66
55
  #
67
56
  # @param [Lumberjack::Device, Object, Symbol, String] device The device to log to.
68
57
  # @param [Hash] options The options for the logger.
58
+ # @option options [Integer, Symbol, String] :level The logging level below which messages will be ignored.
59
+ # @option options [Lumberjack::Formatter] :formatter The formatter to use for outputting messages to the log.
60
+ # @option options [String] :datetime_format The format to use for log timestamps.
61
+ # @option options [Lumberjack::Formatter] :message_formatter The MessageFormatter to use for formatting log messages.
62
+ # @option options [Lumberjack::TagFormatter] :tag_formatter The TagFormatter to use for formatting tags.
63
+ # @option options [String] :progname The name of the program that will be recorded with each log entry.
64
+ # @option options [Numeric] :flush_seconds The maximum number of seconds between flush calls.
65
+ # @option options [Boolean] :roll If the log device is a file path, it will be a Device::DateRollingLogFile if this is set.
66
+ # @option options [Integer] :max_size If the log device is a file path, it will be a Device::SizeRollingLogFile if this is set.
69
67
  def initialize(device = $stdout, options = {})
70
68
  options = options.dup
71
69
  self.level = options.delete(:level) || INFO
72
70
  self.progname = options.delete(:progname)
73
71
  max_flush_seconds = options.delete(:flush_seconds).to_f
74
72
 
75
- @device = open_device(device, options) if device
73
+ @logdev = open_device(device, options) if device
76
74
  self.formatter = (options[:formatter] || Formatter.new)
77
- @tag_formatter = (options[:tag_formatter] || TagFormatter.new)
78
- time_format = (options[:datetime_format] || options[:time_format])
75
+ @message_formatter = options[:message_formatter] || Formatter.empty
76
+ @tag_formatter = options[:tag_formatter] || TagFormatter.new
77
+ time_format = options[:datetime_format] || options[:time_format]
79
78
  self.datetime_format = time_format if time_format
80
79
  @last_flushed_at = Time.now
81
80
  @silencer = true
@@ -85,6 +84,21 @@ module Lumberjack
85
84
  create_flusher_thread(max_flush_seconds) if max_flush_seconds > 0
86
85
  end
87
86
 
87
+ # Get the logging device that is used to write log entries.
88
+ #
89
+ # @return [Lumberjack::Device] The logging device.
90
+ def device
91
+ @logdev
92
+ end
93
+
94
+ # Set the logging device to a new device.
95
+ #
96
+ # @param [Lumberjack::Device] device The new logging device.
97
+ # @return [void]
98
+ def device=(device)
99
+ @logdev = device.nil? ? nil : open_device(device, {})
100
+ end
101
+
88
102
  # Get the timestamp format on the device if it has one.
89
103
  #
90
104
  # @return [String, nil] The timestamp format or nil if the device doesn't support it.
@@ -186,33 +200,39 @@ module Lumberjack
186
200
  # logger.add_entry(:warn, "Request took a long time")
187
201
  # logger.add_entry(Logger::DEBUG){"Start processing with options #{options.inspect}"}
188
202
  def add_entry(severity, message, progname = nil, tags = nil)
189
- begin
190
- severity = Severity.label_to_level(severity) unless severity.is_a?(Integer)
191
- return true unless device && severity && severity >= level
203
+ severity = Severity.label_to_level(severity) unless severity.is_a?(Integer)
204
+ return true unless device && severity && severity >= level
205
+ return true if Thread.current[:lumberjack_logging]
192
206
 
193
- return true if Thread.current[:lumberjack_logging]
194
- Thread.current[:lumberjack_logging] = true
207
+ begin
208
+ Thread.current[:lumberjack_logging] = true # Prevent circular calls to add_entry
195
209
 
196
210
  time = Time.now
211
+
197
212
  message = message.call if message.is_a?(Proc)
198
- message = formatter.format(message)
213
+ msg_class_formatter = message_formatter&.formatter_for(message.class)
214
+ if msg_class_formatter
215
+ message = msg_class_formatter.call(message)
216
+ elsif formatter
217
+ message = formatter.format(message)
218
+ end
219
+ message_tags = nil
220
+ if message.is_a?(Formatter::TaggedMessage)
221
+ message_tags = message.tags
222
+ message = message.message
223
+ end
224
+
199
225
  progname ||= self.progname
226
+ message_tags = Utils.flatten_tags(message_tags) if message_tags
200
227
 
201
228
  current_tags = self.tags
202
229
  tags = nil unless tags.is_a?(Hash)
203
- if current_tags.empty?
204
- tags = Tags.stringify_keys(tags) unless tags.nil?
205
- else
206
- tags = if tags.nil?
207
- current_tags.dup
208
- else
209
- current_tags.merge(Tags.stringify_keys(tags))
210
- end
211
- end
230
+ tags = merge_tags(current_tags, tags)
231
+ tags = merge_tags(tags, message_tags) if message_tags
212
232
  tags = Tags.expand_runtime_values(tags)
213
233
  tags = tag_formatter.format(tags) if tag_formatter
214
234
 
215
- entry = LogEntry.new(time, severity, message, progname, $$, tags)
235
+ entry = LogEntry.new(time, severity, message, progname, Process.pid, tags)
216
236
  write_to_device(entry)
217
237
  ensure
218
238
  Thread.current[:lumberjack_logging] = nil
@@ -361,7 +381,7 @@ module Lumberjack
361
381
 
362
382
  # Return +true+ if +INFO+ messages are being logged.
363
383
  #
364
-
384
+ # @return [Boolean]
365
385
  def info?
366
386
  level <= INFO
367
387
  end
@@ -441,6 +461,14 @@ module Lumberjack
441
461
  end
442
462
  end
443
463
 
464
+ # Provided for compatibility with ActiveSupport::LoggerThreadSafeLevel to temporarily set the log level.
465
+ #
466
+ # @param [Integer, String, Symbol] level The log level to use inside the block.
467
+ # @return [Object] The result of the block.
468
+ def log_at(level, &block)
469
+ with_level(level, &block)
470
+ end
471
+
444
472
  # Set the program name that is associated with log messages. If a block
445
473
  # is given, the program name will be valid only within the block.
446
474
  #
@@ -454,6 +482,15 @@ module Lumberjack
454
482
  end
455
483
  end
456
484
 
485
+ # Set the logger progname for the duration of the block.
486
+ #
487
+ # @yield [Object] The block to execute with the program name set.
488
+ # @param [String] value The program name to use.
489
+ # @return [Object] The result of the block.
490
+ def with_progname(value, &block)
491
+ set_progname(value, &block)
492
+ end
493
+
457
494
  # Get the program name associated with log messages.
458
495
  #
459
496
  # @return [String]
@@ -462,41 +499,68 @@ module Lumberjack
462
499
  end
463
500
 
464
501
  # Set a hash of tags on logger. If a block is given, the tags will only be set
465
- # for the duration of the block. If this method is called inside such a block,
466
- # the tags will only be defined on the tags in that block. When the parent block
467
- # exits, all the tags will be reverted. If there is no block, then the tags will
468
- # be defined as global and apply to all log statements.
502
+ # for the duration of the block. Otherwise the tags will be applied on the current
503
+ # logger context for the duration of that context.
504
+ #
505
+ # If there is no block or context, the tags will be applied to the global context.
506
+ # This behavior is deprecated. Use the `tag_globally` method to set global tags instead.
469
507
  #
470
508
  # @param [Hash] tags The tags to set.
471
509
  # @return [void]
472
510
  def tag(tags, &block)
473
- tags = Tags.stringify_keys(tags)
474
511
  thread_tags = thread_local_value(:lumberjack_logger_tags)
475
512
  if block
476
- merged_tags = (thread_tags ? thread_tags.merge(tags) : tags.dup)
513
+ merged_tags = (thread_tags ? thread_tags.dup : {})
514
+ TagContext.new(merged_tags).tag(tags)
477
515
  push_thread_local_value(:lumberjack_logger_tags, merged_tags, &block)
478
516
  elsif thread_tags
479
- thread_tags.merge!(tags)
517
+ TagContext.new(thread_tags).tag(tags)
480
518
  nil
481
519
  else
482
- @tags.merge!(tags)
483
- nil
520
+ Utils.deprecated("Lumberjack::Logger#tag", "Lumberjack::Logger#tag must be called with a block or inside a context block. In version 2.0 it will no longer be used for setting global tags. Use Lumberjack::Logger#tag_globally instead.") do
521
+ tag_globally(tags)
522
+ end
484
523
  end
485
524
  end
486
525
 
487
- # Remove a tag from the current tag context. If this is called inside a block to a
488
- # call to `tag`, the tags will only be removed for the duration of that block. Otherwise
489
- # they will be removed from the global tags.
526
+ # Set up a context block for the logger. All tags added within the block will be cleared when
527
+ # the block exits.
528
+ #
529
+ # @param [Proc] block The block to execute with the tag context.
530
+ # @return [TagContext] If no block is passed, then a Lumberjack::TagContext is returned that can be used
531
+ # to interact with the tags (add, remove, etc.).
532
+ # @yield [TagContext] If a block is passed, it will be yielded a TagContext object that can be used to
533
+ # add or remove tags within the context.
534
+ def context(&block)
535
+ if block
536
+ thread_tags = thread_local_value(:lumberjack_logger_tags)&.dup
537
+ thread_tags ||= {}
538
+ push_thread_local_value(:lumberjack_logger_tags, thread_tags) do
539
+ block.call(TagContext.new(thread_tags))
540
+ end
541
+ else
542
+ TagContext.new(thread_local_value(:lumberjack_logger_tags) || {})
543
+ end
544
+ end
545
+
546
+ # Add global tags to the logger that will appear on all log entries.
547
+ #
548
+ # @param [Hash] tags The tags to set.
549
+ # @return [void]
550
+ def tag_globally(tags)
551
+ TagContext.new(@tags).tag(tags)
552
+ nil
553
+ end
554
+
555
+ # Remove a tag from the current tag context. If this is called inside a tag context,
556
+ # the tags will only be removed for the duration of that block. Otherwise they will be removed
557
+ # from the global tags.
490
558
  #
491
559
  # @param [Array<String, Symbol>] tag_names The tags to remove.
492
560
  # @return [void]
493
561
  def remove_tag(*tag_names)
494
- thread_tags = thread_local_value(:lumberjack_logger_tags)
495
- if thread_tags
496
- tag_names.each { |name| thread_tags.delete(name.to_s) }
497
- else
498
- tag_names.each { |name| @tags.delete(name.to_s) }
499
- end
562
+ tags = thread_local_value(:lumberjack_logger_tags) || @tags
563
+ TagContext.new(tags).delete(*tag_names)
500
564
  end
501
565
 
502
566
  # Return all tags in scope on the logger including global tags set on the Lumberjack
@@ -513,6 +577,15 @@ module Lumberjack
513
577
  tags
514
578
  end
515
579
 
580
+ # Get the value of a tag by name from the current tag context.
581
+ #
582
+ # @param [String, Symbol] name The name of the tag to get.
583
+ # @return [Object, nil] The value of the tag or nil if the tag does not exist.
584
+ def tag_value(name)
585
+ name = name.join(".") if name.is_a?(Array)
586
+ TagContext.new(tags)[name]
587
+ end
588
+
516
589
  # Remove all tags on the current logger and logging context within a block.
517
590
  # You can still set new block scoped tags within theuntagged block and provide
518
591
  # tags on individual log methods.
@@ -533,6 +606,14 @@ module Lumberjack
533
606
  end
534
607
  end
535
608
 
609
+ # Return true if the thread is currently in a Lumberjack::Context block.
610
+ # When the logger is in a context block, tagging will only apply to that block.
611
+ #
612
+ # @return [Boolean]
613
+ def in_tag_context?
614
+ !!thread_local_value(:lumberjack_logger_tags)
615
+ end
616
+
536
617
  private
537
618
 
538
619
  # Dereference arguments to log calls so we can have methods with compatibility with ::Logger
@@ -560,6 +641,17 @@ module Lumberjack
560
641
  add_entry(severity, message, progname, tags)
561
642
  end
562
643
 
644
+ # Merge a tags hash into an existing tags hash.
645
+ def merge_tags(current_tags, tags)
646
+ if current_tags.nil? || current_tags.empty?
647
+ tags
648
+ elsif tags.nil?
649
+ current_tags
650
+ else
651
+ current_tags.merge(tags)
652
+ end
653
+ end
654
+
563
655
  # Set a local value for a thread tied to this object.
564
656
  def set_thread_local_value(name, value) # :nodoc:
565
657
  values = Thread.current[name]
@@ -3,16 +3,35 @@
3
3
  module Lumberjack
4
4
  module Rack
5
5
  # Middleware to create a global context for Lumberjack for the scope of a rack request.
6
+ #
7
+ # The optional `env_tags` parameter can be used to set up global tags from the request
8
+ # environment. This is useful for setting tags that are relevant to the entire request
9
+ # like the request id, host, etc.
6
10
  class Context
7
- def initialize(app)
11
+ # @param [Object] app The rack application.
12
+ # @param [Hash] env_tags A hash of tags to set from the request environment. If a tag value is
13
+ # a Proc, it will be called with the request `env` as an argument to allow dynamic tag values
14
+ # based on request data.
15
+ def initialize(app, env_tags = nil)
8
16
  @app = app
17
+ @env_tags = env_tags
9
18
  end
10
19
 
11
20
  def call(env)
12
21
  Lumberjack.context do
22
+ apply_tags(env) if @env_tags
13
23
  @app.call(env)
14
24
  end
15
25
  end
26
+
27
+ private
28
+
29
+ def apply_tags(env)
30
+ tags = @env_tags.transform_values do |value|
31
+ value.is_a?(Proc) ? value.call(env) : value
32
+ end
33
+ Lumberjack.tag(tags)
34
+ end
16
35
  end
17
36
  end
18
37
  end
@@ -5,12 +5,16 @@ module Lumberjack
5
5
  # Support for using the Rails ActionDispatch request id in the log.
6
6
  # The format is expected to be a random UUID and only the first chunk is used for terseness
7
7
  # if the abbreviated argument is true.
8
+ #
9
+ # @deprecated Use tags instead of request id for unit of work. Will be removed in version 2.0.
8
10
  class RequestId
9
11
  REQUEST_ID = "action_dispatch.request_id"
10
12
 
11
13
  def initialize(app, abbreviated = false)
12
- @app = app
13
- @abbreviated = abbreviated
14
+ Lumberjack::Utils.deprecated("Lumberjack::Rack::RequestId", "Lumberjack::Rack::RequestId will be removed in version 2.0") do
15
+ @app = app
16
+ @abbreviated = abbreviated
17
+ end
14
18
  end
15
19
 
16
20
  def call(env)
@@ -2,9 +2,13 @@
2
2
 
3
3
  module Lumberjack
4
4
  module Rack
5
+ # @deprecated Use the Lumberjack::Rack::Context middleware instead to set a global tag
6
+ # with an identifier to tie log entries together in a unit of work. Will be removed in version 2.0.
5
7
  class UnitOfWork
6
8
  def initialize(app)
7
- @app = app
9
+ Lumberjack::Utils.deprecated("Lumberjack::Rack::UnitOfWork", "Lumberjack::Rack::UnitOfWork will be removed in version 2.0") do
10
+ @app = app
11
+ end
8
12
  end
9
13
 
10
14
  def call(env)