Logger.Backends.Logfmt (LoggerLogfmt v1.0.1)
View SourceA Logfmt formatter for Elixir's Logger.
This module provides functions to format log messages in the logfmt format, a structured logging format that is easy to parse and human-readable.
Features
- Flexible format configuration with customizable fields
- Support for metadata filtering (whitelist/blacklist modes)
- Multiple timestamp formats (Elixir, ISO8601, Unix epoch)
- Automatic quoting and escaping of values
- Nested map support with dot notation
Configuration
Configure the formatter in your config/config.exs:
config :logger, :logfmt,
format: [:timestamp, :level, :message, :metadata],
metadata: [:application, :request_id],
mode: :whitelist,
timestamp_format: :iso8601Format Options
The :format option accepts a list of atoms that determine which fields
to include in the output:
:timestamp- Log event timestamp:level- Log level (debug, info, warn, error):message- Log message:domain- Logger domain:node- Node name:pid- Process identifier:metadata- Additional metadata:file- Source file:line- Line number
Examples
iex> result = Logger.Backends.Logfmt.format(:info, "User logged in", {{2024, 1, 15}, {10, 30, 45, 123}}, [user_id: 42])
iex> output = IO.iodata_to_binary(result)
iex> output =~ ~r/timestamp=2024-01-15T10:30:45.123/
true
iex> output =~ ~r/level=info/
true
iex> output =~ ~r/message="User logged in"/
true
Summary
Functions
Formats a log message in logfmt format.
Functions
Formats a log message in logfmt format.
Parameters
level- The log level (:debug,:info,:warn,:error)message- The log messagetimestamp- The timestamp tuple{{year, month, day}, {hour, minute, second, millisecond}}metadata- A keyword list of metadataopts- Optional keyword list of formatting options
Returns
An iolist containing the formatted log message.
Examples
iex> result = Logger.Backends.Logfmt.format(:info, "Hello", {{2024, 1, 1}, {12, 0, 0, 0}}, [])
iex> output = IO.iodata_to_binary(result)
iex> output =~ ~r/level=info/
true
iex> output =~ ~r/message=Hello/
true