Class: Lumberjack::ForkedLogger
- Includes:
- ContextLogger
- Defined in:
- lib/lumberjack/forked_logger.rb
Overview
ForkedLogger provides an isolated logging context that forwards all log entries to a parent logger while maintaining its own independent configuration (level, progname, attributes).
This class allows you to create specialized logger instances that:
-
Inherit initial configuration from a parent logger
-
Maintain isolated settings (level, progname, attributes) that don’t affect the parent
-
Forward all log entries to the parent logger’s output device
-
Combine their own attributes with the parent logger’s attributes
-
Provide scoped logging behavior without duplicating output infrastructure
ForkedLogger is particularly useful for:
-
Component isolation: Give each component its own logger with specific attributes
-
Request tracing: Create request-specific loggers with request IDs
-
Temporary debugging: Create debug-level loggers for specific code paths
-
Library integration: Allow libraries to have their own logging configuration
-
Multi-tenant logging: Isolate tenant-specific logging configuration
The forked logger inherits the parent’s initial state but changes are isolated:
-
Inherited: Initial level, progname, and device (through forwarding)
-
Isolated: subsequent changes to level, progname, and attributes do not affect the parent logger
-
Combined: attributes from the parent and the forked loggers are merged when logging
Constant Summary
Constants included from ContextLogger
ContextLogger::LEADING_OR_TRAILING_WHITESPACE, ContextLogger::TRACE
Instance Attribute Summary collapse
-
#parent_logger ⇒ Lumberjack::Logger, #add_entry
readonly
The parent logger that receives all log entries from this forked logger.
Instance Method Summary collapse
-
#add_entry(severity, message, progname = nil, attributes = nil) ⇒ Boolean
private
Forward a log entry to the parent logger with the forked logger’s configuration applied.
-
#device ⇒ Lumberjack::Device
Return the log device of the parent logger, if available.
-
#flush ⇒ void
Flush any buffered log entries in the parent logger’s device.
-
#formatter ⇒ Lumberjack::EntryFormatter
Return the formatter of the parent logger, if available.
-
#initialize(logger) ⇒ ForkedLogger
constructor
Create a new forked logger that forwards all log entries to the specified parent logger.
Methods included from ContextLogger
#<<, #add, #append_to, #attribute_value, #attributes, #clear_attributes, #context, #debug, #debug!, #debug?, #default_severity, #default_severity=, #ensure_context, #error, #error!, #error?, #fatal, #fatal!, #fatal?, #fork, #in_context?, included, #info, #info!, #info?, #level, #level=, #progname, #progname=, #tag, #tag!, #tag_all_contexts, #trace, #trace!, #trace?, #unknown, #untag, #untag!, #warn, #warn!, #warn?, #with_level, #with_progname
Methods inherited from Logger
#attribute_formatter, #attribute_formatter=, #close, #closed?, #datetime_format, #datetime_format=, #device=, #formatter=, #in_tag_context?, #inspect, #log_at, #message_formatter, #message_formatter=, #remove_tag, #reopen, #set_progname, #silence, #tag_formatter, #tag_formatter=, #tag_globally, #tag_value, #tagged, #tags, #untagged
Constructor Details
#initialize(logger) ⇒ ForkedLogger
Create a new forked logger that forwards all log entries to the specified parent logger. The forked logger inherits the parent’s initial level and progname but maintains its own independent context for future changes.
67 68 69 70 71 72 73 74 |
# File 'lib/lumberjack/forked_logger.rb', line 67 def initialize(logger) init_context_locals! self.isolation_level = logger.isolation_level if logger.respond_to?(:isolation_level) @parent_logger = logger @context = Context.new @context.level ||= logger.level @context.progname ||= logger.progname end |
Instance Attribute Details
#parent_logger ⇒ Lumberjack::Logger, #add_entry (readonly)
The parent logger that receives all log entries from this forked logger.
59 60 61 |
# File 'lib/lumberjack/forked_logger.rb', line 59 def parent_logger @parent_logger end |
Instance Method Details
#add_entry(severity, message, progname = nil, attributes = nil) ⇒ Boolean
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.
Forward a log entry to the parent logger with the forked logger’s configuration applied. This method coordinates between the forked logger’s settings and the parent logger’s output capabilities.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/lumberjack/forked_logger.rb', line 87 def add_entry(severity, , progname = nil, attributes = nil) parent_logger.with_level(level || Logger::DEBUG) do attributes = merge_attributes(local_attributes, attributes) progname ||= self.progname if parent_logger.is_a?(ContextLogger) parent_logger.add_entry(severity, , progname, attributes) else parent_logger.tag(attributes) do parent_logger.add(severity, , progname) end end end end |
#device ⇒ Lumberjack::Device
Return the log device of the parent logger, if available.
112 113 114 |
# File 'lib/lumberjack/forked_logger.rb', line 112 def device parent_logger.device if parent_logger.respond_to?(:device) end |
#flush ⇒ void
This method returns an undefined value.
Flush any buffered log entries in the parent logger’s device.
105 106 107 |
# File 'lib/lumberjack/forked_logger.rb', line 105 def flush parent_logger.flush end |
#formatter ⇒ Lumberjack::EntryFormatter
Return the formatter of the parent logger, if available.
119 120 121 |
# File 'lib/lumberjack/forked_logger.rb', line 119 def formatter parent_logger.formatter if parent_logger.respond_to?(:formatter) end |