Logger

Struct Logger 

Source
pub struct Logger { /* private fields */ }

Implementations§

Source§

impl Logger

Source

pub fn new() -> Logger

Initializes the global logger with an RLogger instance with default log level set to Level::Trace.

use rslogger::Logger;
Logger::new().init().unwrap();
log::warn!("This is an example message.");
Examples found in repository?
examples/stdout/single-thread/utc-timestamps.rs (line 5)
4fn main() {
5    Logger::new()
6        .with_level(log::LevelFilter::Trace)
7        .with_utc_timestamps()
8        .add_writer_stdout(false, None)
9        .init().unwrap();
10
11    info!("This is a info test");
12    warn!("This is a warn test");
13    error!("This is an error test");
14
15
16    log::logger().flush();
17}
More examples
Hide additional examples
examples/stdout/single-thread/local-timestamps.rs (line 5)
4fn main() {
5    Logger::new()
6        .with_level(log::LevelFilter::Trace)
7        .with_local_timestamps()
8        .add_writer_stdout(false, None)
9        .init().unwrap();
10
11    info!("This is a info test");
12    warn!("This is a warn test");
13    error!("This is an error test");
14
15
16    log::logger().flush();
17}
examples/stdout/single-thread/with-target.rs (line 5)
4fn main() {
5    Logger::new()
6        .with_level(log::LevelFilter::Trace)
7        .with_utc_timestamps()
8        .with_target()
9        .add_writer_stdout(false, None)
10        .init().unwrap();
11
12    info!("This is a info test");
13    warn!("This is a warn test");
14    error!("This is an error test");
15
16
17    log::logger().flush();
18}
examples/stdout/separate-thread/big-buf-capacity.rs (line 5)
4fn main() {
5    Logger::new()
6        .with_level(log::LevelFilter::Trace)
7        .with_local_timestamps()
8        .add_writer_stdout(true, Some(1000))
9        .init().unwrap();
10
11    info!("This is a info test");
12    warn!("This is a warn test");
13    error!("This is an error test");
14
15    // Note: This method must be called only at the end of the program. After that, you can no longer log.
16    log::logger().flush();
17
18    // This would panic
19    // info!("Test");
20}
examples/stdout/single-thread/big-buf-capacity.rs (line 5)
4fn main() {
5    Logger::new()
6        .with_level(log::LevelFilter::Trace)
7        .with_local_timestamps()
8        .add_writer_stdout(false, Some(1000))
9        .init().unwrap();
10
11    info!("This is a info test");
12    warn!("This is a warn test");
13    error!("This is an error test");
14
15    // In this case, if we remove this line, we would not see the traces
16    // You can try it.
17    // This is because the flush method is not called automatically
18    log::logger().flush();
19}
examples/stdout/separate-thread/thread-ids.rs (line 7)
6fn main() {
7    Logger::new()
8        .with_level(log::LevelFilter::Trace)
9        .with_local_timestamps()
10        .with_thread()
11        .add_writer_stdout(false, None)
12        .init().unwrap();
13
14    let handler1 = thread::Builder::new().spawn(move | | {
15        for i in 0..10 {
16            info!("This is a info, i: {i}");
17        }
18    }).unwrap();
19
20    let handler2 = thread::Builder::new().spawn(move | | {
21        for i in 0..10 {
22            info!("This is a info, i: {i}");
23        }
24    }).unwrap();
25    
26    warn!("Warning on main!");
27
28    handler1.join().unwrap();
29    handler2.join().unwrap();
30
31    log::logger().flush();
32}
Source

pub fn with_level(self, level: LevelFilter) -> Logger

Sets the global log level of the logger.

