Format-json
Render a Record into compact JSON text. This helper is the direct structured-output path used by JSON-oriented logging workflows when you want machine-readable serialization without building a sink.
Interface
pub fn format_json(rec : Record) -> String {input
rec : Record- Log record to serialize.
output
String- Compact JSON text built from the record contents.
Explanation
Detailed rules explaining key parameters and behaviors
- The output always includes
level,message, andfields. timestamp_msis only included whenrec.timestamp_ms != 0.targetis only included whenrec.targetis not empty.- The result is compact JSON text rather than pretty-printed output.
- Field values are serialized through the record's structured field array instead of reparsing formatted text.
How to Use
Here are some specific examples provided.
When Need Machine-readable Output In Tests Or Adapters
When record data should be passed along as structured JSON text:
let rec = Record::new(Level::Info, "ready", fields=[field("service", "api")])
println(format_json(rec))In this example, the returned string can be consumed by JSON-aware tooling.
When Need Structured Output Without A Json Sink Instance
When a callback or bridge already has a Record and only needs serialization:
let callback = fn(rec : Record) {
let payload = format_json(rec)
println(payload)
}In this example, the code reuses the built-in JSON record shape directly.
Error Case
e.g.:
There is no separate failure path for valid
Recordvalues.If a caller needs readable aligned text rather than JSON,
format_text(...)is the better API.
Notes
This helper returns compact JSON and does not expose a pretty-print option.
It is a natural companion to
json_console_sink()for structured logging flows.