[go: up one dir, main page]

Trait CologStyle

Source
pub trait CologStyle {
    // Provided methods
    fn level_color(&self, level: &Level, msg: &str) -> String { ... }
    fn level_token(&self, level: &Level) -> &str { ... }
    fn prefix_token(&self, level: &Level) -> String { ... }
    fn line_separator(&self) -> String { ... }
    fn format(
        &self,
        buf: &mut Formatter,
        record: &Record<'_>,
    ) -> Result<(), Error> { ... }
}
Expand description

Customizable styles for colog

All functions on this trait come with a provided default implementation. This is how to the default style is provided.

To create a custom style, make a new type which implements CologStyle, overriding one or more of the default implementations on the trait.

§Examples

#[macro_use]
extern crate log;
use colog::format::CologStyle;
use env_logger::Builder;
use log::{Level, LevelFilter};

// unless we want state, we don't need any fields on our style type
pub struct CustomLevelToken;

// implement CologStyle on our type, and override `level_token`
impl CologStyle for CustomLevelToken {
    fn level_token(&self, level: &Level) -> &str {
        match *level {
            Level::Error => "ERR",
            Level::Warn => "WRN",
            Level::Info => "INF",
            Level::Debug => "DBG",
            Level::Trace => "TRC",
        }
    }
}

fn main() {
    let mut builder = Builder::new();

    // this is where we enable our custom styling
    builder.format(colog::formatter(CustomLevelToken));

    // set a custom filter level
    builder.filter(None, LevelFilter::Trace);

    // initialize the logger
    builder.init();

    error!("error message");
    error!("error with fmt: {}", 42);
    warn!("warn message");
    info!("info message");
    debug!("debug message");
    trace!("trace message");
}

Provided Methods§

Source

fn level_color(&self, level: &Level, msg: &str) -> String

Format a message for a particular log level

§Parameters
  • level: The log level of the message
  • msg: The message text
§Returns

A String which is formatted according to the level. Typically, this is done by wrapping the string in terminal color codes, appropriate for the log level.

§Defaults

See default_level_color

Examples found in repository?
examples/custom-level-prefix.rs (line 16)
12    fn prefix_token(&self, level: &Level) -> String {
13        format!(
14            "{}{}{}",
15            "| ".blue().bold(),
16            self.level_color(level, self.level_token(level)),
17            " -->".blue().bold()
18        )
19    }
Source

fn level_token(&self, level: &Level) -> &str

Provide a “token” for a particular log level

§Parameters
  • level: The log level of the message
§Returns

A &str which represents the token name for the given log level.

This is typically a single letter ("W" for warn, "D" for debug, etc), or a short string ("WARN", "DEBUG", etc)

§Defaults

See default_level_token

Examples found in repository?
examples/custom-level-prefix.rs (line 16)
12    fn prefix_token(&self, level: &Level) -> String {
13        format!(
14            "{}{}{}",
15            "| ".blue().bold(),
16            self.level_color(level, self.level_token(level)),
17            " -->".blue().bold()
18        )
19    }
Source

fn prefix_token(&self, level: &Level) -> String

Construct the line prefix for a message of the given log level.

This method is not typically overriden (rather Self::level_color or Self::level_token is specialized), but is available for customization

§Defaults

See default_prefix_token

Source

fn line_separator(&self) -> String

Returns the default line separator string, used when formatting multi-line log messages.

When implementing a style where multi-line log messages should appear unchanged, override this method to return "\n".

§Returns

A string which will replace "\n" in the log message.

§Defaults

"\n" + (" | " in bold white)

Source

fn format(&self, buf: &mut Formatter, record: &Record<'_>) -> Result<(), Error>

Top-level formatting function for log messages.

This method is not typically overriden (rather Self::level_color or Self::level_token is specialized), but is available for customization.

Overriding this method entirely changes the behavior of colog.

§Defaults

See default_format

Implementors§