Examples found in repository?
examples/stdout/single-thread/utc-timestamps.rs (line 6)
4fn main() {
5    Logger::new()
6        .with_level(log::LevelFilter::Trace)
7        .with_utc_timestamps()
8        .add_writer_stdout(false, None)
9        .init().unwrap();
10
11    info!("This is a info test");
12    warn!("This is a warn test");
13    error!("This is an error test");
14
15
16    log::logger().flush();
17}
More examples
Hide additional examples
examples/stdout/single-thread/local-timestamps.rs (line 6)
4fn main() {
5    Logger::new()
6        .with_level(log::LevelFilter::Trace)
7        .with_local_timestamps()
8        .add_writer_stdout(false, None)
9        .init().unwrap();
10
11    info!("This is a info test");
12    warn!("This is a warn test");
13    error!("This is an error test");
14
15
16    log::logger().flush();
17}
examples/stdout/single-thread/with-target.rs (line 6)
4fn main() {
5    Logger::new()
6        .with_level(log::LevelFilter::Trace)
7        .with_utc_timestamps()
8        .with_target()
9        .add_writer_stdout(false, None)
10        .init().unwrap();
11
12    info!("This is a info test");
13    warn!("This is a warn test");
14    error!("This is an error test");
15
16
17    log::logger().flush();
18}
examples/stdout/separate-thread/big-buf-capacity.rs (line 6)
4fn main() {
5    Logger::new()
6        .with_level(log::LevelFilter::Trace)
7        .with_local_timestamps()
8        .add_writer_stdout(true, Some(1000))
9        .init().unwrap();
10
11    info!("This is a info test");
12    warn!("This is a warn test");
13    error!("This is an error test");
14
15    // Note: This method must be called only at the end of the program. After that, you can no longer log.
16    log::logger().flush();
17
18    // This would panic
19    // info!("Test");
20}
examples/stdout/single-thread/big-buf-capacity.rs (line 6)
4fn main() {
5    Logger::new()
6        .with_level(log::LevelFilter::Trace)
7        .with_local_timestamps()
8        .add_writer_stdout(false, Some(1000))
9        .init().unwrap();
10
11    info!("This is a info test");
12    warn!("This is a warn test");
13    error!("This is an error test");
14
15    // In this case, if we remove this line, we would not see the traces
16    // You can try it.
17    // This is because the flush method is not called automatically
18    log::logger().flush();
19}
examples/stdout/separate-thread/thread-ids.rs (line 8)
6fn main() {
7    Logger::new()
8        .with_level(log::LevelFilter::Trace)
9        .with_local_timestamps()
10        .with_thread()
11        .add_writer_stdout(false, None)
12        .init().unwrap();
13
14    let handler1 = thread::Builder::new().spawn(move | | {
15        for i in 0..10 {
16            info!("This is a info, i: {i}");
17        }
18    }).unwrap();
19
20    let handler2 = thread::Builder::new().spawn(move | | {
21        for i in 0..10 {
22            info!("This is a info, i: {i}");
23        }
24    }).unwrap();
25    
26    warn!("Warning on main!");
27
28    handler1.join().unwrap();
29    handler2.join().unwrap();
30
31    log::logger().flush();
32}
Source

pub fn with_utc_timestamps(self) -> Logger

Display timestamps in UTC time

Examples found in repository?
examples/stdout/single-thread/utc-timestamps.rs (line 7)
4fn main() {
5    Logger::new()
6        .with_level(log::LevelFilter::Trace)
7        .with_utc_timestamps()
8        .add_writer_stdout(false, None)
9        .init().unwrap();
10
11    info!("This is a info test");
12    warn!("This is a warn test");
13    error!("This is an error test");
14
15
16    log::logger().flush();
17}
More examples
Hide additional examples
examples/stdout/single-thread/with-target.rs (line 7)
4fn main() {
5    Logger::new()
6        .with_level(log::LevelFilter::Trace)
7        .with_utc_timestamps()
8        .with_target()
9        .add_writer_stdout(false, None)
10        .init().unwrap();
11
12    info!("This is a info test");
13    warn!("This is a warn test");
14    error!("This is an error test");
15
16
17    log::logger().flush();
18}
Source

pub fn with_local_timestamps(self) -> Logger

Display timestamps in Local time

