Class: VCAP::Logging::Formatter::DelimitedFormatter
- Inherits:
-
BaseFormatter
- Object
- BaseFormatter
- VCAP::Logging::Formatter::DelimitedFormatter
- 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
-
#initialize(delim = DEFAULT_DELIMITER, &blk) ⇒ DelimitedFormatter
constructor
This provides a tiny DSL for constructing the formatting function.
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
'%s'
log_level
data
end
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 |