tracing_logger_config/
config.rs1use 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#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize, Hash, Eq, PartialEq)]
63#[serde(rename_all = "camelCase")]
64pub enum RotationKind {
65 #[default]
67 Never,
68 Minutely,
70 Hourly,
72 Daily,
74}
75
76#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize, Hash, Eq, PartialEq)]
77#[serde(rename_all = "camelCase")]
78pub enum LevelInner {
79 Trace,
83 Debug,
87 #[default]
91 Info,
92 Warn,
96 Error,
100}
101
102impl fmt::Display for LevelInner {
103 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{:?}", &self) }
104}