Class: Semlogr::Formatters::TextFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/semlogr/formatters/text_formatter.rb

Constant Summary collapse

DEFAULT_TEMPLATE =
"[{timestamp}] {severity}: {message}\n{error}".freeze

Instance Method Summary collapse

Constructor Details

#initialize(template: DEFAULT_TEMPLATE) ⇒ TextFormatter

Returns a new instance of TextFormatter.



10
11
12
# File 'lib/semlogr/formatters/text_formatter.rb', line 10

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

Instance Method Details

#format(log_event) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/semlogr/formatters/text_formatter.rb', line 14

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

  @template.tokens.each do |token|
    case token
    when Templates::PropertyToken
      if token.property_name == :message
        log_event.render(output)
      elsif output_properties[token.property_name]
        token.render(output, output_properties)
      end
    else
      token.render(output, output_properties)
    end
  end

  output
end