Class: Lumberjack::Context

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.

Parameters:

  • parent_context (Lumberjack::Context, nil) (defaults to: nil)

    The parent context to inherit from.



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

#attributesHash? (readonly)

The attributes hash containing key-value pairs to include in log entries.

Returns:

  • (Hash, nil)

    The attributes hash, or nil if no attributes are set.



16
17
18
# File 'lib/lumberjack/context.rb', line 16

def attributes
  @attributes
end

#default_severityInteger?

The default severity used when writing log messages directly to a stream.

Returns:

  • (Integer, nil)

    The default severity level, or nil if not set.



28
29
30
# File 'lib/lumberjack/context.rb', line 28

def default_severity
  @default_severity
end

#levelInteger?

The logging level for this context.

Returns:

  • (Integer, nil)

    The logging level, or nil if not set (inherits from parent or default).



20
21
22
# File 'lib/lumberjack/context.rb', line 20

def level
  @level
end

#parentLumberjack::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.

Returns:



33
34
35
# File 'lib/lumberjack/context.rb', line 33

def parent
  @parent
end

#prognameString?

The program name for this context.

Returns:

  • (String, nil)

    The program name, or nil if not set (inherits from parent or default).



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.

Parameters:

  • key (String, Symbol)

    The attribute key. Supports dot notation for nested access.

Returns:

  • (Object)

    The attribute value, or nil if the key doesn’t exist.



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.

Parameters:

  • key (String, Symbol)

    The attribute key. Supports dot notation for nested assignment.

  • value (Object)

    The attribute value to set.



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.

Parameters:

  • attributes (Hash)

    A hash of attribute names to values. Keys can be strings or symbols, and support dot notation for nested attributes.

See Also:



83
84
85
# File 'lib/lumberjack/context.rb', line 83

def assign_attributes(attributes)
  attributes_helper.update(attributes)
end

#clear_attributesObject

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.

Parameters:

  • keys (Array<String, Symbol>)

    The attribute keys to remove. Can use dot notation for nested attributes.



119
120
121
# File 'lib/lumberjack/context.rb', line 119

def delete(*keys)
  attributes_helper.delete(*keys)
end

#resetvoid

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