Examples found in repository?
examples/stdout/single-thread/local-timestamps.rs (line 7)
4fn main() {
5    Logger::new()
6        .with_level(log::LevelFilter::Trace)
7        .with_local_timestamps()
8        .add_writer_stdout(false, None)
9        .init().unwrap();
10
11    info!("This is a info test");
12    warn!("This is a warn test");
13    error!("This is an error test");
14
15
16    log::logger().flush();
17}
More examples
Hide additional examples
examples/stdout/separate-thread/big-buf-capacity.rs (line 7)
4fn main() {
5    Logger::new()
6        .with_level(log::LevelFilter::Trace)
7        .with_local_timestamps()
8        .add_writer_stdout(true, Some(1000))
9        .init().unwrap();
10
11    info!("This is a info test");
12    warn!("This is a warn test");
13    error!("This is an error test");
14
15    // Note: This method must be called only at the end of the program. After that, you can no longer log.
16    log::logger().flush();
17
18    // This would panic
19    // info!("Test");
20}
examples/stdout/single-thread/big-buf-capacity.rs (line 7)
4fn main() {
5    Logger::new()
6        .with_level(log::LevelFilter::Trace)
7        .with_local_timestamps()
8        .add_writer_stdout(false, Some(1000))
9        .init().unwrap();
10
11    info!("This is a info test");
12    warn!("This is a warn test");
13    error!("This is an error test");
14
15    // In this case, if we remove this line, we would not see the traces
16    // You can try it.
17    // This is because the flush method is not called automatically
18    log::logger().flush();
19}
examples/stdout/separate-thread/thread-ids.rs (line 9)
6fn main() {
7    Logger::new()
8        .with_level(log::LevelFilter::Trace)
9        .with_local_timestamps()
10        .with_thread()
11        .add_writer_stdout(false, None)
12        .init().unwrap();
13
14    let handler1 = thread::Builder::new().spawn(move | | {
15        for i in 0..10 {
16            info!("This is a info, i: {i}");
17        }
18    }).unwrap();
19
20    let handler2 = thread::Builder::new().spawn(move | | {
21        for i in 0..10 {
22            info!("This is a info, i: {i}");
23        }
24    }).unwrap();
25    
26    warn!("Warning on main!");
27
28    handler1.join().unwrap();
29    handler2.join().unwrap();
30
31    log::logger().flush();
32}
examples/stdout/separate-thread/no-thread-names.rs (line 9)
6fn main() {
7    Logger::new()
8        .with_level(log::LevelFilter::Trace)
9        .with_local_timestamps()
10        .add_writer_stdout(false, None)
11        .init().unwrap();
12
13    let handler1 = thread::Builder::new().name("Thread 1".to_string()).spawn(move | | {
14        for i in 0..10 {
15            info!("This is a info, i: {i}");
16        }
17    }).unwrap();
18
19    let handler2 = thread::Builder::new().name("Thread 2".to_string()).spawn(move | | {
20        for i in 0..10 {
21            info!("This is a info, i: {i}");
22        }
23    }).unwrap();
24    
25    warn!("Warning on main!");
26
27    handler1.join().unwrap();
28    handler2.join().unwrap();
29
30    log::logger().flush();
31}
examples/stdout/separate-thread/thread-names.rs (line 9)
6fn main() {
7    Logger::new()
8        .with_level(log::LevelFilter::Trace)
9        .with_local_timestamps()
10        .with_thread()
11        .add_writer_stdout(false, None)
12        .init().unwrap();
13
14    let handler1 = thread::Builder::new().name("Thread 1".to_string()).spawn(move | | {
15        for i in 0..10 {
16            info!("This is a info, i: {i}");
17        }
18    }).unwrap();
19
20    let handler2 = thread::Builder::new().name("Thread 2".to_string()).spawn(move | | {
21        for i in 0..10 {
22            info!("This is a info, i: {i}");
23        }
24    }).unwrap();
25    
26    warn!("Warning on main!");
27
28    handler1.join().unwrap();
29    handler2.join().unwrap();
30
31    log::logger().flush();
32}
Source

pub fn without_timestamps(self) -> Logger

Don’t display timestamps

Source

pub fn with_thread(self) -> Logger

Display thread Id

