diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 512c955..fbd3dac 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -11,6 +11,8 @@ use figment::{ }; use serde::Serialize; +use crate::config::LogLevel; + /// Otter is a lightweight implementation of the Gpodder API, designed to be used for small /// personal deployments. #[derive(Parser)] @@ -28,21 +30,26 @@ pub struct ClapConfig { short, long = "config", env = "OTTER_CONFIG_FILE", - value_name = "CONFIG_FILE" + value_name = "CONFIG_FILE", + global = true )] config_file: Option, #[serde(skip_serializing_if = "Option::is_none")] - #[arg(long = "data", value_name = "DATA_DIR")] + #[arg(long = "data", value_name = "DATA_DIR", global = true)] data_dir: Option, #[serde(skip_serializing_if = "Option::is_none")] - #[arg(short, long, value_name = "DOMAIN")] + #[arg(short, long, value_name = "DOMAIN", global = true)] domain: Option, #[serde(skip_serializing_if = "Option::is_none")] - #[arg(short, long, value_name = "PORT")] + #[arg(short, long, value_name = "PORT", global = true)] port: Option, + + #[serde(skip_serializing_if = "Option::is_none")] + #[arg(short, long = "log", value_name = "LOG_LEVEL", global = true)] + log_level: Option, } #[derive(Subcommand)] diff --git a/src/cli/serve.rs b/src/cli/serve.rs index a434db0..024d5b4 100644 --- a/src/cli/serve.rs +++ b/src/cli/serve.rs @@ -1,9 +1,13 @@ use std::time::Duration; +use tracing_subscriber::util::SubscriberInitExt; + use crate::server; pub fn serve(config: &crate::config::Config) -> u8 { - tracing_subscriber::fmt::init(); + tracing_subscriber::fmt() + .with_max_level(tracing::Level::from(config.log_level)) + .init(); tracing::info!("Initializing database and running migrations"); diff --git a/src/config.rs b/src/config.rs index 67bc59a..c015933 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,6 +1,27 @@ use std::path::PathBuf; -use serde::Deserialize; +use clap::ValueEnum; +use serde::{Deserialize, Serialize}; + +#[derive(Deserialize, Serialize, Clone, ValueEnum, Copy)] +#[serde(rename_all = "lowercase")] +pub enum LogLevel { + Debug, + Info, + Warn, + Error, +} + +impl From for tracing::Level { + fn from(value: LogLevel) -> Self { + match value { + LogLevel::Debug => Self::DEBUG, + LogLevel::Info => Self::INFO, + LogLevel::Warn => Self::WARN, + LogLevel::Error => Self::ERROR, + } + } +} #[derive(Deserialize)] pub struct Config { @@ -12,6 +33,8 @@ pub struct Config { pub port: u16, #[serde(default = "default_session_cleanup_interval")] pub session_cleanup_interval: u64, + #[serde(default = "default_log_level")] + pub log_level: LogLevel, } fn default_data_dir() -> PathBuf { @@ -30,3 +53,7 @@ fn default_session_cleanup_interval() -> u64 { // Default is once a day 60 * 60 * 24 } + +fn default_log_level() -> LogLevel { + LogLevel::Warn +}