tracing_logger_config/
config.rs

1use std::{fmt,
2          path::{Path, PathBuf}};
3
4#[derive(Clone, Serialize, Deserialize, Debug)]
5#[serde(rename_all = "camelCase")]
6pub struct Config {
7    pub log_path: Option<PathBuf>,
8    #[serde(skip_serializing_if = "Option::is_none")]
9    pub log_error_path: Option<PathBuf>,
10    #[serde(default)]
11    pub rotation: RotationKind,
12    pub level: Option<LevelInner>,
13}
14
15#[derive(Clone, Serialize, Deserialize, Debug)]
16#[serde(rename_all = "camelCase")]
17pub struct ExporterEndpoint {
18    pub port: u16,
19    pub host: String,
20}
21
22impl ExporterEndpoint {
23    pub fn get_host(&self) -> String { format!("{}:{}", self.host, self.port) }
24}
25
26impl Config {
27    pub fn log_path(&self) -> Option<LogPath> { self.log_path.as_ref().map(Self::build_path) }
28
29    pub fn log_error_path(&self) -> Option<LogPath> {
30        self.log_error_path.as_ref().map(Self::build_path)
31    }
32
33    fn build_path(path: &PathBuf) -> LogPath {
34        let directory = path.parent().unwrap_or_else(|| Path::new(""));
35        let file_name = path.file_name().unwrap_or(path.as_os_str());
36        LogPath {
37            directory: directory.display().to_string(),
38            filename: file_name.to_string_lossy().to_string(),
39        }
40    }
41}
42
43impl Default for Config {
44    fn default() -> Self {
45        Self {
46            log_error_path: None,
47            log_path: None,
48            rotation: RotationKind::default(),
49            level: None,
50        }
51    }
52}
53
54#[derive(Clone, Serialize, Deserialize, Debug)]
55#[serde(rename_all = "camelCase")]
56pub struct LogPath {
57    pub directory: String,
58    pub filename: String,
59}
60
61/// Defines a fixed period for rolling of a log file.
62#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize, Hash, Eq, PartialEq)]
63#[serde(rename_all = "camelCase")]
64pub enum RotationKind {
65    /// Provides a rotation that never rotates.
66    #[default]
67    Never,
68    /// Provides an minutely rotation.
69    Minutely,
70    /// Provides an hourly rotation.
71    Hourly,
72    /// Provides a daily rotation.
73    Daily,
74}
75
76#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize, Hash, Eq, PartialEq)]
77#[serde(rename_all = "camelCase")]
78pub enum LevelInner {
79    /// The "trace" level.
80    ///
81    /// Designates very low priority, often extremely verbose, information.
82    Trace,
83    /// The "debug" level.
84    ///
85    /// Designates lower priority information.
86    Debug,
87    /// The "info" level.
88    ///
89    /// Designates useful information.
90    #[default]
91    Info,
92    /// The "warn" level.
93    ///
94    /// Designates hazardous situations.
95    Warn,
96    /// The "error" level.
97    ///
98    /// Designates very serious errors.
99    Error,
100}
101
102impl fmt::Display for LevelInner {
103    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{:?}", &self) }
104}