Examples found in repository?
examples/stdout/separate-thread/thread-ids.rs (line 10)
6fn main() {
7    Logger::new()
8        .with_level(log::LevelFilter::Trace)
9        .with_local_timestamps()
10        .with_thread()
11        .add_writer_stdout(false, None)
12        .init().unwrap();
13
14    let handler1 = thread::Builder::new().spawn(move | | {
15        for i in 0..10 {
16            info!("This is a info, i: {i}");
17        }
18    }).unwrap();
19
20    let handler2 = thread::Builder::new().spawn(move | | {
21        for i in 0..10 {
22            info!("This is a info, i: {i}");
23        }
24    }).unwrap();
25    
26    warn!("Warning on main!");
27
28    handler1.join().unwrap();
29    handler2.join().unwrap();
30
31    log::logger().flush();
32}
More examples
Hide additional examples
examples/stdout/separate-thread/thread-names.rs (line 10)
6fn main() {
7    Logger::new()
8        .with_level(log::LevelFilter::Trace)
9        .with_local_timestamps()
10        .with_thread()
11        .add_writer_stdout(false, None)
12        .init().unwrap();
13
14    let handler1 = thread::Builder::new().name("Thread 1".to_string()).spawn(move | | {
15        for i in 0..10 {
16            info!("This is a info, i: {i}");
17        }
18    }).unwrap();
19
20    let handler2 = thread::Builder::new().name("Thread 2".to_string()).spawn(move | | {
21        for i in 0..10 {
22            info!("This is a info, i: {i}");
23        }
24    }).unwrap();
25    
26    warn!("Warning on main!");
27
28    handler1.join().unwrap();
29    handler2.join().unwrap();
30
31    log::logger().flush();
32}
examples/file/single-thread/two-threads.rs (line 13)
9fn main() {
10    Logger::new()
11        .with_level(log::LevelFilter::Trace)
12        .with_local_timestamps()
13        .with_thread()
14        .add_writer_file(
15            PathBuf::from("./LOGS/two-threads.log"), 
16            false, None)
17        .init().unwrap();
18
19    let handler1 = thread::Builder::new().name("Thread 1".to_string()).spawn(move | | {
20        for i in 0..10 {
21            info!("This is a info, i: {i}");
22        }
23    }).unwrap();
24
25    let handler2 = thread::Builder::new().name("Thread 2".to_string()).spawn(move | | {
26        for i in 0..10 {
27            info!("This is a info, i: {i}");
28        }
29    }).unwrap();
30    
31    warn!("Warning on main!");
32
33    handler1.join().unwrap();
34    handler2.join().unwrap();
35
36    log::logger().flush();
37}
examples/file_and_stdout/generic.rs (line 10)
6fn main() {
7    Logger::new()
8        .with_level(log::LevelFilter::Trace)
9        .with_local_timestamps()
10        .with_thread()
11        .add_writer_stdout(true, Some(1000))
12        .add_writer_file(PathBuf::from("./LOGS/genercs_1.log"), true, Some(10))
13        .add_writer_file(PathBuf::from("./LOGS/genercs_2.log"), true, Some(10000))
14        .add_writer_file(PathBuf::from("./LOGS/genercs_3.log"), true, None)
15        .init().unwrap();
16
17    info!("This is a info test");
18    warn!("This is a warn test");
19    error!("This is an error test");
20
21    let handler1 = thread::Builder::new().name("Thread 1".to_string()).spawn(move | | {
22        for i in 0..10 {
23            info!("This is a info, i: {i}");
24        }
25    }).unwrap();
26
27    let handler2 = thread::Builder::new().name("Thread 2".to_string()).spawn(move | | {
28        for i in 0..10 {
29            info!("This is a info, i: {i}");
30        }
31    }).unwrap();
32    
33    warn!("Warning on main!");
34
35    handler1.join().unwrap();
36    handler2.join().unwrap();
37
38    // Note: This method must be called only at the end of the program. After that, you can no longer log.
39    log::logger().flush();
40
41    // This would panic
42    // info!("Test");
43}
examples/file_and_stdout/different-levels.rs (line 10)
6fn main() {
7    Logger::new()
8        .with_level(log::LevelFilter::Trace)
9        .with_local_timestamps()
10        .with_thread()
11        .add_writer_stdout(true, Some(1000))
12        // This will trace only error or less
13        .add_writer_file_with_level(PathBuf::from("./LOGS/genercs_1.log"), true, Some(10), log::LevelFilter::Error)
14        // This will trace only info or less
15        .add_writer_file_with_level(PathBuf::from("./LOGS/genercs_2.log"), true, Some(10000), log::LevelFilter::Info)
16        // This will trace only trace or less (default)
17        .add_writer_file(PathBuf::from("./LOGS/genercs_3.log"), true, None)
18        .add_writer_stdout(false, None)
19        .init().unwrap();
20
21    let handler1 = thread::Builder::new().spawn(move | | {
22        for i in 0..10 {
23            info!("This is a info, i: {i}");
24        }
25    }).unwrap();
26
27    let handler2 = thread::Builder::new().spawn(move | | {
28        for i in 0..10 {
29            info!("This is a info, i: {i}");
30        }
31    }).unwrap();
32    
33    warn!("Warning on main!");
34    error!("This is traced on genercs_1.log");
35    info!("This is traced on genercs_2.log but not on genercs_1.log");
36    trace!("This is traced on genercs_3.log but not on genercs_1/2.log");
37
38    handler1.join().unwrap();
39    handler2.join().unwrap();
40
41    log::logger().flush();
42}
Source

