Class: Semlogr::Sinks::ColoredConsole

Inherits:
Object
  • Object
show all
Defined in:
lib/semlogr/sinks/colored_console.rb

Constant Summary collapse

DEFAULT_TEMPLATE =
"[{timestamp}] {severity}: {message}\n{error}".freeze
LOG_SEVERITY_COLORS =
{
  LogSeverity::DEBUG => :white,
  LogSeverity::INFO => :white,
  LogSeverity::WARN => :yellow,
  LogSeverity::ERROR => :red,
  LogSeverity::FATAL => :red
}.freeze
COLOR_CODES =
{
  white: 37,
  yellow: 33,
  red: 31,
  blue: 34
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(template: DEFAULT_TEMPLATE) ⇒ ColoredConsole

Returns a new instance of ColoredConsole.



24
25
26
# File 'lib/semlogr/sinks/colored_console.rb', line 24

def initialize(template: DEFAULT_TEMPLATE)
  @template = Templates::Parser.parse(template)
end

Instance Method Details

#emit(log_event) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/semlogr/sinks/colored_console.rb', line 28

def emit(log_event)
  output = ''
  output_properties = Properties::OutputProperties.create(log_event)

  @template.tokens.each do |token|
    case token
    when Templates::PropertyToken
      render_property_token(output, token, log_event, output_properties)
    else
      token.render(output, output_properties)
    end
  end

  STDOUT.write(output)
end