Class: Lumberjack::Template
- Inherits:
-
Object
- Object
- Lumberjack::Template
- Defined in:
- lib/lumberjack/template.rb
Overview
A template converts entries to strings. Templates can contain the following place holders to reference log entry values:
-
:time
-
:severity
-
:progname
-
:unit_of_work_id
-
:message
Constant Summary collapse
- TEMPLATE_ARGUMENT_ORDER =
%w(:time :severity :progname :pid :unit_of_work_id :message).freeze
- DEFAULT_TIME_FORMAT =
"%Y-%m-%dT%H:%M:%S."
- MILLISECOND_FORMAT =
"%03d"
- MICROSECOND_FORMAT =
"%06d"
Instance Method Summary collapse
-
#call(entry) ⇒ Object
Convert an entry into a string using the template.
-
#initialize(first_line, options = {}) ⇒ Template
constructor
Create a new template from the markup.
Constructor Details
#initialize(first_line, options = {}) ⇒ Template
Create a new template from the markup. The first_line
argument is used to format only the first line of a message. Additional lines will be added to the message unformatted. If you wish to format the additional lines, use the :additional_lines
options to specify a template. Note that you’ll need to provide the line separator character in this template if you want to keep the message on multiple lines.
The time will be formatted as YYYY-MM-DDTHH:MM:SSS.SSS by default. If you wish to change the format, you can specify the :time_format
option which can be either a time format template as documented in Time#strftime or the values :milliseconds
or :microseconds
to use the standard format with the specified precision.
Messages will have white space stripped from both ends.
27 28 29 30 31 32 33 34 |
# File 'lib/lumberjack/template.rb', line 27 def initialize(first_line, = {}) @first_line_template = compile(first_line) additional_lines = [:additional_lines] || "#{Lumberjack::LINE_SEPARATOR}:message" @additional_line_template = compile(additional_lines) # Formatting the time is relatively expensive, so only do it if it will be used @template_include_time = first_line.include?(":time") || additional_lines.include?(":time") @time_format = [:time_format] || :milliseconds end |
Instance Method Details
#call(entry) ⇒ Object
Convert an entry into a string using the template.
37 38 39 40 41 42 43 44 45 |
# File 'lib/lumberjack/template.rb', line 37 def call(entry) lines = entry..strip.split(Lumberjack::LINE_SEPARATOR) formatted_time = format_time(entry.time) if @template_include_time = @first_line_template % [formatted_time, entry.severity_label, entry.progname, entry.pid, entry.unit_of_work_id, lines.shift] lines.each do |line| << @additional_line_template % [formatted_time, entry.severity_label, entry.progname, entry.pid, entry.unit_of_work_id, line] end end |