pub fn with_target(self) -> Logger

Displays the module name that is logging

Examples found in repository?
examples/stdout/single-thread/with-target.rs (line 8)
4fn main() {
5    Logger::new()
6        .with_level(log::LevelFilter::Trace)
7        .with_utc_timestamps()
8        .with_target()
9        .add_writer_stdout(false, None)
10        .init().unwrap();
11
12    info!("This is a info test");
13    warn!("This is a warn test");
14    error!("This is an error test");
15
16
17    log::logger().flush();
18}
Source

pub fn without_target(self) -> Logger

Hides the module name that is logging

Source

pub fn add_writer_stdout( self, multi_thread: bool, capacity: Option<usize>, ) -> Logger

Adds a stdout writer.

§Param
  • multi_thread - If set to true, the writer will be multi thread, otherwise single thread
  • capacity - If Some(capacity), specified the buffer capacity of the writer. If None, initializes it with the default capacity.
Examples found in repository?
examples/stdout/single-thread/utc-timestamps.rs (line 8)
4fn main() {
5    Logger::new()
6        .with_level(log::LevelFilter::Trace)
7        .with_utc_timestamps()
8        .add_writer_stdout(false, None)
9        .init().unwrap();
10
11    info!("This is a info test");
12    warn!("This is a warn test");
13    error!("This is an error test");
14
15
16    log::logger().flush();
17}
More examples
Hide additional examples
examples/stdout/single-thread/local-timestamps.rs (line 8)
4fn main() {
5    Logger::new()
6        .with_level(log::LevelFilter::Trace)
7        .with_local_timestamps()
8        .add_writer_stdout(false, None)
9        .init().unwrap();
10
11    info!("This is a info test");
12    warn!("This is a warn test");
13    error!("This is an error test");
14
15
16    log::logger().flush();
17}
examples/stdout/single-thread/with-target.rs (line 9)
4fn main() {
5    Logger::new()
6        .with_level(log::LevelFilter::Trace)
7        .with_utc_timestamps()
8        .with_target()
9        .add_writer_stdout(false, None)
10        .init().unwrap();
11
12    info!("This is a info test");
13    warn!("This is a warn test");
14    error!("This is an error test");
15
16
17    log::logger().flush();
18}
examples/stdout/separate-thread/big-buf-capacity.rs (line 8)
4fn main() {
5    Logger::new()
6        .with_level(log::LevelFilter::Trace)
7        .with_local_timestamps()
8        .add_writer_stdout(true, Some(1000))
9        .init().unwrap();
10
11    info!("This is a info test");
12    warn!("This is a warn test");
13    error!("This is an error test");
14
15    // Note: This method must be called only at the end of the program. After that, you can no longer log.
16    log::logger().flush();
17
18    // This would panic
19    // info!("Test");
20}
examples/stdout/single-thread/big-buf-capacity.rs (line 8)
4fn main() {
5    Logger::new()
6        .with_level(log::LevelFilter::Trace)
7        .with_local_timestamps()
8        .add_writer_stdout(false, Some(1000))
9        .init().unwrap();
10
11    info!("This is a info test");
12    warn!("This is a warn test");
13    error!("This is an error test");
14
15    // In this case, if we remove this line, we would not see the traces
16    // You can try it.
17    // This is because the flush method is not called automatically
18    log::logger().flush();
19}
examples/stdout/separate-thread/thread-ids.rs (line 11)
6fn main() {
7    Logger::new()
8        .with_level(log::LevelFilter::Trace)
9        .with_local_timestamps()
10        .with_thread()
11        .add_writer_stdout(false, None)
12        .init().unwrap();
13
14    let handler1 = thread::Builder::new().spawn(move | | {
15        for i in 0..10 {
16            info!("This is a info, i: {i}");
17        }
18    }).unwrap();
19
20    let handler2 = thread::Builder::new().spawn(move | | {
21        for i in 0..10 {
22            info!("This is a info, i: {i}");
23        }
24    }).unwrap();
25    
26    warn!("Warning on main!");
27
28    handler1.join().unwrap();
29    handler2.join().unwrap();
30
31    log::logger().flush();
32}
Source

