Class: Lumberjack::Context
- Inherits:
-
Object
- Object
- Lumberjack::Context
- Defined in:
- lib/lumberjack/context.rb
Overview
Context stores logging settings and attributes that can be scoped to specific code blocks or inherited between loggers. It provides a hierarchical system for managing logging state including level, progname, default severity, and custom attributes.
Child contexts inherit all configuration from their parent but can override any values. Changes to child contexts don't affect parent contexts, providing true isolation.
Instance Attribute Summary collapse
-
#attributes ⇒ Hash?
readonly
The attributes hash containing key-value pairs to include in log entries.
-
#default_severity ⇒ Integer?
The default severity used when writing log messages directly to a stream.
-
#level ⇒ Integer?
The logging level for this context.
-
#parent ⇒ Lumberjack::Context?
private
The parent context from which this context inherited its initial attributes.
-
#progname ⇒ String?
The program name for this context.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Get a context attribute by key.
-
#[]=(key, value) ⇒ void
Set a context attribute by key.
-
#assign_attributes(attributes) ⇒ void
Assign multiple attributes to this context from a hash.
-
#clear_attributes ⇒ Object
Remove all attributes from this context.
-
#delete(*keys) ⇒ void
Remove specific attributes from this context.
-
#initialize(parent_context = nil) ⇒ Context
constructor
Create a new context, optionally inheriting configuration from a parent context.
-
#reset ⇒ void
Clear all context data including attributes, level, and progname.
Constructor Details
#initialize(parent_context = nil) ⇒ Context
Create a new context, optionally inheriting configuration from a parent context.
When a parent context is provided, the new context inherits all configuration (level, progname, default_severity) and a copy of all attributes. Changes to the new context won't affect the parent context, providing true isolation.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/lumberjack/context.rb', line 42 def initialize(parent_context = nil) @attributes = nil @level = nil @progname = nil @default_severity = nil if parent_context if parent_context.attributes # Dup array values so appending tags in this context does not mutate the parent. @attributes = parent_context.attributes.transform_values do |value| value.is_a?(Array) ? value.dup : value end end self.level = parent_context.level self.progname = parent_context.progname end end |
Instance Attribute Details
#attributes ⇒ Hash? (readonly)
The attributes hash containing key-value pairs to include in log entries.
16 17 18 |
# File 'lib/lumberjack/context.rb', line 16 def attributes @attributes end |
#default_severity ⇒ Integer?
The default severity used when writing log messages directly to a stream.
28 29 30 |
# File 'lib/lumberjack/context.rb', line 28 def default_severity @default_severity end |
#level ⇒ Integer?
The logging level for this context.
20 21 22 |
# File 'lib/lumberjack/context.rb', line 20 def level @level end |
#parent ⇒ Lumberjack::Context?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The parent context from which this context inherited its initial attributes.
33 34 35 |
# File 'lib/lumberjack/context.rb', line 33 def parent @parent end |
#progname ⇒ String?
The program name for this context.
24 25 26 |
# File 'lib/lumberjack/context.rb', line 24 def progname @progname end |
Instance Method Details
#[](key) ⇒ Object
Get a context attribute by key. Supports both string and symbol keys, and can access nested attributes using dot notation.
97 98 99 |
# File 'lib/lumberjack/context.rb', line 97 def [](key) attributes_helper[key] end |
#[]=(key, value) ⇒ void
This method returns an undefined value.
Set a context attribute by key. Supports both string and symbol keys, and can set nested attributes using dot notation.
107 108 109 |
# File 'lib/lumberjack/context.rb', line 107 def []=(key, value) attributes_helper[key] = value end |
#assign_attributes(attributes) ⇒ void
This method returns an undefined value.
Assign multiple attributes to this context from a hash. This method allows bulk assignment of context attributes and supports nested attribute names using dot notation.
88 89 90 |
# File 'lib/lumberjack/context.rb', line 88 def assign_attributes(attributes) attributes_helper.update(attributes) end |
#clear_attributes ⇒ Object
Remove all attributes from this context. This only affects attributes directly set on this context, not those inherited from parent contexts.
113 114 115 |
# File 'lib/lumberjack/context.rb', line 113 def clear_attributes @attributes&.clear end |
#delete(*keys) ⇒ void
This method returns an undefined value.
Remove specific attributes from this context. This only affects attributes directly set on this context, not those inherited from parent contexts. Supports dot notation for nested attribute removal.
124 125 126 |
# File 'lib/lumberjack/context.rb', line 124 def delete(*keys) attributes_helper.delete(*keys) end |
#reset ⇒ void
This method returns an undefined value.
Clear all context data including attributes, level, and progname. This resets the context to its initial state while preserving the parent context relationship.
144 145 146 147 148 |
# File 'lib/lumberjack/context.rb', line 144 def reset @attributes&.clear @level = nil @progname = nil end |