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 |
# File 'lib/lumberjack/context.rb', line 42 def initialize(parent_context = nil) @attributes = nil @level = nil @progname = nil @default_severity = nil if parent_context @attributes = parent_context.attributes.dup if parent_context.attributes 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.
92 93 94 |
# File 'lib/lumberjack/context.rb', line 92 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.
102 103 104 |
# File 'lib/lumberjack/context.rb', line 102 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.
83 84 85 |
# File 'lib/lumberjack/context.rb', line 83 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.
108 109 110 |
# File 'lib/lumberjack/context.rb', line 108 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.
119 120 121 |
# File 'lib/lumberjack/context.rb', line 119 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.
139 140 141 142 143 |
# File 'lib/lumberjack/context.rb', line 139 def reset @attributes&.clear @level = nil @progname = nil end |