Module: Lumberjack::ContextLogger
- Included in:
- ForkedLogger, Logger
- Defined in:
- lib/lumberjack/context_logger.rb
Overview
ContextLogger provides a logging interface with support for contextual attributes, level management, and program name scoping. This module is included by Logger and ForkedLogger to provide a common API for structured logging.
Key features include:
-
Context-aware attribute management with tag/untag methods
-
Scoped logging levels and program names
-
Compatibility with Ruby’s standard Logger API
-
Support for forking isolated logger contexts
Constant Summary collapse
- TRACE =
Constant used for setting trace log level.
Severity::TRACE
- LEADING_OR_TRAILING_WHITESPACE =
/(?:\A\s)|(?:\s\z)/
Class Method Summary collapse
Instance Method Summary collapse
-
#<<(msg) ⇒ void
Add a message when the severity is not known.
-
#add(severity, message_or_progname_or_attributes = nil, progname_or_attributes = nil, &block) ⇒ true
(also: #log)
::Logger compatible method to add a log entry.
-
#add_entry(severity, message, progname = nil, attributes = nil) ⇒ void
private
Add an entry to the log.
-
#append_to(attribute_name, *tags, &block) ⇒ Object, Lumberjack::Logger
Append a value to an attribute.
-
#attribute_value(name) ⇒ Object?
Get the value of an attribute by name from the current context.
-
#attributes ⇒ Hash
Return all attributes in scope on the logger including global attributes set on the Lumberjack context, attributes set on the logger, and attributes set on the current block for the logger.
-
#clear_attributes(&block) ⇒ void
Remove all attributes on the current logger and logging context within a block.
-
#context(&block) {|Context| ... } ⇒ Object
Set up a context block for the logger.
-
#debug(message_or_progname_or_attributes = nil, progname_or_attributes = nil, &block) ⇒ true
Log a
DEBUGmessage. -
#debug! ⇒ void
Set the log level to debug.
-
#debug? ⇒ Boolean
Return
trueifDEBUGmessages are being logged. -
#default_severity ⇒ Integer
Get the default severity used when writing log messages directly to a stream.
-
#default_severity=(value) ⇒ void
Set the default severity used when writing log messages directly to a stream for the current context.
-
#ensure_context(&block) ⇒ Object
Ensure that the block of code is wrapped by a context.
-
#error(message_or_progname_or_attributes = nil, progname_or_attributes = nil, &block) ⇒ true
Log an
ERRORmessage. -
#error! ⇒ void
Set the log level to error.
-
#error? ⇒ Boolean
Return
trueifERRORmessages are being logged. -
#fatal(message_or_progname_or_attributes = nil, progname_or_attributes = nil, &block) ⇒ true
Log a
FATALmessage. -
#fatal! ⇒ void
Set the log level to fatal.
-
#fatal? ⇒ Boolean
Return
trueifFATALmessages are being logged. -
#fork(level: nil, progname: nil, attributes: nil) ⇒ ForkedLogger
Forks a new logger with a new context that will send output through this logger.
-
#in_context? ⇒ Boolean
Return true if the thread is currently in a context block with a local context.
-
#info(message_or_progname_or_attributes = nil, progname_or_attributes = nil, &block) ⇒ true
Log an
INFOmessage. -
#info! ⇒ void
Set the log level to info.
-
#info? ⇒ Boolean
Return
trueifINFOmessages are being logged. -
#level ⇒ Integer
(also: #sev_threshold)
Get the level of severity of entries that are logged.
-
#level=(value) ⇒ void
(also: #sev_threshold=)
Set the log level using either an integer level like Logger::INFO or a label like :info or “info”.
-
#progname ⇒ String?
Get the current progname.
-
#progname=(value) ⇒ void
Set the logger progname for the current context.
-
#tag(attributes, &block) ⇒ Object, Lumberjack::ContextLogger
Tag the logger with a set of attributes.
-
#tag!(attributes) ⇒ nil
Tags the logger with a set of persistent attributes.
-
#tag_all_contexts(attributes) ⇒ nil
Tags the outermost context with a set of attributes.
-
#trace(message_or_progname_or_attributes = nil, progname_or_attributes = nil, &block) ⇒ true
Log a
TRACEmessage. -
#trace! ⇒ void
Set the log level to trace.
-
#trace? ⇒ Boolean
Return
trueifTRACEmessages are being logged. -
#unknown(message_or_progname_or_attributes = nil, progname_or_attributes = nil, &block) ⇒ void
Log a message when the severity is not known.
-
#untag(*attribute_names) ⇒ void
Remove attributes from the current context block.
-
#untag!(*attribute_names) ⇒ void
Remove attributes from the default context for the logger.
-
#warn(message_or_progname_or_attributes = nil, progname_or_attributes = nil, &block) ⇒ true
Log a
WARNmessage. -
#warn! ⇒ void
Set the log level to warn.
-
#warn? ⇒ Boolean
Return
trueifWARNmessages are being logged. -
#with_level(severity, &block) ⇒ Object
Adjust the log level during the block execution for the current Fiber only.
-
#with_progname(value) {|Object| ... } ⇒ Object
Set the logger progname for the duration of the block.
Class Method Details
.included(base) ⇒ Object
28 29 30 31 |
# File 'lib/lumberjack/context_logger.rb', line 28 def included(base) base.include(ContextLocals) unless base.include?(ContextLocals) base.include(IOCompatibility) unless base.include?(IOCompatibility) end |
Instance Method Details
#<<(msg) ⇒ void
This method returns an undefined value.
Add a message when the severity is not known.
302 303 304 |
# File 'lib/lumberjack/context_logger.rb', line 302 def <<(msg) add_entry(default_severity, msg) end |
#add(severity, message_or_progname_or_attributes = nil, progname_or_attributes = nil, &block) ⇒ true Also known as: log
::Logger compatible method to add a log entry.
122 123 124 125 126 127 128 129 130 |
# File 'lib/lumberjack/context_logger.rb', line 122 def add(severity, = nil, progname_or_attributes = nil, &block) # This convoluted logic is to have API compatibility with ::Logger#add. severity ||= Logger::UNKNOWN if .nil? && !progname_or_attributes.is_a?(Hash) = progname_or_attributes progname_or_attributes = nil end call_add_entry(severity, , progname_or_attributes, &block) end |
#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. This method must be implemented by the class that includes this module.
522 523 524 |
# File 'lib/lumberjack/context_logger.rb', line 522 def add_entry(severity, , progname = nil, attributes = nil) raise NotImplementedError end |
#append_to(attribute_name, *tags, &block) ⇒ Object, Lumberjack::Logger
Append a value to an attribute. This method can be used to add “tags” to a logger by appending values to the same attribute. The tag values will be appended to any value that is already in the attribute. If a block is passed, then a new context will be opened as well. If no block is passed, then the values will be appended to the attribute in the current context. If there is no current context, then nothing will happen.
388 389 390 391 392 393 394 395 396 |
# File 'lib/lumberjack/context_logger.rb', line 388 def append_to(attribute_name, *, &block) return self unless block || in_context? = attribute_value(attribute_name) || [] = [] unless .is_a?(Array) = + .flatten tag(attribute_name => , &block) end |
#attribute_value(name) ⇒ Object?
Get the value of an attribute by name from the current context.
487 488 489 490 |
# File 'lib/lumberjack/context_logger.rb', line 487 def attribute_value(name) name = name.join(".") if name.is_a?(Array) AttributesHelper.new(attributes)[name] end |
#attributes ⇒ Hash
Return all attributes in scope on the logger including global attributes set on the Lumberjack context, attributes set on the logger, and attributes set on the current block for the logger.
479 480 481 |
# File 'lib/lumberjack/context_logger.rb', line 479 def attributes merge_all_attributes || {} end |
#clear_attributes(&block) ⇒ void
This method returns an undefined value.
Remove all attributes on the current logger and logging context within a block. You can still set new block scoped attributes within the block and provide attributes on individual log methods.
497 498 499 500 501 502 503 504 505 |
# File 'lib/lumberjack/context_logger.rb', line 497 def clear_attributes(&block) new_context_locals do |locals| locals.cleared = true context do |ctx| ctx.clear_attributes block.call end end end |
#context(&block) {|Context| ... } ⇒ Object
Set up a context block for the logger. All attributes added within the block will be cleared when the block exits.
404 405 406 407 408 409 410 411 412 413 414 415 |
# File 'lib/lumberjack/context_logger.rb', line 404 def context(&block) unless block_given? raise ArgumentError, "A block must be provided to the context method" end new_context = Context.new(current_context) new_context.parent = local_context new_context_locals do |locals| locals.context = new_context block.call(new_context) end end |
#debug(message_or_progname_or_attributes = nil, progname_or_attributes = nil, &block) ⇒ true
Log a DEBUG message. The message can be passed in either the message argument or in a block.
241 242 243 |
# File 'lib/lumberjack/context_logger.rb', line 241 def debug( = nil, progname_or_attributes = nil, &block) call_add_entry(Logger::DEBUG, , progname_or_attributes, &block) end |
#debug! ⇒ void
This method returns an undefined value.
Set the log level to debug.
255 256 257 |
# File 'lib/lumberjack/context_logger.rb', line 255 def debug! self.level = Logger::DEBUG end |
#debug? ⇒ Boolean
Return true if DEBUG messages are being logged.
248 249 250 |
# File 'lib/lumberjack/context_logger.rb', line 248 def debug? level <= Logger::DEBUG end |
#default_severity ⇒ Integer
Get the default severity used when writing log messages directly to a stream.
102 103 104 |
# File 'lib/lumberjack/context_logger.rb', line 102 def default_severity current_context&.default_severity || default_context&.default_severity || Logger::UNKNOWN end |
#default_severity=(value) ⇒ void
This method returns an undefined value.
Set the default severity used when writing log messages directly to a stream for the current context.
111 112 113 114 |
# File 'lib/lumberjack/context_logger.rb', line 111 def default_severity=(value) ctx = current_context ctx.default_severity = value if ctx end |
#ensure_context(&block) ⇒ Object
Ensure that the block of code is wrapped by a context. If there is not already a context in scope for this logger, one will be created.
421 422 423 424 425 426 427 |
# File 'lib/lumberjack/context_logger.rb', line 421 def ensure_context(&block) if in_context? yield else context(&block) end end |
#error(message_or_progname_or_attributes = nil, progname_or_attributes = nil, &block) ⇒ true
Log an ERROR message. The message can be passed in either the message argument or in a block.
166 167 168 |
# File 'lib/lumberjack/context_logger.rb', line 166 def error( = nil, progname_or_attributes = nil, &block) call_add_entry(Logger::ERROR, , progname_or_attributes, &block) end |
#error! ⇒ void
This method returns an undefined value.
Set the log level to error.
180 181 182 |
# File 'lib/lumberjack/context_logger.rb', line 180 def error! self.level = Logger::ERROR end |
#error? ⇒ Boolean
Return true if ERROR messages are being logged.
173 174 175 |
# File 'lib/lumberjack/context_logger.rb', line 173 def error? level <= Logger::ERROR end |
#fatal(message_or_progname_or_attributes = nil, progname_or_attributes = nil, &block) ⇒ true
Log a FATAL message. The message can be passed in either the message argument or in a block.
141 142 143 |
# File 'lib/lumberjack/context_logger.rb', line 141 def fatal( = nil, progname_or_attributes = nil, &block) call_add_entry(Logger::FATAL, , progname_or_attributes, &block) end |
#fatal! ⇒ void
This method returns an undefined value.
Set the log level to fatal.
155 156 157 |
# File 'lib/lumberjack/context_logger.rb', line 155 def fatal! self.level = Logger::FATAL end |
#fatal? ⇒ Boolean
Return true if FATAL messages are being logged.
148 149 150 |
# File 'lib/lumberjack/context_logger.rb', line 148 def fatal? level <= Logger::FATAL end |
#fork(level: nil, progname: nil, attributes: nil) ⇒ ForkedLogger
Forks a new logger with a new context that will send output through this logger. The new logger will inherit the level, progname, and attributes of the current logger context. Any changes to those values, though, will be isolated to just the forked logger. Any calls to log messages will be forwarded to the parent logger for output to the logging device.
446 447 448 449 450 451 452 453 |
# File 'lib/lumberjack/context_logger.rb', line 446 def fork(level: nil, progname: nil, attributes: nil) logger = ForkedLogger.new(self) logger.level = level if level logger.progname = progname if progname logger.tag!(attributes) if attributes logger.isolation_level = isolation_level logger end |
#in_context? ⇒ Boolean
Return true if the thread is currently in a context block with a local context.
510 511 512 |
# File 'lib/lumberjack/context_logger.rb', line 510 def in_context? !!local_context end |
#info(message_or_progname_or_attributes = nil, progname_or_attributes = nil, &block) ⇒ true
Log an INFO message. The message can be passed in either the message argument or in a block.
216 217 218 |
# File 'lib/lumberjack/context_logger.rb', line 216 def info( = nil, progname_or_attributes = nil, &block) call_add_entry(Logger::INFO, , progname_or_attributes, &block) end |
#info! ⇒ void
This method returns an undefined value.
Set the log level to info.
230 231 232 |
# File 'lib/lumberjack/context_logger.rb', line 230 def info! self.level = Logger::INFO end |
#info? ⇒ Boolean
Return true if INFO messages are being logged.
223 224 225 |
# File 'lib/lumberjack/context_logger.rb', line 223 def info? level <= Logger::INFO end |
#level ⇒ Integer Also known as: sev_threshold
Get the level of severity of entries that are logged. Entries with a lower severity level will be ignored.
38 39 40 |
# File 'lib/lumberjack/context_logger.rb', line 38 def level current_context&.level || default_context&.level end |
#level=(value) ⇒ void Also known as: sev_threshold=
This method returns an undefined value.
Set the log level using either an integer level like Logger::INFO or a label like :info or “info”
49 50 51 52 53 54 |
# File 'lib/lumberjack/context_logger.rb', line 49 def level=(value) value = Severity.coerce(value) unless value.nil? ctx = current_context ctx.level = value if ctx end |
#progname ⇒ String?
Get the current progname.
83 84 85 |
# File 'lib/lumberjack/context_logger.rb', line 83 def progname current_context&.progname || default_context&.progname end |
#progname=(value) ⇒ void
This method returns an undefined value.
Set the logger progname for the current context. This is the name of the program that is logging.
73 74 75 76 77 78 |
# File 'lib/lumberjack/context_logger.rb', line 73 def progname=(value) value = value&.to_s&.freeze ctx = current_context ctx.progname = value if ctx end |
#tag(attributes, &block) ⇒ Object, Lumberjack::ContextLogger
Tag the logger with a set of attributes. If a block is given, the attributes will only be set for the duration of the block. Otherwise the attributes will be applied on the current logger context for the duration of the current context. If there is no current context, then a new logger object will be returned with those attributes set on it.
327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/lumberjack/context_logger.rb', line 327 def tag(attributes, &block) if block context do |ctx| ctx.assign_attributes(attributes) block.call(ctx) end else local_context&.assign_attributes(attributes) self end end |
#tag!(attributes) ⇒ nil
Tags the logger with a set of persistent attributes. These attributes will be included on every log entry and are not tied to a context block. If the logger does not have a default context, then these will be ignored.
348 349 350 351 |
# File 'lib/lumberjack/context_logger.rb', line 348 def tag!(attributes) default_context&.assign_attributes(attributes) nil end |
#tag_all_contexts(attributes) ⇒ nil
Tags the outermost context with a set of attributes. If there is no outermost context, then nothing will happen. This method can be used to bubble attributes up to the top level context. It can be used in situations where you want to ensure a set of attributes are set for the rest of the request or operation defined by the outmermost context.
369 370 371 372 373 374 375 376 |
# File 'lib/lumberjack/context_logger.rb', line 369 def tag_all_contexts(attributes) parent_context = local_context while parent_context parent_context.assign_attributes(attributes) parent_context = parent_context.parent end nil end |
#trace(message_or_progname_or_attributes = nil, progname_or_attributes = nil, &block) ⇒ true
Log a TRACE message. The message can be passed in either the message argument or in a block. Trace logs are a level lower than debug and are generally used to log code execution paths for low level debugging.
268 269 270 |
# File 'lib/lumberjack/context_logger.rb', line 268 def trace( = nil, progname_or_attributes = nil, &block) call_add_entry(TRACE, , progname_or_attributes, &block) end |
#trace! ⇒ void
This method returns an undefined value.
Set the log level to trace.
282 283 284 |
# File 'lib/lumberjack/context_logger.rb', line 282 def trace! self.level = TRACE end |
#trace? ⇒ Boolean
Return true if TRACE messages are being logged.
275 276 277 |
# File 'lib/lumberjack/context_logger.rb', line 275 def trace? level <= TRACE end |
#unknown(message_or_progname_or_attributes = nil, progname_or_attributes = nil, &block) ⇒ void
This method returns an undefined value.
Log a message when the severity is not known. Unknown messages will always appear in the log. The message can be passed in either the message argument or in a block.
294 295 296 |
# File 'lib/lumberjack/context_logger.rb', line 294 def unknown( = nil, progname_or_attributes = nil, &block) call_add_entry(Logger::UNKNOWN, , progname_or_attributes, &block) end |
#untag(*attribute_names) ⇒ void
This method returns an undefined value.
Remove attributes from the current context block.
459 460 461 462 463 |
# File 'lib/lumberjack/context_logger.rb', line 459 def untag(*attribute_names) attributes = local_context&.attributes AttributesHelper.new(attributes).delete(*attribute_names) if attributes nil end |
#untag!(*attribute_names) ⇒ void
This method returns an undefined value.
Remove attributes from the default context for the logger.
469 470 471 472 473 |
# File 'lib/lumberjack/context_logger.rb', line 469 def untag!(*attribute_names) attributes = default_context&.attributes AttributesHelper.new(attributes).delete(*attribute_names) if attributes nil end |
#warn(message_or_progname_or_attributes = nil, progname_or_attributes = nil, &block) ⇒ true
Log a WARN message. The message can be passed in either the message argument or in a block.
191 192 193 |
# File 'lib/lumberjack/context_logger.rb', line 191 def warn( = nil, progname_or_attributes = nil, &block) call_add_entry(Logger::WARN, , progname_or_attributes, &block) end |
#warn! ⇒ void
This method returns an undefined value.
Set the log level to warn.
205 206 207 |
# File 'lib/lumberjack/context_logger.rb', line 205 def warn! self.level = Logger::WARN end |
#warn? ⇒ Boolean
Return true if WARN messages are being logged.
198 199 200 |
# File 'lib/lumberjack/context_logger.rb', line 198 def warn? level <= Logger::WARN end |
#with_level(severity, &block) ⇒ Object
Adjust the log level during the block execution for the current Fiber only.
62 63 64 65 66 67 |
# File 'lib/lumberjack/context_logger.rb', line 62 def with_level(severity, &block) context do |ctx| ctx.level = severity block.call(ctx) end end |
#with_progname(value) {|Object| ... } ⇒ Object
Set the logger progname for the duration of the block.
92 93 94 95 96 97 |
# File 'lib/lumberjack/context_logger.rb', line 92 def with_progname(value, &block) context do |ctx| ctx.progname = value block.call(ctx) end end |