Module formatter

Source
Expand description

Provides formatters for sink formatting log records.

§Formatter

Each normal Sink owns a Formatter, which is used to format each log.

The default formatter for most sinks is FullFormatter, you can call Sink::set_formatter to replace it with another formatter.

The easiest way to make a custom formatter is to build a pattern, see Compile-time and runtime pattern formatter below. If pattern isn’t flexible enough for you, you need to implement Formatter trait for your own formatter struct. See the implementation of FullFormatter and ./examples directory for examples.

§Compile-time and runtime pattern formatter

spdlog-rs supports formatting your log records according to a pattern string. There are 2 ways to construct a pattern:

use spdlog::formatter::{pattern, PatternFormatter};

// This pattern is built at compile-time, the template accepts only a literal string.
let pattern = pattern!("[{date} {time}.{millisecond}] [{level}] {payload}{eol}");

#[cfg(feature = "runtime-pattern")]
{
    use spdlog::formatter::runtime_pattern;

    // This pattern is built at runtime, the template accepts a runtime string.
    let input = "[{date} {time}.{millisecond}] [{level}] {payload}{eol}";
    let pattern = runtime_pattern!(input)?;
}

// Use the compile-time or runtime pattern.
your_sink.set_formatter(Box::new(PatternFormatter::new(pattern)));

Macros§

pattern
Builds a pattern from a template literal string at compile-time.
runtime_pattern
Builds a pattern from a template string at runtime.

Structs§

FormatterContext
Provides context for formatters.
FullFormatter
Full information logs formatter.
JsonFormatterserde_json
JSON logs formatter.
PatternContext
Provides context for patterns.
PatternFormatter
Formats logs according to a specified pattern.
RuntimePatternruntime-pattern
Runtime pattern built via runtime_pattern! macro.

Traits§

Formatter
Represents a formatter that can be used for formatting logs.
Pattern
Represents a pattern for replacing a placeholder in templates.