class MrLogaLoga::Logger
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')
Public Class Methods
new(*args, **kwargs)
click to toggle source
Calls superclass method
# File lib/mr_loga_loga/logger.rb, line 24 def initialize(*args, **kwargs) super @default_formatter = MrLogaLoga::Formatters::KeyValue.new end
Public Instance Methods
add(severity, message = nil, progname = nil, *_args, **context, &block)
click to toggle source
Adds a new log message with the given severity
# File lib/mr_loga_loga/logger.rb, line 36 def add(severity, message = nil, progname = nil, *_args, **context, &block) severity ||= UNKNOWN return true unless log?(severity) progname = @progname if progname.nil? if message.nil? if block message = block.call else message = progname progname = @progname end end @logdev.write(format(format_severity(severity), Time.now, progname, message, context)) true end
Also aliased as: log
context(**kwargs, &block)
click to toggle source
Generates a new context
# 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)
click to toggle source
# File lib/mr_loga_loga/logger.rb, line 74 def log?(severity) !@logdev.nil? && severity >= level end
method_missing(symbol, *args, &block)
click to toggle source
# File lib/mr_loga_loga/logger.rb, line 65 def method_missing(symbol, *args, &block) context = block ? -> { { symbol => block.call } } : { symbol => unwrap(args) } Context.new(self, context) end
respond_to_missing?(name, include_private = false)
click to toggle source
Calls superclass method
# File lib/mr_loga_loga/logger.rb, line 70 def respond_to_missing?(name, include_private = false) super(name, include_private) end
Private Instance Methods
format(severity, datetime, progname, message, context)
click to toggle source
# File lib/mr_loga_loga/logger.rb, line 88 def format(severity, datetime, progname, message, context) (@formatter || @default_formatter).call(severity, datetime, progname, message, **context) end
unwrap(args)
click to toggle source
# File lib/mr_loga_loga/logger.rb, line 80 def unwrap(args) if args.size == 1 args[0] else args end end