pub struct JsonFormat { /* private fields */ }json only.Expand description
JSON formatter.
The default configuration emits compact JSON Lines. Every string is
escaped so the output is valid JSON for any input, and the escaping
also covers DEL, C1 controls, U+2028/U+2029 and bidi controls so a
record stays on one line even for tools that treat those as line
breaks. User keys named timestamp, level, target, message,
file or line are written as fields.<name>.
§Examples
use log_io::format::{Format, JsonFormat};
use log_io::{Field, Level, Metadata, Record, Value};
let fmt = JsonFormat::new();
let fields = [Field::new("port", 8080_u16), Field::new("level", "spoofed")];
let record = Record::new(Metadata::new(Level::Info, "app"), "started", &fields);
let mut out = String::new();
fmt.write_record(&record, &mut out)?;
assert_eq!(
out,
"{\"level\":\"info\",\"target\":\"app\",\"message\":\"started\",\
\"port\":8080,\"fields.level\":\"spoofed\"}\n"
);Implementations§
Source§impl JsonFormat
impl JsonFormat
Sourcepub const fn pretty(self, enabled: bool) -> Self
pub const fn pretty(self, enabled: bool) -> Self
Toggle pretty (multi-line, indented) output. Intended for reading by eye; line-oriented ingestion needs the compact form.
Sourcepub const fn timestamp_rfc3339(self, enabled: bool) -> Self
pub const fn timestamp_rfc3339(self, enabled: bool) -> Self
Emit timestamps as RFC 3339 strings instead of Unix nanoseconds.
Enabled: "timestamp":"2023-11-14T22:13:20.000000000Z".
Disabled (default): "timestamp":1700000000000000000. Note that
nanosecond integers exceed 2^53, so consumers that parse JSON
numbers as doubles lose sub-microsecond precision; prefer the
RFC 3339 form for such consumers.