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:
- Macro
pattern!
: Builds a pattern at compile-time. - Macro
runtime_pattern!
: Builds a pattern at runtime.
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§
- Formatter
Context - Provides context for formatters.
- Full
Formatter - Full information logs formatter.
- Json
Formatter serde_json
- JSON logs formatter.
- Pattern
Context - Provides context for patterns.
- Pattern
Formatter - Formats logs according to a specified pattern.
- Runtime
Pattern runtime-pattern
- Runtime pattern built via
runtime_pattern!
macro.