Move log level to config file

This commit is contained in:
Andras Schmelczer 2025-12-07 15:06:08 +00:00
parent 8439bd8b92
commit 78a706ab8d
9 changed files with 47 additions and 26 deletions

11
sync-server/Cargo.lock generated
View file

@ -375,16 +375,6 @@ dependencies = [
"clap_derive",
]
[[package]]
name = "clap-verbosity-flag"
version = "3.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eeab6a5cdfc795a05538422012f20a5496f050223c91be4e5420bfd13c641fb1"
dependencies = [
"clap",
"log",
]
[[package]]
name = "clap_builder"
version = "4.5.38"
@ -2143,7 +2133,6 @@ dependencies = [
"bimap",
"chrono",
"clap",
"clap-verbosity-flag",
"futures",
"humantime-serde",
"log",

View file

@ -30,7 +30,6 @@ clap = { version = "4.5.38", features = ["derive"] }
futures = "0.3.31"
serde_yaml = "0.9.34"
serde_json = "1.0.140"
clap-verbosity-flag = "3.0.3"
bimap = "0.6.3"
ts-rs = { version = "10.1", features = ["uuid-impl", "chrono-impl"] }
base64 = "0.22.1"

View file

@ -30,3 +30,4 @@ users:
logging:
log_directory: logs
log_rotation: 7days
log_level: info

View file

@ -1,7 +1,6 @@
use std::ffi::OsString;
use clap::Parser;
use clap_verbosity_flag::{InfoLevel, Verbosity};
use crate::cli::color_when::ColorWhen;
@ -12,9 +11,6 @@ pub struct Args {
#[arg(index = 1)]
pub config_path: Option<OsString>,
#[command(flatten)]
pub verbose: Verbosity<InfoLevel>,
#[arg(
long,
value_name = "WHEN",

View file

@ -3,7 +3,10 @@ use std::time::Duration;
use log::debug;
use serde::{Deserialize, Serialize};
use crate::consts::{DEFAULT_LOG_DIRECTORY, DEFAULT_LOG_ROTATION_INTERVAL};
use crate::{
consts::{DEFAULT_LOG_DIRECTORY, DEFAULT_LOG_LEVEL, DEFAULT_LOG_ROTATION_INTERVAL},
utils::log_level::LogLevel,
};
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct LoggingConfig {
@ -12,6 +15,9 @@ pub struct LoggingConfig {
#[serde(default = "default_log_rotation", with = "humantime_serde")]
pub log_rotation: Duration,
#[serde(default = "default_log_level")]
pub log_level: LogLevel,
}
impl Default for LoggingConfig {
@ -19,6 +25,7 @@ impl Default for LoggingConfig {
Self {
log_directory: default_log_directory(),
log_rotation: default_log_rotation(),
log_level: default_log_level(),
}
}
}
@ -32,3 +39,8 @@ 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
}

View file

@ -1,5 +1,7 @@
use std::time::Duration;
use crate::utils::log_level::LogLevel;
pub const DEFAULT_CONFIG_PATH: &str = "config.yml";
pub const DEFAULT_DATABASES_DIRECTORY_PATH: &str = "databases";
@ -14,6 +16,7 @@ pub const DEFAULT_MAX_CLIENTS_PER_VAULT: usize = 256;
pub const DEFAULT_LOG_DIRECTORY: &str = "logs";
pub const DEFAULT_LOG_ROTATION_INTERVAL: Duration = Duration::from_secs(60 * 60 * 24); // 1 day
pub const DEFAULT_LOG_LEVEL: LogLevel = LogLevel::Info;
pub const DEFAULT_MERGEABLE_FILE_EXTENSIONS: &[&str] = &["md", "txt"];

View file

@ -60,14 +60,7 @@ fn set_up_logging(
args: &Args,
logging_config: &config::logging_config::LoggingConfig,
) -> Result<(), SyncServerError> {
let level_filter = match args.verbose.log_level_filter() {
// We don't want to allow disabling all logging
log::LevelFilter::Off | log::LevelFilter::Error => tracing::Level::ERROR,
log::LevelFilter::Warn => tracing::Level::WARN,
log::LevelFilter::Info => tracing::Level::INFO,
log::LevelFilter::Debug => tracing::Level::DEBUG,
log::LevelFilter::Trace => tracing::Level::TRACE,
};
let level_filter = logging_config.log_level.as_tracing_level();
let env_filter = EnvFilter::builder()
.with_default_directive(level_filter.into())
@ -77,7 +70,7 @@ fn set_up_logging(
let use_colors = args.color.use_colors();
let is_debug_mode = args.verbose.log_level_filter() >= log::LevelFilter::Debug;
let is_debug_mode = logging_config.log_level.is_debug_or_trace();
let file_appender = RotatingFileWriter::new(
&logging_config.log_directory,

View file

@ -2,6 +2,7 @@ pub mod dedup_paths;
pub mod find_first_available_path;
pub mod is_binary;
pub mod is_file_type_mergable;
pub mod log_level;
pub mod normalize;
pub mod rotating_file_writer;
pub mod sanitize_path;

View file

@ -0,0 +1,27 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum LogLevel {
Error,
Warn,
Info,
Debug,
Trace,
}
impl LogLevel {
pub fn as_tracing_level(self) -> tracing::Level {
match self {
Self::Error => tracing::Level::ERROR,
Self::Warn => tracing::Level::WARN,
Self::Info => tracing::Level::INFO,
Self::Debug => tracing::Level::DEBUG,
Self::Trace => tracing::Level::TRACE,
}
}
pub fn is_debug_or_trace(self) -> bool {
matches!(self, Self::Debug | Self::Trace)
}
}