Enhance Ruby logger with support for key-value metadata.
Logger::Metadata is available as a ruby gem.
$ gem install logger-metadata
require 'logger-metadata'
logger = Logger.new
Logger::Metadata.inject(logger)
logger.metadata(key: 'value') do
logger.info('message')
end
All log messages within the metadata block will contain the metadata. Nested metadata blocks can be used.
Logger::Metadata will augment the default the default formatter to include metadata and timestamp. For example
[2016-11-30 14:27:19 +0200] Message (key=value)
Rails application is automatically setup by requiring logger/metadata/railtie
if Rails
is defined.