pub fn add_writer_stdout_with_level( self, multi_thread: bool, capacity: Option<usize>, level: LevelFilter, ) -> Logger

Adds a stdout writer.

§Param
  • multi_thread - If set to true, the writer will be multi thread, otherwise single thread
  • capacity - If Some(capacity), specified the buffer capacity of the writer. If None, initializes it with the default capacity.
  • level - Max level for this tracer.
Source

pub fn add_writer_file( self, file_path: PathBuf, multi_thread: bool, capacity: Option<usize>, ) -> Logger

Adds a file writer.

§Param
  • file_path - The path of the file to write on.
  • multi_thread - If set to true, the writer will be multi thread, otherwise single thread
  • capacity - If Some(capacity), specified the buffer capacity of the writer. If None, initializes it with the default capacity.
  • level - Max level for this tracer.
Examples found in repository?
examples/file/single-thread/two-threads.rs (lines 14-16)
9fn main() {
10    Logger::new()
11        .with_level(log::LevelFilter::Trace)
12        .with_local_timestamps()
13        .with_thread()
14        .add_writer_file(
15            PathBuf::from("./LOGS/two-threads.log"), 
16            false, None)
17        .init().unwrap();
18
19    let handler1 = thread::Builder::new().name("Thread 1".to_string()).spawn(move | | {
20        for i in 0..10 {
21            info!("This is a info, i: {i}");
22        }
23    }).unwrap();
24
25    let handler2 = thread::Builder::new().name("Thread 2".to_string()).spawn(move | | {
26        for i in 0..10 {
27            info!("This is a info, i: {i}");
28        }
29    }).unwrap();
30    
31    warn!("Warning on main!");
32
33    handler1.join().unwrap();
34    handler2.join().unwrap();
35
36    log::logger().flush();
37}
More examples
Hide additional examples
examples/file_and_stdout/generic.rs (line 12)
6fn main() {
7    Logger::new()
8        .with_level(log::LevelFilter::Trace)
9        .with_local_timestamps()
10        .with_thread()
11        .add_writer_stdout(true, Some(1000))
12        .add_writer_file(PathBuf::from("./LOGS/genercs_1.log"), true, Some(10))
13        .add_writer_file(PathBuf::from("./LOGS/genercs_2.log"), true, Some(10000))
14        .add_writer_file(PathBuf::from("./LOGS/genercs_3.log"), true, None)
15        .init().unwrap();
16
17    info!("This is a info test");
18    warn!("This is a warn test");
19    error!("This is an error test");
20
21    let handler1 = thread::Builder::new().name("Thread 1".to_string()).spawn(move | | {
22        for i in 0..10 {
23            info!("This is a info, i: {i}");
24        }
25    }).unwrap();
26
27    let handler2 = thread::Builder::new().name("Thread 2".to_string()).spawn(move | | {
28        for i in 0..10 {
29            info!("This is a info, i: {i}");
30        }
31    }).unwrap();
32    
33    warn!("Warning on main!");
34
35    handler1.join().unwrap();
36    handler2.join().unwrap();
37
38    // Note: This method must be called only at the end of the program. After that, you can no longer log.
39    log::logger().flush();
40
41    // This would panic
42    // info!("Test");
43}
examples/file_and_stdout/different-levels.rs (line 17)
6fn main() {
7    Logger::new()
8        .with_level(log::LevelFilter::Trace)
9        .with_local_timestamps()
10        .with_thread()
11        .add_writer_stdout(true, Some(1000))
12        // This will trace only error or less
13        .add_writer_file_with_level(PathBuf::from("./LOGS/genercs_1.log"), true, Some(10), log::LevelFilter::Error)
14        // This will trace only info or less
15        .add_writer_file_with_level(PathBuf::from("./LOGS/genercs_2.log"), true, Some(10000), log::LevelFilter::Info)
16        // This will trace only trace or less (default)
17        .add_writer_file(PathBuf::from("./LOGS/genercs_3.log"), true, None)
18        .add_writer_stdout(false, None)
19        .init().unwrap();
20
21    let handler1 = thread::Builder::new().spawn(move | | {
22        for i in 0..10 {
23            info!("This is a info, i: {i}");
24        }
25    }).unwrap();
26
27    let handler2 = thread::Builder::new().spawn(move | | {
28        for i in 0..10 {
29            info!("This is a info, i: {i}");
30        }
31    }).unwrap();
32    
33    warn!("Warning on main!");
34    error!("This is traced on genercs_1.log");
35    info!("This is traced on genercs_2.log but not on genercs_1.log");
36    trace!("This is traced on genercs_3.log but not on genercs_1/2.log");
37
38    handler1.join().unwrap();
39    handler2.join().unwrap();
40
41    log::logger().flush();
42}
Source

