class Moon::Logfmt::Logger
Basic Logger
class for Logfmt
writing The main functions are write
and new
new
will copy the current logger and append its context data
Constants
- DEFAULT_DATETIME_FORMAT
The default datetime string format @return [String]
Attributes
Context related data, this protected, don't even think of using it. @return [Hash<[String, Symbol], String>]
A function which takes the final context string and formats it @return [Proc]
The underlaying IO to write to, the default is STDOUT @return [IO, puts]
A function which takes a key and value string and produces a string @return [Proc]
Whether to prepend timestamps to the logs @return [Boolean]
Public Class Methods
@param [Hash<[String, Symbol], String>] data
# File lib/moon-logfmt/logger.rb, line 40 def initialize(data = {}) @io = STDOUT @formatter = Formatter.default @key_value_formatter = KeyValueFormatter.default @context = data @timestamp = true @level = Moon::Logfmt::Severity::DEBUG @datetime_format = DEFAULT_DATETIME_FORMAT end
Public Instance Methods
@param [Logfmt::Logger] org @return [self]
# File lib/moon-logfmt/logger.rb, line 59 def initialize_copy(org) @io = org.io @level = org.level @timestamp = org.timestamp @context = org.context.dup @formatter = org.formatter @key_value_formatter = org.key_value_formatter self end
@param [Symbol]
# File lib/moon-logfmt/logger.rb, line 53 def level=(lvl) @level = Moon::Logfmt.determine_loglevel_from_object(lvl) end
Creates a new context by forking the current logger
@param [Hash<[Symbol, String], String>] data
# File lib/moon-logfmt/logger.rb, line 122 def new(data) dup.tap { |l| l.context.merge!(data) } end
Writes a new log line
@param [Hash<[String, Symbol], String>] data
# File lib/moon-logfmt/logger.rb, line 115 def write(data) write_context nil, Time.now, nil, data end
Protected Instance Methods
Writes a new context line to the logdev @param [Integer] severity @param [Time] time @param [String] progname @param [Hash] ctx
# File lib/moon-logfmt/logger.rb, line 107 def write_context(severity, time, progname, ctx) result_ctx = transform_context(severity, time, progname, ctx) write_to_logdev format_context(severity, time, progname, result_ctx) end
# File lib/moon-logfmt/logger.rb, line 98 def write_to_logdev(str) @io.puts str end
Private Instance Methods
Formats the provided context data
@param [Hash<[String, Symbol], String>] data @return [String]
# File lib/moon-logfmt/logger.rb, line 81 def format_context(severity, time, progname, ctx) str = [] Moon::Logfmt.escape_context_data ctx do |key, value| str << @key_value_formatter.call(key, value) end @formatter.call(severity, time, progname, str.join(' ')) end
Adds timestamp information to the provided data
@param [Hash<Symbol, Object>] data to add timestamp to @return [Hash] data given
# File lib/moon-logfmt/logger.rb, line 73 def timestamp_context(data) data.tap { |d| d[:now] = Time.now.strftime(@datetime_format) } end
# File lib/moon-logfmt/logger.rb, line 89 def transform_context(severity, time, progname, ctx) data = {} data[:level] = Moon::Logfmt.loglevel_to_symbol(severity) if severity timestamp_context(data) if @timestamp data[:progname] = progname if progname data.merge!(context.merge(ctx)) data end