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
-
:tags
-
:message
Any other words prefixed with a colon will be substituted with the value of the tag with that name.
Constant Summary collapse
- TEMPLATE_ARGUMENT_ORDER =
%w(:time :severity :progname :pid :message :tags).freeze
- MILLISECOND_TIME_FORMAT =
"%Y-%m-%dT%H:%M:%S.%3N"
- MICROSECOND_TIME_FORMAT =
"%Y-%m-%dT%H:%M:%S.%6N"
Instance Method Summary collapse
-
#call(entry) ⇒ Object
Convert an entry into a string using the template.
- #datetime_format ⇒ Object
- #datetime_format=(format) ⇒ Object
-
#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.
30 31 32 33 34 35 36 37 |
# File 'lib/lumberjack/template.rb', line 30 def initialize(first_line, = {}) @first_line_template, @first_line_tags = compile(first_line) additional_lines = [:additional_lines] || "#{Lumberjack::LINE_SEPARATOR}:message" @additional_line_template, @additional_line_tags = 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") self.datetime_format = ([:time_format] || :milliseconds) end |
Instance Method Details
#call(entry) ⇒ Object
Convert an entry into a string using the template.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/lumberjack/template.rb', line 53 def call(entry) return entry unless entry.is_a?(LogEntry) first_line = entry..to_s additional_lines = nil if first_line.include?(Lumberjack::LINE_SEPARATOR) additional_lines = first_line.split(Lumberjack::LINE_SEPARATOR) first_line = additional_lines.shift end formatted_time = @time_formatter.call(entry.time) if @template_include_time format_args = [formatted_time, entry.severity_label, entry.progname, entry.pid, first_line] tag_arguments = tag_args(entry., @first_line_tags) = (@first_line_template % (format_args + tag_arguments)) .rstrip! if .end_with?(" ") if additional_lines && !additional_lines.empty? tag_arguments = tag_args(entry., @additional_line_tags) unless @additional_line_tags == @first_line_tags additional_lines.each do |line| format_args[format_args.size - 1] = line = (@additional_line_template % (format_args + tag_arguments)).rstrip .rstrip! if .end_with?(" ") << end end end |
#datetime_format ⇒ Object
48 49 50 |
# File 'lib/lumberjack/template.rb', line 48 def datetime_format @time_formatter.format end |
#datetime_format=(format) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/lumberjack/template.rb', line 39 def datetime_format=(format) if format == :milliseconds format = MILLISECOND_TIME_FORMAT elsif format == :microseconds format = MICROSECOND_TIME_FORMAT end @time_formatter = Formatter::DateTimeFormatter.new(format) end |