lumberjack 1.4.2 → 2.0.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.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/ARCHITECTURE.md +524 -176
  3. data/CHANGELOG.md +89 -0
  4. data/README.md +604 -211
  5. data/UPGRADE_GUIDE.md +80 -0
  6. data/VERSION +1 -1
  7. data/lib/lumberjack/attribute_formatter.rb +451 -0
  8. data/lib/lumberjack/attributes_helper.rb +100 -0
  9. data/lib/lumberjack/context.rb +120 -23
  10. data/lib/lumberjack/context_logger.rb +620 -0
  11. data/lib/lumberjack/device/buffer.rb +209 -0
  12. data/lib/lumberjack/device/date_rolling_log_file.rb +10 -62
  13. data/lib/lumberjack/device/log_file.rb +76 -29
  14. data/lib/lumberjack/device/logger_wrapper.rb +137 -0
  15. data/lib/lumberjack/device/multi.rb +92 -30
  16. data/lib/lumberjack/device/null.rb +26 -8
  17. data/lib/lumberjack/device/size_rolling_log_file.rb +13 -54
  18. data/lib/lumberjack/device/test.rb +337 -0
  19. data/lib/lumberjack/device/writer.rb +184 -176
  20. data/lib/lumberjack/device.rb +134 -15
  21. data/lib/lumberjack/device_registry.rb +90 -0
  22. data/lib/lumberjack/entry_formatter.rb +357 -0
  23. data/lib/lumberjack/fiber_locals.rb +55 -0
  24. data/lib/lumberjack/forked_logger.rb +143 -0
  25. data/lib/lumberjack/formatter/date_time_formatter.rb +14 -3
  26. data/lib/lumberjack/formatter/exception_formatter.rb +12 -2
  27. data/lib/lumberjack/formatter/id_formatter.rb +13 -1
  28. data/lib/lumberjack/formatter/inspect_formatter.rb +14 -1
  29. data/lib/lumberjack/formatter/multiply_formatter.rb +10 -0
  30. data/lib/lumberjack/formatter/object_formatter.rb +13 -1
  31. data/lib/lumberjack/formatter/pretty_print_formatter.rb +15 -2
  32. data/lib/lumberjack/formatter/redact_formatter.rb +18 -3
  33. data/lib/lumberjack/formatter/round_formatter.rb +12 -0
  34. data/lib/lumberjack/formatter/string_formatter.rb +9 -1
  35. data/lib/lumberjack/formatter/strip_formatter.rb +13 -1
  36. data/lib/lumberjack/formatter/structured_formatter.rb +18 -2
  37. data/lib/lumberjack/formatter/tagged_message.rb +10 -32
  38. data/lib/lumberjack/formatter/tags_formatter.rb +32 -0
  39. data/lib/lumberjack/formatter/truncate_formatter.rb +8 -1
  40. data/lib/lumberjack/formatter.rb +271 -141
  41. data/lib/lumberjack/formatter_registry.rb +84 -0
  42. data/lib/lumberjack/io_compatibility.rb +133 -0
  43. data/lib/lumberjack/local_log_template.rb +209 -0
  44. data/lib/lumberjack/log_entry.rb +154 -79
  45. data/lib/lumberjack/log_entry_matcher/score.rb +276 -0
  46. data/lib/lumberjack/log_entry_matcher.rb +126 -0
  47. data/lib/lumberjack/logger.rb +328 -556
  48. data/lib/lumberjack/message_attributes.rb +38 -0
  49. data/lib/lumberjack/rack/context.rb +66 -15
  50. data/lib/lumberjack/rack.rb +0 -2
  51. data/lib/lumberjack/remap_attribute.rb +24 -0
  52. data/lib/lumberjack/severity.rb +52 -15
  53. data/lib/lumberjack/tag_context.rb +8 -71
  54. data/lib/lumberjack/tag_formatter.rb +22 -188
  55. data/lib/lumberjack/tags.rb +15 -21
  56. data/lib/lumberjack/template.rb +252 -62
  57. data/lib/lumberjack/template_registry.rb +60 -0
  58. data/lib/lumberjack/utils.rb +198 -48
  59. data/lib/lumberjack.rb +167 -59
  60. data/lumberjack.gemspec +4 -2
  61. metadata +41 -15
  62. data/lib/lumberjack/device/rolling_log_file.rb +0 -145
  63. data/lib/lumberjack/rack/request_id.rb +0 -31
  64. data/lib/lumberjack/rack/unit_of_work.rb +0 -21
  65. data/lib/lumberjack/tagged_logger_support.rb +0 -81
  66. data/lib/lumberjack/tagged_logging.rb +0 -29
