vault-link/sync-server/src/config/logging_config.rs
Andras Schmelczer a9ce09b59d split: server foundation (Cargo, config, errors, utils, main)
Cargo.{toml,lock} bumps, build.rs, config-e2e.yml, rust-toolchain.toml,
src/config/* (database/logging/server/user configs), src/consts.rs,
src/errors.rs, src/main.rs, and src/utils/* (dedup_paths,
find_first_available_path, rotating_file_writer, sanitize_path).
2026-05-08 21:35:18 +01:00

63 lines
1.6 KiB
Rust

use std::time::Duration;
use anyhow::{Result, ensure};
use log::debug;
use serde::{Deserialize, Serialize};
use crate::{
consts::{
DEFAULT_LOG_DIRECTORY, DEFAULT_LOG_LEVEL, DEFAULT_LOG_ROTATION_INTERVAL, DURATION_ZERO,
},
utils::log_level::LogLevel,
};
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct LoggingConfig {
#[serde(default = "default_log_directory")]
pub log_directory: String,
#[serde(default = "default_log_rotation", with = "humantime_serde")]
pub log_rotation: Duration,
#[serde(default = "default_log_level")]
pub log_level: LogLevel,
}
impl LoggingConfig {
pub fn validate(&self) -> Result<()> {
ensure!(
!self.log_directory.is_empty(),
"log_directory must not be an empty string"
);
ensure!(
self.log_rotation > DURATION_ZERO,
"log_rotation must be greater than 0"
);
Ok(())
}
}
impl Default for LoggingConfig {
fn default() -> Self {
Self {
log_directory: default_log_directory(),
log_rotation: default_log_rotation(),
log_level: default_log_level(),
}
}
}
fn default_log_directory() -> String {
debug!("Using default log directory: `{DEFAULT_LOG_DIRECTORY}`");
DEFAULT_LOG_DIRECTORY.to_owned()
}
fn default_log_rotation() -> Duration {
debug!("Using default log rotation: {DEFAULT_LOG_ROTATION_INTERVAL:?}");
DEFAULT_LOG_ROTATION_INTERVAL
}
fn default_log_level() -> LogLevel {
debug!("Using default log level: Info");
DEFAULT_LOG_LEVEL
}