Class: VCAP::Logging::Formatter::DelimitedFormatter

Inherits:
BaseFormatter
  • Object
show all
Defined in:
lib/vcap/logging/formatter/delimited_formatter.rb

Overview

A formatter for creating messages delimited by a given value (e.g. space separated logs)

Constant Summary collapse

DEFAULT_DELIMITER =
' '
DEFAULT_TIMESTAMP_FORMAT =

YYYY-MM-DD HH:MM:SS TZ

'%F %T %z'

Instance Method Summary collapse

Methods inherited from BaseFormatter

#format_record, #parse_message

Constructor Details

#initialize(delim = DEFAULT_DELIMITER, &blk) ⇒ DelimitedFormatter

This provides a tiny DSL for constructing the formatting function. We opt to define the method inline in order to avoid incurring multiple method calls per call to format_record().

Usage:

formatter = VCAP::Logging::Formatter::DelimitedFormatter.new do

timestamp '%s'
log_level
data

end

Parameters:

  • delim (defaults to: DEFAULT_DELIMITER)

    String Delimiter that will separate fields in the message

  • Block

    Block that defines the log message format



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/vcap/logging/formatter/delimited_formatter.rb', line 26

def initialize(delim=DEFAULT_DELIMITER, &blk)
  @exprs = []

  # Collect the expressions we want to use when constructing messages in the
  # order that they should appear.
  instance_eval(&blk)

  # Build the format string to that will generate the message along with
  # the arguments
  fmt_chars = @exprs.map {|e| e[0] }
  fmt       = fmt_chars.join(delim) + "\n"
  fmt_args  = @exprs.map {|e| e[1] }.join(', ')

  instance_eval("def format_record(log_record); '#{fmt}' % [#{fmt_args}]; end")
end