@@ -1,55 +1,152 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Lumberjack
4
- # A context is used to store tags that are then added to all log entries within a block.
4
+ # Context stores logging settings and attributes that can be scoped to specific code blocks
5
+ # or inherited between loggers. It provides a hierarchical system for managing logging state
6
+ # including level, progname, default severity, and custom attributes.
7
+ #
8
+ # Child contexts inherit all configuration from their parent but can override any values.
9
+ # Changes to child contexts don't affect parent contexts, providing true isolation.
10
+ #
11
+ # @see Lumberjack::ContextLogger
12
+ # @see Lumberjack::AttributesHelper
5
13
  class Context
6
- attr_reader :tags
14
+ # The attributes hash containing key-value pairs to include in log entries.
15
+ # @return [Hash, nil] The attributes hash, or nil if no attributes are set.
16
+ attr_reader :attributes
7
17
 
8
- # @param parent_context [Context] A parent context to inherit tags from.
18
+ # The logging level for this context.
19
+ # @return [Integer, nil] The logging level, or nil if not set (inherits from parent or default).
20
+ attr_reader :level
21
+
22
+ # The program name for this context.
23
+ # @return [String, nil] The program name, or nil if not set (inherits from parent or default).
24
+ attr_reader :progname
25
+
26
+ # The default severity used when writing log messages directly to a stream.
27
+ # @return [Integer, nil] The default severity level, or nil if not set.
28
+ attr_reader :default_severity
29
+
30
+ # The parent context from which this context inherited its initial attributes.
31
+ # @return [Lumberjack::Context, nil] The parent context, or nil if this is a top-level context.
32
+ # @api private
33
+ attr_accessor :parent
34
+
35
+ # Create a new context, optionally inheriting configuration from a parent context.
36
+ #
37
+ # When a parent context is provided, the new context inherits all configuration
38
+ # (level, progname, default_severity) and a copy of all attributes. Changes to the
39
+ # new context won't affect the parent context, providing true isolation.
40
+ #
41
+ # @param parent_context [Lumberjack::Context, nil] The parent context to inherit from.
9
42
  def initialize(parent_context = nil)
10
- @tags = {}
11
- @tags.merge!(parent_context.tags) if parent_context
12
- @tag_context = TagContext.new(@tags)
43
+ @attributes = nil
44
+ @level = nil
45
+ @progname = nil
46
+ @default_severity = nil
47
+
48
+ if parent_context
49
+ @attributes = parent_context.attributes.dup if parent_context.attributes
50
+ self.level = parent_context.level
51
+ self.progname = parent_context.progname
52
+ end
53
+ end
54
+
55
+ # Set the logging level for this context. The level determines which log entries
56
+ # will be processed when this context is active.
57
+ #
58
+ # @param value [Integer, Symbol, String, nil] The logging level. Can be a numeric level,
59
+ # symbol (:debug, :info, :warn, :error, :fatal), string, or nil to unset.
60
+ # @return [void]
61
+ def level=(value)
62
+ value = Severity.coerce(value) unless value.nil?
63
+ @level = value
64
+ end
65
+
66
+ # Set the program name for this context. The progname identifies the component
67
+ # or program that is generating log entries.
68
+ #
69
+ # @param value [String, Symbol, nil] The program name. Will be converted to a frozen string.
70
+ # @return [void]
71
+ def progname=(value)
72
+ @progname = value&.to_s&.freeze
13
73
  end
