Logger.Backends.Logfmt.Encoder (LoggerLogfmt v1.0.1)
View SourceEncodes key-value pairs in logfmt format.
This module handles the encoding of various Elixir data types into logfmt-compliant key-value pairs, with proper quoting and escaping.
Supported Types
- Integers and floats
- Booleans
- Atoms
- Strings (with automatic quoting when needed)
- Timestamps (multiple formats supported)
- DateTime and NaiveDateTime structs
- Maps (encoded with dot notation for nested keys)
- Any type (fallback to
inspect/2)
Timestamp Formats
:elixir- "2024-01-15 10:30:45.123":iso8601- "2024-01-15T10:30:45.123Z":epoch_time- Unix timestamp (seconds since epoch)
Time Unit Detection for epoch_time
When using :epoch_time format with DateTime or NaiveDateTime values,
the time unit is automatically detected from the key suffix:
*_ms- milliseconds (e.g.,timestamp_ms=1732579200000)*_us- microseconds (e.g.,created_at_us=1732579200000000)*_ns- nanoseconds (e.g.,event_time_ns=1732579200000000000)- No suffix - seconds (e.g.,
timestamp=1732579200)
Examples
iex> Logger.Backends.Logfmt.Encoder.encode("level", "info")
"level=info"
iex> Logger.Backends.Logfmt.Encoder.encode("count", 42)
"count=42"
iex> Logger.Backends.Logfmt.Encoder.encode("message", "Hello World")
"message=\"Hello World\""
Summary
Functions
Encodes a key-value pair in logfmt format.
Functions
Encodes a key-value pair in logfmt format.
Parameters
key- The key (will be converted to string)val- The value to encodeopts- Optional keyword list with encoding options::delimiter- Key-value delimiter (default:=):timestamp_format- Format for timestamp values (default::elixir):prefix- Prefix for nested keys (default:"")
Returns
A string or iolist representing the encoded key-value pair.
Examples
iex> Logger.Backends.Logfmt.Encoder.encode("name", "John")
"name=John"
iex> Logger.Backends.Logfmt.Encoder.encode("count", 42)
"count=42"