pub fn add_writer_file_with_level( self, file_path: PathBuf, multi_thread: bool, capacity: Option<usize>, level: LevelFilter, ) -> Logger

Adds a file writer.

§Param
  • file_path - The path of the file to write on.
  • multi_thread - If set to true, the writer will be multi thread, otherwise single thread
  • capacity - If Some(capacity), specified the buffer capacity of the writer. If None, initializes it with the default capacity.
Examples found in repository?
examples/file_and_stdout/different-levels.rs (line 13)
6fn main() {
7    Logger::new()
8        .with_level(log::LevelFilter::Trace)
9        .with_local_timestamps()
10        .with_thread()
11        .add_writer_stdout(true, Some(1000))
12        // This will trace only error or less
13        .add_writer_file_with_level(PathBuf::from("./LOGS/genercs_1.log"), true, Some(10), log::LevelFilter::Error)
14        // This will trace only info or less
15        .add_writer_file_with_level(PathBuf::from("./LOGS/genercs_2.log"), true, Some(10000), log::LevelFilter::Info)
16        // This will trace only trace or less (default)
17        .add_writer_file(PathBuf::from("./LOGS/genercs_3.log"), true, None)
18        .add_writer_stdout(false, None)
19        .init().unwrap();
20
21    let handler1 = thread::Builder::new().spawn(move | | {
22        for i in 0..10 {
23            info!("This is a info, i: {i}");
24        }
25    }).unwrap();
26
27    let handler2 = thread::Builder::new().spawn(move | | {
28        for i in 0..10 {
29            info!("This is a info, i: {i}");
30        }
31    }).unwrap();
32    
33    warn!("Warning on main!");
34    error!("This is traced on genercs_1.log");
35    info!("This is traced on genercs_2.log but not on genercs_1.log");
36    trace!("This is traced on genercs_3.log but not on genercs_1/2.log");
37
38    handler1.join().unwrap();
39    handler2.join().unwrap();
40
41    log::logger().flush();
42}
Source

pub fn init(self) -> Result<(), SetLoggerError>

