pub fn set_hook(hook: Hook)Expand description
Sets a hook to be called at the specified point in the logging process.
Multiple hooks can be set.
IMPORTANT: do not use the macros defined in this crate in BeforeLog or AfterLog hooks.
This will lead to an infinite loop, as the logging macros trigger the hooks
themselves.
Examples found in repository?
examples/hooks.rs (lines 4-8)
3fn main() {
4 traccia::set_hook(Hook::AfterLog(Box::new(|_, target| {
5 if let TargetId::Console(_) = target {
6 println!("This will be printed after the log message");
7 }
8 })));
9
10 traccia::set_hook(Hook::BeforeLog(Box::new(|level, target| {
11 if let TargetId::File(_) = target {
12 if level == LogLevel::Info {
13 println!("This will be printed only before calling the info! macro on a file.")
14 }
15 }
16 })));
17
18 traccia::set_hook(Hook::BeforeLog(Box::new(|_, target| {
19 if let TargetId::Console(_) = target {
20 println!("This will be printed before the log message");
21 }
22 })));
23
24 traccia::init_with_config(traccia::Config {
25 level: LogLevel::Trace,
26 targets: vec![
27 Box::new(traccia::Console::new()),
28 Box::new(
29 traccia::File::new("./.logs/latest.log", traccia::FileMode::Truncate)
30 .expect("Failed to open file."),
31 ),
32 ],
33 ..Default::default()
34 });
35
36 info!("This is a test log message");
37 warn!("This is a test warning message");
38}