Class: Lumberjack::Logger
- Inherits:
-
Logger
- Object
- Logger
- Lumberjack::Logger
- Includes:
- ContextLogger
- Defined in:
- lib/lumberjack/logger.rb
Overview
Lumberjack::Logger is a thread-safe, feature-rich logging implementation that extends Ruby's standard library Logger class with advanced capabilities for structured logging.
Key features include:
- Structured logging with attributes (key-value pairs) attached to log entries
- Context isolation for scoping logging behavior to specific code blocks
- Flexible output devices supporting files, streams, and custom destinations
- Customizable formatters for messages and attributes
The Logger maintains full API compatibility with Ruby's standard Logger while adding powerful extensions for modern logging needs.
Log entries are written to a logging Device if their severity meets or exceeds the log level. Each log entry records the log message and severity along with the time it was logged, the program name, process id, and an optional hash of attributes. Messages are converted to strings using a Formatter associated with the logger.
Direct Known Subclasses
Constant Summary
Constants included from ContextLogger
ContextLogger::LEADING_OR_TRAILING_WHITESPACE, ContextLogger::TRACE
Instance Method Summary collapse
-
#add_entry(severity, message, progname = nil, attributes = nil) ⇒ void
private
Add an entry to the log.
-
#attribute_formatter ⇒ Lumberjack::AttributeFormatter
Get the attribute formatter used to format log entry attributes.
-
#attribute_formatter=(value) ⇒ void
Set the attribute formatter used to format log entry attributes.
-
#close ⇒ void
Close the logging device.
-
#closed? ⇒ Boolean
Returns
trueif the logging device is closed. -
#datetime_format ⇒ String?
Get the timestamp format on the device if it has one.
-
#datetime_format=(format) ⇒ void
Set the timestamp format on the device if it is supported.
-
#device ⇒ Lumberjack::Device
Get the logging device that is used to write log entries.
-
#device=(device) ⇒ void
Set the logging device to a new device.
-
#flush ⇒ void
Flush the logging device.
-
#formatter=(value) ⇒ void
Set the formatter used for log entries.
-
#initialize(logdev, shift_age = 0, shift_size = 1048576, level: DEBUG, progname: nil, formatter: nil, datetime_format: nil, binmode: false, shift_period_suffix: "%Y%m%d", **kwargs) ⇒ Lumberjack::Logger
constructor
Create a new logger to log to a Device.
-
#inspect ⇒ String
Return a human-readable representation of the logger showing its key configuration.
-
#message_formatter ⇒ Lumberjack::Formatter
Get the message formatter used to format log messages.
-
#message_formatter=(value) ⇒ void
Set the message formatter used to format log messages.
-
#reopen(logdev = nil) ⇒ Lumberjack::Logger
Reopen the logging device.
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
Constructor Details
#initialize(logdev, shift_age = 0, shift_size = 1048576, level: DEBUG, progname: nil, formatter: nil, datetime_format: nil, binmode: false, shift_period_suffix: "%Y%m%d", **kwargs) ⇒ Lumberjack::Logger
Create a new logger to log to a Device.
The device argument can be in any one of several formats:
- A symbol for a device name (e.g. :null, :test). You can call
Lumberjack::DeviceRegistry.registered_devicesfor a list. - A stream
- A file path string or
Pathname - A
Lumberjack::Deviceobject - An object with a
writemethod will be wrapped in a Device::Writer - An array of any of the above will open a Multi device that will send output to all devices.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/lumberjack/logger.rb', line 92 def initialize(logdev, shift_age = 0, shift_size = 1048576, level: DEBUG, progname: nil, formatter: nil, datetime_format: nil, binmode: false, shift_period_suffix: "%Y%m%d", **kwargs) init_context_locals! @recursion_guard_key = :"lumberjack_logging_#{object_id}" self.isolation_level = kwargs.delete(:isolation_level) || Lumberjack.isolation_level # Include standard args that affect devices with the optional kwargs which may # contain device specific options. = kwargs.merge(shift_age: shift_age, shift_size: size_with_units(shift_size), binmode: binmode, shift_period_suffix: shift_period_suffix) [:standard_logger_formatter] = formatter if standard_logger_formatter?(formatter) @logdev = Device.open_device(logdev, ) @context = Context.new self.level = level || DEBUG self.progname = progname self.formatter = build_entry_formatter(formatter) self.datetime_format = datetime_format if datetime_format @closed = false end |
Instance Method Details
#add_entry(severity, message, progname = nil, attributes = nil) ⇒ void
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.
This method returns an undefined value.
Add an entry to the log.
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/lumberjack/logger.rb', line 232 def add_entry(severity, , progname = nil, attributes = nil) return false unless device # Prevent infinite recursion if logging is attempted from within a logging call. # The guard is stored in fiber-local storage since recursion is a property of the # current call stack, which belongs to exactly one fiber. if Thread.current[@recursion_guard_key] log_to_stderr(severity, ) return false end severity = Severity.label_to_level(severity) unless severity.is_a?(Integer) begin Thread.current[@recursion_guard_key] = true locals = current_context_locals time = Time.now progname ||= locals&.context&.progname || default_context&.progname attributes = nil unless attributes.is_a?(Hash) attributes = merge_all_attributes(locals, attributes) , attributes = formatter.format(, attributes) if formatter entry = Lumberjack::LogEntry.new(time, severity, , progname, Process.pid, attributes) write_to_device(entry) ensure Thread.current[@recursion_guard_key] = nil end true end |
#attribute_formatter ⇒ Lumberjack::AttributeFormatter
Get the attribute formatter used to format log entry attributes.
176 177 178 |
# File 'lib/lumberjack/logger.rb', line 176 def attribute_formatter formatter.attribute_formatter end |
#attribute_formatter=(value) ⇒ void
This method returns an undefined value.
Set the attribute formatter used to format log entry attributes.
184 185 186 |
# File 'lib/lumberjack/logger.rb', line 184 def attribute_formatter=(value) formatter.attribute_formatter = value end |
#close ⇒ void
This method returns an undefined value.
Close the logging device.
199 200 201 202 203 |
# File 'lib/lumberjack/logger.rb', line 199 def close flush device.close if device.respond_to?(:close) @closed = true end |
#closed? ⇒ Boolean
Returns true if the logging device is closed.
208 209 210 211 212 |
# File 'lib/lumberjack/logger.rb', line 208 def closed? return true if @closed device.respond_to?(:closed?) && device.closed? end |
#datetime_format ⇒ String?
Get the timestamp format on the device if it has one.
144 145 146 |
# File 'lib/lumberjack/logger.rb', line 144 def datetime_format device.datetime_format if device.respond_to?(:datetime_format) end |
#datetime_format=(format) ⇒ void
This method returns an undefined value.
Set the timestamp format on the device if it is supported.
152 153 154 155 156 |
# File 'lib/lumberjack/logger.rb', line 152 def datetime_format=(format) if device.respond_to?(:datetime_format=) device.datetime_format = format end end |
#device ⇒ Lumberjack::Device
Get the logging device that is used to write log entries.
120 121 122 |
# File 'lib/lumberjack/logger.rb', line 120 def device @logdev end |
#device=(device) ⇒ void
This method returns an undefined value.
Set the logging device to a new device.
128 129 130 |
# File 'lib/lumberjack/logger.rb', line 128 def device=(device) @logdev = Device.open_device(device, {}) end |
#flush ⇒ void
This method returns an undefined value.
Flush the logging device. Messages are not guaranteed to be written until this method is called.
191 192 193 194 |
# File 'lib/lumberjack/logger.rb', line 191 def flush device.flush nil end |
#formatter=(value) ⇒ void
This method returns an undefined value.
Set the formatter used for log entries. This can be an EntryFormatter, a standard Logger::Formatter, or any callable object that formats log entries.
137 138 139 |
# File 'lib/lumberjack/logger.rb', line 137 def formatter=(value) @formatter = build_entry_formatter(value) end |
#inspect ⇒ String
Return a human-readable representation of the logger showing its key configuration.
268 269 270 271 |
# File 'lib/lumberjack/logger.rb', line 268 def inspect formatted_object_id = object_id.to_s(16).rjust(16, "0") "#<Lumberjack::Logger:0x#{formatted_object_id} level:#{Severity.level_to_label(level)} device:#{device.class.name} progname:#{progname.inspect} attributes:#{attributes.inspect}>" end |
#message_formatter ⇒ Lumberjack::Formatter
Get the message formatter used to format log messages.
161 162 163 |
# File 'lib/lumberjack/logger.rb', line 161 def formatter. end |
#message_formatter=(value) ⇒ void
This method returns an undefined value.
Set the message formatter used to format log messages.
169 170 171 |
# File 'lib/lumberjack/logger.rb', line 169 def (value) formatter. = value end |
#reopen(logdev = nil) ⇒ Lumberjack::Logger
Reopen the logging device.
218 219 220 221 222 |
# File 'lib/lumberjack/logger.rb', line 218 def reopen(logdev = nil) @closed = false device.reopen(logdev) if device.respond_to?(:reopen) self end |