Class: Lumberjack::ForkedLogger

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

Examples:

Basic forked logger

parent = Lumberjack::Logger.new(STDOUT, level: :info)
forked = Lumberjack::ForkedLogger.new(parent)
forked.level = :debug  # Only affects the forked logger
forked.debug("Debug message")  # Logged because forked logger is debug level

Component-specific logging

main_logger = Lumberjack::Logger.new("/var/log/app.log")
db_logger = Lumberjack::ForkedLogger.new(main_logger)
db_logger.progname = "Database"
db_logger.tag!(component: "database", version: "1.2.3")
db_logger.info("Connection established")  # Includes component attributes and different progname

Request-scoped logging

def handle_request(request_id)
  request_logger = Lumberjack::ForkedLogger.new(@logger)
  request_logger.tag!(request_id: request_id, user_id: current_user.id)

  request_logger.info("Processing request")  # Includes request context
  # ... process request ...
  request_logger.info("Request completed")   # All logs tagged with request info
end

See Also:

Constant Summary

Constants included from ContextLogger

ContextLogger::LEADING_OR_TRAILING_WHITESPACE, ContextLogger::TRACE

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • logger (Lumberjack::ContextLogger, #add_entry)

    The parent logger to forward entries to. Must respond to either add_entry (for Lumberjack loggers) or standard Logger methods.



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_loggerLumberjack::Logger, #add_entry (readonly)

The parent logger that receives all log entries from this forked logger.

Returns:



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.

Parameters:

  • severity (Integer, Symbol, String)

    The severity level of the log entry.

  • message (Object)

    The message to log.

  • progname (String, nil) (defaults to: nil)

    The program name (defaults to this logger’s progname).

  • attributes (Hash, nil) (defaults to: nil)

    Additional attributes to include with the log entry.

Returns:

  • (Boolean)

    Returns the result of the parent logger’s logging operation.



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, message, 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, message, progname, attributes)
    else
      parent_logger.tag(attributes) do
        parent_logger.add(severity, message, progname)
      end
    end
  end
end

#deviceLumberjack::Device

Return the log device of the parent logger, if available.

Returns:



112
113
114
# File 'lib/lumberjack/forked_logger.rb', line 112

def device
  parent_logger.device if parent_logger.respond_to?(:device)
end

#flushvoid

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

#formatterLumberjack::EntryFormatter

Return the formatter of the parent logger, if available.

Returns:



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

def formatter
  parent_logger.formatter if parent_logger.respond_to?(:formatter)
end