View Source LogfmtEx (logfmt_ex v0.4.2)
A convenience for formatting logs in logfmt, to be used with the Logger.Backends.Console
backend.
To use, specify the {LogfmtEx, :format}
tuple in the backend's configuration, replacing the format string:
config :logger, :console,
format: {LogfmtEx, :format}
logfmt
Logfmt
In logfmt, each line consists of a single level of key=value pairs, densely packed together.
For example:
Logger.info("I am a message", user_id: 123)
given the configuration
config :logger, :console,
format: {LogfmtEx, :format},
metadata: [:user_id, :pid, :file]
config :logfmt_ex, :opts,
message_key: "msg",
timestamp_key: "ts",
timestamp_format: :iso8601
would emit:
level=info msg="I am a message" ts="12:38:38.055 1973-03-12" user_id=123 pid=#PID<0.223.0> file=myapp/some_module.exs
configuration
Configuration
Several aspects of the format function can be customized via the application env in a config/config.exs
file,
under config :logfmt_ex, :opts
:
:delimiter
- defaults to=
.:format
- A list of atoms that defines the order in which key/value pairs will written to the log line. Defaults to[:timestamp, :level, :message, :metadata]
. Valid parameters are:timestamp
- the timestamp of the log message:level
- the log level:message
- the log message itself:metadata
- metadata as key=value paris:node
- the node name
timestamp_key
- changes the key used for the timestamp field. Defaults totimestamp
.timestamp_format
- How the timestamp is formatted. Defaults to:elixir
. The options are:elixir
- Uses the same formatting functions found in the standard elixir log formatter. Example:"12:38:38.055 1973-03-12"
:epoch_seconds
- outputs an integer representing the number of seconds elapsed since January 1, 1970. Only useful for applications that emit logs sporadically.:iso8601
- Formats the timestamp according to ISO8601-2019. Example:2000-02-29T23:00:07
level_key
- the key used for the log level. Defaults tolevel
.message_key
- the key used for the message field. Defaults tomessage
, butmsg
is a popular alternative.
For encoding your own structs and types, see the LogfmtEx.ValueEncoder
protocol.
Link to this section Summary
Types
A pattern is a list of valid pattern keys that determines in which order the key=value pairs are printed. It defaults to [:timestamp, :level, :message, :metadata].
Valid pattern keys. These mostly mimic the pattern keys in Logger.Formatter
,
though :time and :date are merged into :timestamp.
Functions
The main formatting function.
Link to this section Types
@type pattern() :: [pattern_keys()]
A pattern is a list of valid pattern keys that determines in which order the key=value pairs are printed. It defaults to [:timestamp, :level, :message, :metadata].
@type pattern_keys() :: :timestamp | :level | :message | :metadata | :node
Valid pattern keys. These mostly mimic the pattern keys in Logger.Formatter
,
though :time and :date are merged into :timestamp.
Link to this section Functions
The main formatting function.
It is invoked by the console backend with four arguments:
- the log level: an atom (
atom/0
) - the message: this is usually
IO.chardata/0
- the current timestamp: a term of type
Logger.Formatter.time/0
- the metadata: a keyword list (
keyword/0
)
May optionally be passed a list of options that is merged into the Application environment.