pub fn set_default_logger(logger: Arc<Logger>)Expand description
Sets the given logger as the new global default logger.
ยงExamples
use spdlog::prelude::*;
spdlog::set_default_logger(new_logger);
info!("this log will be handled by `new_logger`");Examples found in repository?
examples/02-file.rs (line 23)
18fn configure_file_logger() -> Result<(), Box<dyn std::error::Error>> {
19 let path = env::current_exe()?.with_file_name("file.log");
20
21 let file_sink = FileSink::builder().path(path).build_arc()?;
22 let new_logger = Logger::builder().sink(file_sink).build_arc()?;
23 spdlog::set_default_logger(new_logger);
24
25 info!("this log will be written to the file `all.log`");
26
27 Ok(())
28}
29
30fn configure_rotating_daily_file_logger() -> Result<(), Box<dyn std::error::Error>> {
31 let path = env::current_exe()?.with_file_name("rotating_daily.log");
32
33 let file_sink = RotatingFileSink::builder()
34 .base_path(path)
35 .rotation_policy(RotationPolicy::Daily { hour: 0, minute: 0 })
36 .build_arc()?;
37 let new_logger = Logger::builder().sink(file_sink).build_arc()?;
38 spdlog::set_default_logger(new_logger);
39
40 info!("this log will be written to the file `rotating_daily.log`, and the file will be rotated daily at 00:00");
41
42 Ok(())
43}
44
45fn configure_rotating_size_file_logger() -> Result<(), Box<dyn std::error::Error>> {
46 let path = env::current_exe()?.with_file_name("rotating_size.log");
47
48 let file_sink = RotatingFileSink::builder()
49 .base_path(path)
50 .rotation_policy(RotationPolicy::FileSize(1024))
51 .build_arc()?;
52 let new_logger = Logger::builder().sink(file_sink).build_arc()?;
53 spdlog::set_default_logger(new_logger);
54
55 info!("this log will be written to the file `rotating_size.log`, and the file will be rotated when its size reaches 1024 bytes");
56
57 Ok(())
58}
59
60fn configure_rotating_hourly_file_logger() -> Result<(), Box<dyn std::error::Error>> {
61 let path = env::current_exe()?.with_file_name("rotating_hourly.log");
62
63 let file_sink = RotatingFileSink::builder()
64 .base_path(path)
65 .rotation_policy(RotationPolicy::Hourly)
66 .build_arc()?;
67 let new_logger = Logger::builder().sink(file_sink).build_arc()?;
68 spdlog::set_default_logger(new_logger);
69
70 info!("this log will be written to the file `rotating_hourly.log`, and the file will be rotated every hour");
71
72 Ok(())
73}
74
75fn configure_rotating_period_file_logger() -> Result<(), Box<dyn std::error::Error>> {
76 let path = env::current_exe()?.with_file_name("rotating_period.log");
77
78 let file_sink = RotatingFileSink::builder()
79 .base_path(path)
80 .rotation_policy(RotationPolicy::Period(Duration::from_secs(
81 60 * 90, // 90 minutes
82 )))
83 .build_arc()?;
84 let new_logger = Logger::builder().sink(file_sink).build_arc()?;
85 spdlog::set_default_logger(new_logger);
86
87 info!("this log will be written to the file `rotating_period.log`, and the file will be rotated every 1.5 hours");
88
89 Ok(())
90}More examples
examples/native/linux.rs (line 11)
2fn main() -> Result<(), Box<dyn std::error::Error>> {
3 use spdlog::{prelude::*, sink::JournaldSink};
4
5 let sink = JournaldSink::builder().build_arc()?;
6 let logger = spdlog::default_logger().fork_with(|logger| {
7 logger.set_name(Some("demo")).unwrap();
8 logger.sinks_mut().push(sink);
9 Ok(())
10 })?;
11 spdlog::set_default_logger(logger);
12
13 info!("info message from spdlog-rs's JournaldSink");
14 error!("error message from spdlog-rs's JournaldSink", kv: { error_code = 114514 });
15 Ok(())
16}examples/03-logger.rs (line 24)
8fn main() -> Result<(), Box<dyn std::error::Error>> {
9 // `spdlog-rs` has a global default logger and logs will be processed by it
10 // by default, You can configure it.
11 let default_logger = spdlog::default_logger();
12 default_logger.set_level_filter(LevelFilter::All);
13
14 // Or completely replace it with a new one.
15 let path = env::current_exe()?.with_file_name("all.log");
16 let file_sink = FileSink::builder().path(path).build_arc()?;
17
18 let new_logger = Logger::builder()
19 .level_filter(LevelFilter::All)
20 .flush_level_filter(LevelFilter::MoreSevereEqual(Level::Warn))
21 .sink(file_sink.clone())
22 .build_arc()?;
23 new_logger.set_flush_period(Some(Duration::from_secs(3)));
24 spdlog::set_default_logger(new_logger);
25
26 info!("this log will be written to the file `all.log`");
27
28 // In addition to having the global default logger, more loggers are allowed to
29 // be configured, stored and used independently.
30 let db = AppDatabase::new(file_sink)?;
31 db.write_i32(114514);
32
33 Ok(())
34}