Class: Moon::Logfmt::Logger

Inherits:
Object
  • Object
show all
Includes:
StdlibLoggable
Defined in:
lib/moon-logfmt/logger.rb

Overview

Basic Logger class for Logfmt writing The main functions are #write and #new #new will copy the current logger and append its context data

Constant Summary

Constants included from Severity

Severity::DEBUG, Severity::ERROR, Severity::FATAL, Severity::INFO, Severity::UNKNOWN, Severity::WARN

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from StdlibLoggable

#add, #debug, #error, #fatal, #info, #unknown, #warn

Constructor Details

#initialize(data = {}) ⇒ Logger

Returns a new instance of Logger.

Parameters:

  • data (Hash<[String, Symbol], String>) (defaults to: {})


31
32
33
34
35
36
# File 'lib/moon-logfmt/logger.rb', line 31

def initialize(data = {})
  @io = STDOUT
  @formatter = FORMATTER
  @context = data
  @timestamp = true
end

Instance Attribute Details

#formatterProc

A function which takes a key and value string and produces a string

Returns:

  • (Proc)


18
19
20
# File 'lib/moon-logfmt/logger.rb', line 18

def formatter
  @formatter
end

#ioIO, #puts

The underlaying IO to write to, the default is STDOUT

Returns:

  • (IO, #puts)


14
15
16
# File 'lib/moon-logfmt/logger.rb', line 14

def io
  @io
end

#timestampBoolean

Whether to prepend timestamps to the logs

Returns:

  • (Boolean)


22
23
24
# File 'lib/moon-logfmt/logger.rb', line 22

def timestamp
  @timestamp
end

Instance Method Details

#initialize_copy(org) ⇒ self

Parameters:

Returns:

  • (self)


40
41
42
43
44
45
46
# File 'lib/moon-logfmt/logger.rb', line 40

def initialize_copy(org)
  @io = org.io
  @timestamp = org.timestamp
  @context = org.context.dup
  @formatter = org.formatter
  self
end

#new(data) ⇒ Object

Creates a new context by forking the current logger

Parameters:

  • data (Hash<[Symbol, String], String>)


84
85
86
# File 'lib/moon-logfmt/logger.rb', line 84

def new(data)
  dup.tap { |l| l.context.merge!(data) }
end

#write(data) ⇒ Object

Writes a new log line

Parameters:

  • data (Hash<[String, Symbol], String>)


75
76
77
78
79
# File 'lib/moon-logfmt/logger.rb', line 75

def write(data)
  pre = {}
  timestamp_context(pre) if @timestamp
  @io.puts format_context(pre.merge(context.merge(data)))
end