logger

Function logger 

Source
pub fn logger() -> &'static Logger
Examples found in repository?
examples/output_redirection.rs (line 10)
4fn main() -> std::io::Result<()> {
5    // First log to stdout
6    info!("This goes to terminal");
7
8    // Redirect to a file
9    let log_file = File::create("test.log")?;
10    logger().set_writer(Box::new(log_file))?;
11
12    info!("This goes to test.log");
13    warn!("This warning also goes to test.log");
14
15    Ok(())
16}
More examples
Hide additional examples
examples/threads.rs (line 14)
12fn main() {
13    // Enable verbose mode globally
14    logger().verbose(true);
15
16    info!("This is an info message from main on the main thread");
17
18    thread::spawn(|| {
19        debug!("This is a debug message from another thread");
20    })
21    .join()
22    .unwrap();
23
24    function_log();
25
26    thread::spawn(|| {
27        function_thread_log();
28    })
29    .join()
30    .unwrap();
31}
examples/context.rs (line 5)
3fn scan_ports(host: &str) {
4    // Add context that will be included in all logs in this scope
5    let _host_ctx = logger().add_context("host", host);
6
7    info!("Starting port scan");
8
9    // Add another context
10    let _scan_ctx = logger().add_context("scan_type", "TCP");
11    warn!("Found open port 80"); // Will include both host and scan_type
12
13    // scan_ctx is dropped here, removing "scan_type" from context
14}
15
16fn main() {
17    let _req_id = logger().add_context("request_id", "12345");
18
19    info!("Starting application"); // Includes request_id
20
21    scan_ports("example.com"); // Includes request_id and temporarily host
22
23    info!("Finished!"); // Only includes request_id again
24}
examples/filtering.rs (line 5)
3fn main() {
4    // Set minimum log level to WARN - this will hide DEBUG and INFO messages
5    logger().min_level(LogLevel::WARN);
6
7    debug!("This debug message won't show");
8    info!("This info message won't show");
9    warn!("This warning will show!");
10    error!("This error will show!");
11
12    // Change minimum level back to DEBUG to show everything
13    logger().min_level(LogLevel::DEBUG);
14
15    debug!("Now this debug message shows");
16    info!("And this info message too");
17}
examples/verbose.rs (line 5)
3fn main() {
4    // Enable verbose mode globally
5    logger().verbose(true);
6
7    info!("This is an info message");
8    let a = 1337;
9    info!("This is an info message with a variable: {}", a);
10    log!(
11        LogLevel::INFO,
12        "This is an info message with different syntactic sugar {}",
13        a
14    );
15    debug!("This is a debug message");
16    warn!("This is a warning message");
17    error!("This is an error message");
18    success!("This is a success message");
19    failure!("This is a failure message");
20}
examples/thread_safety.rs (line 7)
5fn main() {
6    // Enable verbose mode to see thread information
7    logger().verbose(true);
8
9    // Spawn multiple threads that log simultaneously
10    let mut handles = vec![];
11
12    for i in 0..3 {
13        let handle = thread::spawn(move || {
14            let _ctx = logger().add_context("thread_id", i.to_string());
15
16            for j in 0..3 {
17                info!("Message {} from thread {}", j, i);
18                thread::sleep(Duration::from_millis(100));
19            }
20        });
21
22        handles.push(handle);
23    }
24
25    // Wait for all threads to finish
26    for handle in handles {
27        handle.join().unwrap();
28    }
29}