Function init_with_config

Source
pub fn init_with_config(config: Config)
Expand description

Initializes the global logger with a custom configuration.

This function allows full customization of the logger behavior.

§Arguments

  • config - The configuration to use

§Panics

Panics if a logger is already initialized

Examples found in repository?
examples/custom-format.rs (lines 16-20)
15fn main() {
16    traccia::init_with_config(traccia::Config {
17        level: LogLevel::Trace,
18        format: Some(Box::new(CustomFormatter)),
19        ..Default::default()
20    });
21
22    trace!("This is a trace message");
23    debug!("This is a debug message");
24    info!("This is an info message");
25    warn!("This is a warn message");
26    error!("This is an error message");
27    fatal!("This is a fatal message");
28}
More examples
Hide additional examples
examples/output.rs (lines 4-8)
3fn main() {
4    traccia::init_with_config(traccia::Config {
5        level: LogLevel::Debug,
6        targets: vec![Box::new(Console::new().output(Output::Stderr))],
7        ..Default::default()
8    });
9
10    debug!("This will be logged to stderr.");
11    info!("In fact, all messages will be logged to stderr.");
12    error!("This is an error logged to stderr!!!");
13    fatal!("This is a fatal error logged to stderr!!!");
14}
examples/filtered-output.rs (lines 4-12)
3fn main() {
4    traccia::init_with_config(traccia::Config {
5        level: LogLevel::Debug,
6        targets: vec![Box::new(
7            Console::new()
8                .filtered_output(LogLevel::Error, Output::Stderr)
9                .filtered_output(LogLevel::Fatal, Output::Stderr),
10        )],
11        ..Default::default()
12    });
13
14    debug!("This will be logged to stdout");
15    info!("In fact, only error and fatal messages will be logged to stderr.");
16
17    error!("This is an error logged to stderr!!!");
18    fatal!("This is a fatal error logged to stderr!!!");
19}
examples/level-filtering.rs (lines 4-15)
3fn main() {
4    traccia::init_with_config(traccia::Config {
5        level: LogLevel::Trace,
6        targets: vec![
7            Box::new(traccia::Console::new()),
8            Box::new(
9                traccia::File::new("./.logs/latest.log", FileMode::Truncate)
10                    .expect("Failed to open file.")
11                    .filtered(LogLevel::Fatal),
12            ),
13        ],
14        ..Default::default()
15    });
16
17    info!("This will not be written to latest.log, but will be printed to console.");
18    error!("It will write fatal messages only!");
19    fatal!("Like this :(");
20}
examples/target.rs (lines 4-15)
3fn main() {
4    traccia::init_with_config(traccia::Config {
5        level: LogLevel::Trace,
6        targets: vec![
7            Box::new(traccia::Console::new()),
8            Box::new(
9                traccia::File::new(".logs/latest.log", FileMode::Truncate)
10                    .unwrap()
11                    .filtered(LogLevel::Fatal),
12            ),
13        ],
14        ..Default::default()
15    });
16
17    trace!("This is a trace message");
18    debug!("This is a debug message");
19    info!("This is an info message");
20    warn!("This is a warn message");
21    error!("This is an error message");
22    fatal!("This is a fatal message");
23}
examples/threads.rs (lines 20-24)
19fn main() {
20    traccia::init_with_config(traccia::Config {
21        level: LogLevel::Trace,
22        format: Some(Box::new(CustomFormatter)),
23        ..Default::default()
24    });
25
26    let handles = (0..3)
27        .map(|i| {
28            std::thread::spawn(move || {
29                traccia::trace!("This is a trace message from thread {}", i);
30                traccia::debug!("This is a debug message from thread {}", i);
31                traccia::info!("This is an info message from thread {}", i);
32                traccia::warn!("This is a warn message from thread {}", i);
33                traccia::error!("This is an error message from thread {}", i);
34                traccia::fatal!("This is a fatal message from thread {}", i);
35            })
36        })
37        .collect::<Vec<_>>();
38
39    for handle in handles {
40        handle.join().unwrap();
41    }
42}