session_log/config.rs
1use crate::*;
2use once_cell::sync::Lazy;
3use std::sync::{Arc, Mutex};
4
5
6/// Configuration for the logger.
7#[derive(Debug, Clone)]
8pub struct Config {
9 /// The level that the logger will accept for writing to a file.
10 ///
11 /// Default: `Level::Info`
12 pub write_level: Level,
13
14 /// The level that the logger will accept for printing to the console.
15 ///
16 /// Default: `Level::Info`
17 pub print_level: Level,
18
19 /// The directory where the log files will be stored. The directory along with prefix will be used
20 /// as a key for getting the writer. If 2 logger having same `directory` and `file_prefix`, the writer
21 /// will be the same one regardless of if limits are different. This is to prevent multiple writers
22 /// working on the same file.
23 ///
24 /// Default: `./logs`
25 pub directory: String,
26
27 /// The prefix of the log file. The log file will be named as `{prefix} {date}_{time}.log`.
28 /// If the field is None, the log file will be named as `{date}_{time}.log`. Error of unable to
29 /// create the log file with the unsupported character in prefix is not handled.
30 ///
31 /// If one logger having no `prefix`, `size_limit` and `duration_limit` the file name will always
32 /// be `log.log`.
33 ///
34 /// Default: `None`
35 pub file_prefix: Option<String>,
36
37 /// The size limit in bytes of the log file. When the file size exceeds this limit, the file will
38 /// be rotated. If the 0 is set, the file will never be rotated by size.
39 ///
40 /// Default: `10 * 1024 * 1024`
41 pub size_limit: Option<u64>,
42
43 /// The duration limit in seconds of the log file. When the file duration exceeds this limit, the
44 /// file will be rotated. If the 0 is set, the file will never be rotated by duration.
45 pub duration_limit: Option<u64>,
46
47 /// The maximum size in bytes of one batch of logs. If the 0 is set, the logs will be written
48 /// immediately.
49 ///
50 /// Default: `1024`
51 #[cfg(feature = "batch")]
52 pub batch_size: u64,
53
54 /// The interval in milliseconds between each batch write. If the 0 is set, the value will be set
55 /// to 1 millisecond to automatically.
56 ///
57 /// Default: `100`
58 #[cfg(feature = "batch")]
59 pub batch_interval: u64,
60}
61
62
63static DEFAULT_CONFIG: Lazy<Arc<Mutex<Config>>> = Lazy::new(|| Arc::new(Mutex::new(Config {
64 write_level : Level::Info,
65 print_level : Level::Info,
66 directory : "./logs".to_string(),
67 file_prefix : None,
68 size_limit : Some(10 * 1024 * 1024),
69 duration_limit: Some(60 * 60),
70
71 #[cfg(feature = "batch")] batch_size : 1024,
72 #[cfg(feature = "batch")] batch_interval: 100,
73})));
74
75
76impl Default for Config {
77 fn default() -> Self {
78 let config = DEFAULT_CONFIG.lock().unwrap();
79
80 Config {
81 write_level : config.write_level,
82 print_level : config.print_level,
83 directory : config.directory.clone(),
84 file_prefix : config.file_prefix.clone(),
85 size_limit : config.size_limit,
86 duration_limit: config.duration_limit,
87
88 #[cfg(feature = "batch")] batch_size : config.batch_size,
89 #[cfg(feature = "batch")] batch_interval: config.batch_interval,
90 }
91 }
92}
93
94
95impl Config {
96 pub fn update_default(config: Config) {
97 let mut default = DEFAULT_CONFIG.lock().unwrap();
98 default.write_level = config.write_level;
99 default.print_level = config.print_level;
100 default.directory = config.directory;
101 default.file_prefix = config.file_prefix;
102 default.size_limit = config.size_limit;
103 default.duration_limit = config.duration_limit;
104
105 #[cfg(feature = "batch")] {
106 default.batch_size = config.batch_size;
107 default.batch_interval = config.batch_interval;
108 }
109 }
110}