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§
Sourcefn level_color(&self, level: &Level, msg: &str) -> String
fn level_color(&self, level: &Level, msg: &str) -> String
Sourcefn level_token(&self, level: &Level) -> &str
fn level_token(&self, level: &Level) -> &str
Sourcefn prefix_token(&self, level: &Level) -> String
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
Sourcefn line_separator(&self) -> String
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)
Sourcefn format(&self, buf: &mut Formatter, record: &Record<'_>) -> Result<(), Error>
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