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. If your tag name contains characters other than alpha numerics and the underscore, you must surround it with curly brackets: ‘:httphttp.request-id`.
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"
- PLACEHOLDER_PATTERN =
/:(([a-z0-9_]+)|({[^}]+}))/i.freeze
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.
33 34 35 36 37 38 39 40 |
# File 'lib/lumberjack/template.rb', line 33 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.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/lumberjack/template.rb', line 56 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
51 52 53 |
# File 'lib/lumberjack/template.rb', line 51 def datetime_format @time_formatter.format end |
#datetime_format=(format) ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/lumberjack/template.rb', line 42 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 |