Class: Lumberjack::Device::LoggerWrapper
- Inherits:
-
Lumberjack::Device
- Object
- Lumberjack::Device
- Lumberjack::Device::LoggerWrapper
- Defined in:
- lib/lumberjack/device/logger_wrapper.rb
Overview
A logging device that forwards log entries to another logger instance. This device enables hierarchical logging architectures and broadcasting scenarios where log entries need to be distributed to multiple loggers or processed through different logging pipelines.
The device is particularly useful when combined with Device::Multi to create master loggers that can simultaneously write to multiple destinations (files, databases, external services) while maintaining consistent formatting and attribute handling across all targets.
Unlike other devices that write directly to output streams, this device delegates to another logger’s processing pipeline, allowing for complex logging topologies and reuse of existing logger configurations.
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
-
#close ⇒ void
Closes the target logger to release any resources or finalize log output.
-
#dev ⇒ IO, ...
private
Expose the underlying stream if any.
-
#flush ⇒ void
Flushes the target logger, ensuring that any buffered log entries are written out.
-
#initialize(logger) ⇒ LoggerWrapper
constructor
Initialize a new Logger device that forwards entries to the specified logger.
-
#reopen ⇒ void
Reopen the underlying logger device.
-
#write(entry) ⇒ void
Forward a log entry to the target logger for processing.
Methods inherited from Lumberjack::Device
#datetime_format, #datetime_format=, open_device
Constructor Details
#initialize(logger) ⇒ LoggerWrapper
Initialize a new Logger device that forwards entries to the specified logger. The target logger must be a Lumberjack logger that supports the ContextLogger interface to ensure proper entry handling and attribute processing.
64 65 66 67 68 69 70 |
# File 'lib/lumberjack/device/logger_wrapper.rb', line 64 def initialize(logger) unless logger.is_a?(Lumberjack::ContextLogger) || logger.is_a?(::Logger) raise ArgumentError.new("Logger must be a Lumberjack logger") end @logger = logger end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
53 54 55 |
# File 'lib/lumberjack/device/logger_wrapper.rb', line 53 def logger @logger end |
Instance Method Details
#close ⇒ void
This method returns an undefined value.
Closes the target logger to release any resources or finalize log output. This method delegates to the target logger’s close method.
106 107 108 |
# File 'lib/lumberjack/device/logger_wrapper.rb', line 106 def close @logger.close end |
#dev ⇒ IO, ...
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.
Expose the underlying stream if any.
133 134 135 |
# File 'lib/lumberjack/device/logger_wrapper.rb', line 133 def dev @logger.device&.dev end |
#flush ⇒ void
This method returns an undefined value.
Flushes the target logger, ensuring that any buffered log entries are written out. This delegates to the target logger’s flush method, which may flush buffers to disk, external services, or other destinations depending on the logger’s configuration.
125 126 127 |
# File 'lib/lumberjack/device/logger_wrapper.rb', line 125 def flush @logger.flush end |
#reopen ⇒ void
This method returns an undefined value.
Reopen the underlying logger device. This is typically used to reopen log files after log rotation or to refresh the logger’s output stream.
Delegates to the target logger’s reopen method.
116 117 118 |
# File 'lib/lumberjack/device/logger_wrapper.rb', line 116 def reopen @logger.reopen end |
#write(entry) ⇒ void
This method returns an undefined value.
Forward a log entry to the target logger for processing. This method extracts the entry components and delegates to the target logger’s add_entry method, ensuring that all attributes, formatting, and processing logic of the target logger are properly applied.
The forwarded entry maintains all original metadata including severity, timestamp, program name, and custom attributes, allowing the target logger to process it as if it were generated directly.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/lumberjack/device/logger_wrapper.rb', line 83 def write(entry) if @logger.is_a?(Lumberjack::ContextLogger) @logger.add_entry(entry.severity, entry., entry.progname, entry.attributes) else = entry. if entry.attributes && !entry.attributes.empty? = [] entry.attributes.each do |key, value| next if value.nil? || (value.respond_to?(:empty?) && value.empty?) value = value.join(",") if value.is_a?(Enumerable) << "[#{key}=#{value}]" end = "#{} #{.join(" ")}" unless .empty? end @logger.add(entry.severity, , entry.progname) end end |