Class: SemanticLogger::Formatters::Logfmt
- Defined in:
- lib/semantic_logger/formatters/logfmt.rb
Overview
Produces logfmt formatted messages
The following fields are extracted from the raw log and included in the formatted message:
:timestamp, :level, :name, :message, :duration, :tags, :named_tags
E.g. timestamp="2020-07-20T08:32:05.375276Z" level=info name="DefaultTest" base="breakfast" spaces="second breakfast" double_quotes=""elevensies"" single_quotes="'lunch'" tag="success"
All timestamps are ISO8601 formatteed
All user supplied values are escaped and surrounded by double quotes to avoid ambiguious message delimeters
tags are treated as keys with boolean values.
In every key (tags, named tags, payload), spaces, control and format characters, =, and "
are replaced with _, so that untrusted data cannot inject fields or lines, or disguise a key.
tags, named_tags, and payload are flattened into the top level fields. A key named like
one of the entry's own fields (the time key, level, name, message, duration,
duration_ms, tag) or an exception field is prefixed with its source, as in payload.level,
so that logged data cannot replace those fields. So is a key that starts with tags.,
named_tags., or payload., so that it cannot forge another source's field. Other keys that
are the same once encoded, such as :user and "user", or "a b" and "a=b", are written
once, with the value from the later source: payload values take precedence over
named_tags, and named_tags over tags.
An exception is emitted as exception_name, exception_message, stack_trace, and cause,
and with log_exception_details also exception_details.
Futher Reading https://brandur.org/logfmt
Constant Summary collapse
- ENTRY_FIELDS =
The entry's own fields in the Raw hash, written in this order after the time key.
%i[level name message duration duration_ms].freeze
- TAG_FIELD =
The field that tells an entry with an exception from one without.
"tag".freeze
- EXCEPTION_FIELDS =
The exception's fields in the Raw hash, mapped to their logfmt names, so that the exception's name and message do not replace the entry's own.
Utils.make_shareable( { name: "exception_name", message: "exception_message", details: "exception_details", stack_trace: "stack_trace", cause: "cause" }.freeze )
- TAGS_PREFIX =
Prefixes that mark a key from logged data with its source, when the key alone would name one of the entry's own fields, or look like a key marked this way.
"tags.".freeze
- NAMED_TAGS_PREFIX =
"named_tags.".freeze
- PAYLOAD_PREFIX =
"payload.".freeze
- SOURCE_PREFIXES =
[TAGS_PREFIX, NAMED_TAGS_PREFIX, PAYLOAD_PREFIX].freeze
- UNSAFE_KEY_CHARS =
Characters that cannot appear in a logfmt key: spaces and control characters (including NEL), which would end the key or the line, the
=and"delimiters, LINE SEPARATOR and PARAGRAPH SEPARATOR, which some readers treat as newlines, and the invisible format characters, such as ZERO WIDTH SPACE and RIGHT-TO-LEFT OVERRIDE, which would let a key pass for another one, such as "level" followed by a ZERO WIDTH SPACE for "level". /[\p{Cc}\p{Cf} ="\u2028\u2029]/- UNSAFE_ASCII_KEY_CHARS =
The ASCII subset of UNSAFE_KEY_CHARS, which is much faster to match against the ASCII keys that make up nearly every log event.
/[\x00-\x20="\x7f]/- PLAIN_SYMBOL_CACHE_SIZE =
The most Symbol keys to remember the names of, so that untrusted keys cannot grow the cache without bound.
512
Instance Attribute Summary
Attributes inherited from Raw
Attributes inherited from Base
#filter, #instance_named_tags, #instance_tags, #name
Instance Method Summary collapse
- #call(log, logger) ⇒ Object
-
#initialize(time_format: :iso_8601, time_key: :timestamp, **args) ⇒ Logfmt
constructor
A new instance of Logfmt.
-
#time_key=(time_key) ⇒ Object
Also writes and reserves the new time key in place of the old one.
Methods inherited from Raw
#application, #duration, #environment, #exception, #file_name_and_line, #host, #level, #message, #metric, #name, #named_tags, #payload, #pid, #tags, #thread_name, #time
Methods inherited from Base
#backtrace, #benchmark, #fast_tag, #level, #level=, #log, #measure, #named_tags, #pop_tags, #push_tags, #should_log?, #silence, #tagged, #tags, #with_level
Constructor Details
#initialize(time_format: :iso_8601, time_key: :timestamp, **args) ⇒ Logfmt
Returns a new instance of Logfmt.
72 73 74 75 |
# File 'lib/semantic_logger/formatters/logfmt.rb', line 72 def initialize(time_format: :iso_8601, time_key: :timestamp, **args) super name_entry_fields end |
Instance Method Details
#call(log, logger) ⇒ Object
83 84 85 86 87 |
# File 'lib/semantic_logger/formatters/logfmt.rb', line 83 def call(log, logger) @raw = super raw_to_logfmt end |
#time_key=(time_key) ⇒ Object
Also writes and reserves the new time key in place of the old one.
78 79 80 81 |
# File 'lib/semantic_logger/formatters/logfmt.rb', line 78 def time_key=(time_key) super name_entry_fields end |