spdlog/formatter/pattern_formatter/pattern/
level.rs

1use std::fmt::Write;
2
3use crate::{
4    formatter::pattern_formatter::{Pattern, PatternContext},
5    Error, Record, StringBuf,
6};
7
8/// A pattern that writes the level of a log record into the output. Examples:
9/// `critical`, `error`, `warn`.
10#[derive(Clone, Default)]
11pub struct Level;
12
13impl Pattern for Level {
14    fn format(
15        &self,
16        record: &Record,
17        dest: &mut StringBuf,
18        _ctx: &mut PatternContext,
19    ) -> crate::Result<()> {
20        dest.write_str(record.level().as_str())
21            .map_err(Error::FormatRecord)
22    }
23}
24
25/// A pattern that writes the level in a shorter form of a log record into the
26/// output. Examples: `C`, `E`, `W`.
27#[derive(Clone, Default)]
28pub struct ShortLevel;
29
30impl Pattern for ShortLevel {
31    fn format(
32        &self,
33        record: &Record,
34        dest: &mut StringBuf,
35        _ctx: &mut PatternContext,
36    ) -> crate::Result<()> {
37        dest.write_str(record.level().as_short_str())
38            .map_err(Error::FormatRecord)
39    }
40}