Examples found in repository?
examples/stdout/single-thread/utc-timestamps.rs (line 9)
4fn main() {
5    Logger::new()
6        .with_level(log::LevelFilter::Trace)
7        .with_utc_timestamps()
8        .add_writer_stdout(false, None)
9        .init().unwrap();
10
11    info!("This is a info test");
12    warn!("This is a warn test");
13    error!("This is an error test");
14
15
16    log::logger().flush();
17}
More examples
Hide additional examples
examples/stdout/single-thread/local-timestamps.rs (line 9)
4fn main() {
5    Logger::new()
6        .with_level(log::LevelFilter::Trace)
7        .with_local_timestamps()
8        .add_writer_stdout(false, None)
9        .init().unwrap();
10
11    info!("This is a info test");
12    warn!("This is a warn test");
13    error!("This is an error test");
14
15
16    log::logger().flush();
17}
examples/stdout/single-thread/with-target.rs (line 10)
4fn main() {
5    Logger::new()
6        .with_level(log::LevelFilter::Trace)
7        .with_utc_timestamps()
8        .with_target()
9        .add_writer_stdout(false, None)
10        .init().unwrap();
11
12    info!("This is a info test");
13    warn!("This is a warn test");
14    error!("This is an error test");
15
16
17    log::logger().flush();
18}
examples/stdout/separate-thread/big-buf-capacity.rs (line 9)
4fn main() {
5    Logger::new()
6        .with_level(log::LevelFilter::Trace)
7        .with_local_timestamps()
8        .add_writer_stdout(true, Some(1000))
9        .init().unwrap();
10
11    info!("This is a info test");
12    warn!("This is a warn test");
13    error!("This is an error test");
14
15    // Note: This method must be called only at the end of the program. After that, you can no longer log.
16    log::logger().flush();
17
18    // This would panic
19    // info!("Test");
20}
examples/stdout/single-thread/big-buf-capacity.rs (line 9)
4fn main() {
5    Logger::new()
6        .with_level(log::LevelFilter::Trace)
7        .with_local_timestamps()
8        .add_writer_stdout(false, Some(1000))
9        .init().unwrap();
10
11    info!("This is a info test");
12    warn!("This is a warn test");
13    error!("This is an error test");
14
15    // In this case, if we remove this line, we would not see the traces
16    // You can try it.
17    // This is because the flush method is not called automatically
18    log::logger().flush();
19}
examples/stdout/separate-thread/thread-ids.rs (line 12)
6fn main() {
7    Logger::new()
8        .with_level(log::LevelFilter::Trace)
9        .with_local_timestamps()
10        .with_thread()
11        .add_writer_stdout(false, None)
12        .init().unwrap();
13
14    let handler1 = thread::Builder::new().spawn(move | | {
15        for i in 0..10 {
16            info!("This is a info, i: {i}");
17        }
18    }).unwrap();
19
20    let handler2 = thread::Builder::new().spawn(move | | {
21        for i in 0..10 {
22            info!("This is a info, i: {i}");
23        }
24    }).unwrap();
25    
26    warn!("Warning on main!");
27
28    handler1.join().unwrap();
29    handler2.join().unwrap();
30
31    log::logger().flush();
32}
Source

pub fn log_level(&self) -> LevelFilter

Trait Implementations§

Source§

impl Default for Logger

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Log for Logger

Source§

fn flush(&self)

Flushes to ensure that all possible buffered data are logged. This method should be called right before closing the program or when you don’t need to log anything else. This is because in case of separate thread, the logger thread will be stopped.

§Example
use rslogger::Logger;
use log::{info, warn, error};
Logger::new()
    .with_level(log::LevelFilter::Trace)
    .with_local_timestamps()
    .add_writer_stdout(true, Some(1000))
    .init().unwrap();
info!("This is a info test");
warn!("This is a warn test");
error!("This is an error test");
log::logger().flush();
let result = std::panic::catch_unwind(|| info!("Test"));
assert!(result.is_err())
Source§

fn enabled(&self, metadata: &Metadata<'_>) -> bool

Determines if a log message with the specified metadata would be logged. Read more
Source§

fn log(&self, record: &Record<'_>)

Logs the Record. Read more

Auto Trait Implementations§

§

impl Freeze for Logger

§

impl RefUnwindSafe for Logger

§

impl Send for Logger

§

impl Sync for Logger

§

impl Unpin for Logger

§

impl UnwindSafe for Logger

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.