14
74
 
15
- # Set tags on the context.
75
+ # Assign multiple attributes to this context from a hash. This method allows
76
+ # bulk assignment of context attributes and supports nested attribute names
77
+ # using dot notation.
16
78
  #
17
- # @param tags [Hash] The tags to set.
79
+ # @param attributes [Hash] A hash of attribute names to values. Keys can be strings
80
+ # or symbols, and support dot notation for nested attributes.
18
81
  # @return [void]
19
- def tag(tags)
20
- @tag_context.tag(tags)
82
+ # @see #[]= for setting individual attributes
83
+ def assign_attributes(attributes)
84
+ attributes_helper.update(attributes)
21
85
  end
22
86
 
23
- # Get a context tag.
87
+ # Get a context attribute by key. Supports both string and symbol keys,
88
+ # and can access nested attributes using dot notation.
24
89
  #
25
- # @param key [String, Symbol] The tag key.
26
- # @return [Object] The tag value.
90
+ # @param key [String, Symbol] The attribute key. Supports dot notation for nested access.
91
+ # @return [Object] The attribute value, or nil if the key doesn't exist.
27
92
  def [](key)
28
- @tag_context[key]
93
+ attributes_helper[key]
29
94
  end
30
95
 
31
- # Set a context tag.
96
+ # Set a context attribute by key. Supports both string and symbol keys,
97
+ # and can set nested attributes using dot notation.
32
98
  #
33
- # @param key [String, Symbol] The tag key.
34
- # @param value [Object] The tag value.
99
+ # @param key [String, Symbol] The attribute key. Supports dot notation for nested assignment.
100
+ # @param value [Object] The attribute value to set.
35
101
  # @return [void]
36
102
  def []=(key, value)
37
- @tag_context[key] = value
103
+ attributes_helper[key] = value
38
104
  end
39
105
 
40
- # Remove tags from the context.
106
+ # Remove all attributes from this context. This only affects attributes
107
+ # directly set on this context, not those inherited from parent contexts.
108
+ def clear_attributes
109
+ @attributes&.clear
110
+ end
111
+
112
+ # Remove specific attributes from this context. This only affects attributes
113
+ # directly set on this context, not those inherited from parent contexts.
114
+ # Supports dot notation for nested attribute removal.
41
115
  #
42
- # @param keys [Array<String, Symbol>] The tag keys to remove.
116
+ # @param keys [Array<String, Symbol>] The attribute keys to remove. Can use
117
+ # dot notation for nested attributes.
43
118
  # @return [void]
44
119
  def delete(*keys)
45
- @tag_context.delete(*keys)
120
+ attributes_helper.delete(*keys)
121
+ end
122
+
123
+ # Set the default severity level for this context. This determines the minimum
124
+ # severity level for log entries when no explicit level is specified.
125
+ #
126
+ # @param value [Integer, Symbol, String, nil] The default severity level. Can be a numeric level,
127
+ # symbol (:debug, :info, :warn, :error, :fatal), string, or nil to unset.
128
+ # @return [void]
129
+ def default_severity=(value)
130
+ value = Severity.coerce(value) unless value.nil?
131
+ @default_severity = value
46
132
  end
47
133
 
48
- # Clear all the context data.
134
+ # Clear all context data including attributes, level, and progname.
135
+ # This resets the context to its initial state while preserving the
136
+ # parent context relationship.
49
137
  #
50
138
  # @return [void]
51
139
  def reset
52
- @tags.clear
140
+ @attributes&.clear
141
+ @level = nil
142
+ @progname = nil
143
+ end
144
+
145
+ private
146
+
147
+ def attributes_helper
148
+ @attributes ||= {}
149
+ AttributesHelper.new(@attributes)
53
150
  end
54
151
  end
55
152
  end