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[RW]

Context related data, this protected, don't even think of using it. @return [Hash<[String, Symbol], String>]

formatter[RW]

A function which takes the final context string and formats it @return [Proc]

io[RW]

The underlaying IO to write to, the default is STDOUT @return [IO, puts]

key_value_formatter[RW]

A function which takes a key and value string and produces a string @return [Proc]

level[R]
timestamp[RW]

Whether to prepend timestamps to the logs @return [Boolean]

Public Class Methods

new(data = {}) click to toggle source

@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

initialize_copy(org) click to toggle source

@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
level=(lvl) click to toggle source

@param [Symbol]

# File lib/moon-logfmt/logger.rb, line 53
def level=(lvl)
  @level = Moon::Logfmt.determine_loglevel_from_object(lvl)
end
new(data) click to toggle source

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
write(data) click to toggle source

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

write_context(severity, time, progname, ctx) click to toggle source

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
write_to_logdev(str) click to toggle source
# File lib/moon-logfmt/logger.rb, line 98
          def write_to_logdev(str)
  @io.puts str
end

Private Instance Methods

format_context(severity, time, progname, ctx) click to toggle source

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
timestamp_context(data) click to toggle source

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
transform_context(severity, time, progname, ctx) click to toggle source
# 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