Class: MrLogaLoga::Logger
- Inherits:
-
Logger
- Object
- Logger
- MrLogaLoga::Logger
- Defined in:
- lib/mr_loga_loga/logger.rb
Overview
Description
This class extends the default Ruby Logger to allow users to attach contextual information to log messages.
Example
This creates a Logger that outputs to the standard output stream, with a level of WARN
:
require 'mr_loga_loga'
logger = MrLogaLoga::Logger.new(STDOUT)
logger.level = Logger::WARN
logger.debug("Default")
logger.context(user: 1).debug('with context')
Instance Method Summary collapse
-
#add(severity, message = nil, progname = nil, *_args, **context, &block) ⇒ Object
(also: #log)
Adds a new log message with the given severity.
-
#context(**kwargs, &block) ⇒ Object
Generates a new context.
-
#initialize(*args, **kwargs) ⇒ Logger
constructor
A new instance of Logger.
- #log?(severity) ⇒ Boolean
- #method_missing(symbol, *args, &block) ⇒ Object
- #respond_to_missing?(name, include_private = false) ⇒ Boolean
Constructor Details
#initialize(*args, **kwargs) ⇒ Logger
Returns a new instance of Logger.
24 25 26 27 |
# File 'lib/mr_loga_loga/logger.rb', line 24 def initialize(*args, **kwargs) super @default_formatter = MrLogaLoga::Formatters::KeyValue.new end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
Instance Method Details
#add(severity, message = nil, progname = nil, *_args, **context, &block) ⇒ Object Also known as: log
Adds a new log message with the given severity
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/mr_loga_loga/logger.rb', line 36 def add(severity, = nil, progname = nil, *_args, **context, &block) severity ||= UNKNOWN return true unless log?(severity) progname = @progname if progname.nil? if .nil? if block = block.call else = progname progname = @progname end end @logdev.write(format(format_severity(severity), Time.now, progname, , context)) true end |
#context(**kwargs, &block) ⇒ Object
Generates a new context
30 31 32 33 |
# File 'lib/mr_loga_loga/logger.rb', line 30 def context(**kwargs, &block) context = block ? -> { kwargs.merge(block.call) } : kwargs Context.new(self, context) end |
#log?(severity) ⇒ Boolean
74 75 76 |
# File 'lib/mr_loga_loga/logger.rb', line 74 def log?(severity) !@logdev.nil? && severity >= level end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
70 71 72 |
# File 'lib/mr_loga_loga/logger.rb', line 70 def respond_to_missing?(name, include_private = false) super(name, include_private) end |