Skip to main content

Module format

Module format 

Source
Expand description

Output formats.

A Format turns a crate::Record into text on a core::fmt::Write destination. Three formats ship in the crate, each behind its own feature flag: JsonFormat, LogfmtFormat and HumanFormat. Custom formats implement the trait, or wrap a closure with from_fn.

§Output safety

The built-in formats share one escaping policy for untrusted text: C0 and C1 control characters, DEL, the Unicode line and paragraph separators (U+2028, U+2029) and the bidirectional override and isolate controls are always escaped. Every record therefore renders as exactly one line (except JsonFormat::pretty) and cannot inject terminal escape sequences, whatever the message, target, key or value contains.

JSON and logfmt reserve the keys timestamp, level, target, message, file and line. A user field with one of those names is written as fields.<name> so it can never be mistaken for, or override, the record’s real metadata.

§Dynamic and static dispatch

Format::write_record takes &mut dyn fmt::Write, which makes the trait object-safe: Box<dyn Format>, &dyn Format and Arc<dyn Format> all work, so a format can be chosen at run time. Format::write_record_generic is the statically dispatched variant the built-in sinks call. The built-in formats override it with a monomorphized implementation; measured on the reference machine, the dyn path costs roughly 40-60% more per record for the built-in formats because every small write becomes an indirect call.

Structs§

FnFormat
A Format built from a closure. Create one with from_fn.
HumanFormathuman
Human-readable formatter.
JsonFormatjson
JSON formatter.
LogfmtFormatlogfmt
logfmt formatter. See the module docs for the wire format.

Enums§

ColorChoicehuman and std
When to colour HumanFormat output.

Traits§

Format
A record-to-text serializer.

Functions§

from_fn
Use a closure as a Format.