1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use log::{Level, LevelFilter};
use swing::{Color, ColorFormat, Config, Logger, Rgb, RgbRange, Theme};
/// Custom theme
pub struct MyTheme {}
impl Theme for MyTheme {
/// This method returns the color that will be used for each
/// level when the Solid color format is chosen
///
/// This implementation simply uses the start color
/// from each level's color range as its solid color, but you
/// can perform another level match here and choose whichever
/// specific colors you would like.
fn solid(&self, level: Level) -> Rgb {
self.range(level).start
}
/// This method returns a range of color that each log level will
/// use. The provided color range defines the bounds for linear gradients,
/// and can be used to calculate solid colors, if desired.
fn range(&self, level: Level) -> RgbRange {
match level {
Level::Trace => RgbRange {
start: Rgb {
r: 245,
g: 1,
b: 167,
},
end: Rgb {
r: 245,
g: 121,
b: 167,
},
},
Level::Debug => RgbRange {
start: Color::Cyan.value(),
end: Rgb {
r: 162,
g: 187,
b: 214,
},
},
Level::Info => RgbRange {
start: Color::Green.value(),
end: Rgb {
r: 209,
g: 235,
b: 165,
},
},
Level::Warn => RgbRange {
start: Color::Yellow.value(),
end: Rgb {
r: 214,
g: 202,
b: 169,
},
},
Level::Error => RgbRange {
start: Color::Red.value(),
end: Rgb {
r: 245,
g: 171,
b: 171,
},
},
}
}
}
fn main() {
// setup logger
let config = Config {
level: LevelFilter::Trace,
theme: Box::new(MyTheme {}),
color_format: Some(ColorFormat::InlineGradient(20)),
..Default::default()
};
Logger::with_config(config).init().unwrap();
// log away!
log::trace!("I looked forward to making a crate");
log::debug!("Where colors in logs annotate,");
log::info!("But encountered a yak");
log::warn!("And couldn't look back");
log::error!("'til the hair on that yak did abate");
}