-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathfile_log.rs
123 lines (104 loc) · 3.44 KB
/
file_log.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//! File Log Subscriber Settings.
use std::path::PathBuf;
/// Re-export to save the end crate from an extra import.
pub use tracing_appender::rolling::Rotation;
use super::{Settings, SettingsToggle};
/// Configure specific settings for the File Log subscriber.
#[derive(Debug, Default, PartialEq)]
pub enum FileLogSettings {
/// File Log subscriber disabled.
#[default]
Disabled,
/// File Log subscriber enabled.
Enabled {
/// Common subscriber settings that apply to the File Log Subscriber.
common_settings: Settings,
/// Path to directory for log files.
file_log_dir: PathBuf,
/// Log rotation frequency.
rotation_period: Rotation,
/// Suffix for log filenames.
filename_suffix: String,
/// Keep the last `n` files on disk.
max_log_files: Option<usize>,
},
}
impl SettingsToggle for FileLogSettings {
fn is_enabled(&self) -> bool {
match self {
FileLogSettings::Disabled => false,
FileLogSettings::Enabled { .. } => true,
}
}
}
/// For building [`FileLogSettings`].
///
/// <div class="warning">
/// Do not use directly, instead use the [`Settings::builder`] associated function.
/// </div>
pub struct FileLogSettingsBuilder {
pub(crate) common_settings: Settings,
pub(crate) file_log_dir: PathBuf,
pub(crate) rotation_period: Rotation,
pub(crate) filename_suffix: String,
pub(crate) max_log_files: Option<usize>,
}
impl FileLogSettingsBuilder {
/// Set file rotation period.
pub fn with_rotation_period(mut self, rotation_period: impl Into<Rotation>) -> Self {
self.rotation_period = rotation_period.into();
self
}
/// Set maximum number of log files to keep.
pub fn with_max_files(mut self, max_log_files: impl Into<Option<usize>>) -> Self {
self.max_log_files = max_log_files.into();
self
}
/// Consumes self and returns a valid [`FileLogSettings`] instance.
pub fn build(self) -> FileLogSettings {
FileLogSettings::Enabled {
common_settings: self.common_settings,
file_log_dir: self.file_log_dir,
rotation_period: self.rotation_period,
filename_suffix: self.filename_suffix,
max_log_files: self.max_log_files,
}
}
}
impl<T> From<Option<T>> for FileLogSettings
where
T: Into<FileLogSettings>,
{
fn from(settings: Option<T>) -> Self {
match settings {
Some(settings) => settings.into(),
None => FileLogSettings::default(),
}
}
}
#[cfg(test)]
mod test {
use tracing::level_filters::LevelFilter;
use super::*;
#[test]
fn builds_settings() {
let expected = FileLogSettings::Enabled {
common_settings: Settings {
environment_variable: "hello",
default_level: LevelFilter::DEBUG,
},
file_log_dir: PathBuf::from("/logs"),
rotation_period: Rotation::HOURLY,
filename_suffix: "tracing-rs.log".to_owned(),
max_log_files: Some(6),
};
let result = Settings::builder()
.with_environment_variable("hello")
.with_default_level(LevelFilter::DEBUG)
.file_log_settings_builder(PathBuf::from("/logs"), "tracing-rs.log")
.with_rotation_period(Rotation::HOURLY)
.with_max_files(6)
.build();
assert_eq!(expected, result);
}
}