Module: ContextualLogger::LoggerMixin
Overview
Context Precedence when this is mixed into a logger:
-
inline **context passed to the logger method
-
‘with_context` overrides on the logger object
-
‘global_context` set on the logger passed to this constructor
Constant Summary
Constants included from Context
Instance Method Summary collapse
-
#add(arg_severity, arg1 = nil, arg2 = nil, **context) ⇒ Object
Note that this interface needs to stay compatible with the underlying ::Logger#add interface, which is: def add(severity, message = nil, progname = nil).
- #current_context ⇒ Object (also: #current_context_for_thread)
- #global_context ⇒ Object
- #global_context=(context) ⇒ Object
- #log_level_enabled?(severity) ⇒ Boolean
- #with_context(stacked_context) ⇒ Object
- #write_entry_to_log(severity, timestamp, progname, message, context:) ⇒ Object
Methods included from Context
#current_context_override, #current_context_override=, #thread_context_key_for_logger_instance
Instance Method Details
#add(arg_severity, arg1 = nil, arg2 = nil, **context) ⇒ Object
Note that this interface needs to stay compatible with the underlying ::Logger#add interface, which is: def add(severity, message = nil, progname = nil)
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/contextual_logger.rb', line 125 def add(arg_severity, arg1 = nil, arg2 = nil, **context) # Ruby will prefer to match hashes to last argument because of ** severity = arg_severity || UNKNOWN if log_level_enabled?(severity) if arg1.nil? if block_given? = yield progname = arg2 || context.delete(:progname) || @progname else = arg2 progname = @progname end else = arg1 progname = arg2 || @progname end write_entry_to_log(severity, Time.now, progname, , context: deep_merge_with_current_context(context)) end true end |
#current_context ⇒ Object Also known as: current_context_for_thread
68 69 70 |
# File 'lib/contextual_logger.rb', line 68 def current_context current_context_override || global_context end |
#global_context ⇒ Object
57 58 59 |
# File 'lib/contextual_logger.rb', line 57 def global_context @global_context ||= Context::EMPTY_CONTEXT end |
#global_context=(context) ⇒ Object
61 62 63 64 65 66 |
# File 'lib/contextual_logger.rb', line 61 def global_context=(context) if ( = ::ContextualLogger.) raise ::ContextualLogger::GlobalContextIsLocked, end @global_context = context.freeze end |
#log_level_enabled?(severity) ⇒ Boolean
119 120 121 |
# File 'lib/contextual_logger.rb', line 119 def log_level_enabled?(severity) severity >= level end |
#with_context(stacked_context) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/contextual_logger.rb', line 75 def with_context(stacked_context) context_handler = ContextHandler.new(self, current_context_override) self.current_context_override = deep_merge_with_current_context(stacked_context) if block_given? begin yield ensure context_handler.reset! end else # If no block given, return context handler to the caller so they can call reset! themselves. context_handler end end |
#write_entry_to_log(severity, timestamp, progname, message, context:) ⇒ Object
146 147 148 149 150 151 152 |
# File 'lib/contextual_logger.rb', line 146 def write_entry_to_log(severity, , progname, , context:) @logdev&.write( redactor.redact( (format_severity(severity), , progname, , context: context) ) ) end |