Module fern::colors

source · []
Expand description

Support for ANSI terminal colors via the colored crate.

To enable support for colors, add the "colored" feature in your Cargo.toml:

[dependencies]
fern = { version = "0.6", features = ["colored"] }

Colors are mainly supported via coloring the log level itself, but it’s also possible to color each entire log line based off of the log level.

First, here’s an example which colors the log levels themselves (“INFO” / “WARN” text will have color, but won’t color the rest of the line). ColoredLevelConfig lets us configure the colors per-level, but also has sane defaults.

use fern::colors::{Color, ColoredLevelConfig};

let mut colors = ColoredLevelConfig::new()
    // use builder methods
    .info(Color::Green);
// or access raw fields
colors.warn = Color::Magenta;

It can then be used within any regular fern formatting closure:

fern::Dispatch::new()
    // ...
    .format(move |out, message, record| {
        out.finish(format_args!(
            "[{}] {}",
            // just use 'colors.color(..)' instead of the level
            // itself to insert ANSI colors.
            colors.color(record.level()),
            message,
        ))
    })

Coloring levels is nice, but the alternative is good too. For an example of an application coloring each entire log line with the right color, see examples/pretty-colored.rs.

Structs

Configuration specifying colors a log level can be colored as.

Opaque structure which represents some text data and a color to display it with.

Enums

The 8 standard colors.