Class: MrLogaLoga::Context
- Inherits:
-
Object
- Object
- MrLogaLoga::Context
- Defined in:
- lib/mr_loga_loga/context.rb
Overview
Description
This class provides a fluent interface to attach contextual information to log messages.
Instance Method Summary collapse
-
#add(severity, message = nil, **context) { ... } ⇒ Context
(also: #log)
Log a message with the current context.
-
#context(context = {}) { ... } ⇒ Context
Add a new context to the log message.
-
#initialize(logger, context = {}) ⇒ Context
constructor
A new instance of Context.
- #method_missing(symbol, *args, &block) ⇒ Object
- #respond_to_missing?(name, include_private = false) ⇒ Boolean
Constructor Details
#initialize(logger, context = {}) ⇒ Context
Returns a new instance of Context.
10 11 12 13 |
# File 'lib/mr_loga_loga/context.rb', line 10 def initialize(logger, context = {}) @logger = logger @context = context end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *args, &block) ⇒ Object
54 55 56 57 58 |
# File 'lib/mr_loga_loga/context.rb', line 54 def method_missing(symbol, *args, &block) context = block ? -> { { symbol => block.call } } : { symbol => unwrap(args) } @context = merge_context(@context, context) self end |
Instance Method Details
#add(severity, message = nil, **context) { ... } ⇒ Context Also known as: log
Log a message with the current context
31 32 33 34 35 36 37 38 39 |
# File 'lib/mr_loga_loga/context.rb', line 31 def add(severity, = nil, **context, &block) severity ||= UNKNOWN return true unless @logger.log?(severity) context = merge_context(@context, context) context = context.call if context.is_a?(Proc) @logger.add(severity, , **context, &block) end |
#context(context = {}) { ... } ⇒ Context
Add a new context to the log message
20 21 22 23 24 |
# File 'lib/mr_loga_loga/context.rb', line 20 def context(context = {}, &block) @context = merge_context(@context, context) @context = merge_context(@context, block) self end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
60 61 62 |
# File 'lib/mr_loga_loga/context.rb', line 60 def respond_to_missing?(name, include_private = false) super(name, include_private) end |