feat(otter): add log level cli config

main
Jef Roosens 2025-03-29 14:43:35 +01:00
parent 5f57d85584
commit 5112a6ce35
No known key found for this signature in database
GPG Key ID: 21FD3D77D56BAF49
3 changed files with 44 additions and 6 deletions

View File

@ -11,6 +11,8 @@ use figment::{
}; };
use serde::Serialize; use serde::Serialize;
use crate::config::LogLevel;
/// Otter is a lightweight implementation of the Gpodder API, designed to be used for small /// Otter is a lightweight implementation of the Gpodder API, designed to be used for small
/// personal deployments. /// personal deployments.
#[derive(Parser)] #[derive(Parser)]
@ -28,21 +30,26 @@ pub struct ClapConfig {
short, short,
long = "config", long = "config",
env = "OTTER_CONFIG_FILE", env = "OTTER_CONFIG_FILE",
value_name = "CONFIG_FILE" value_name = "CONFIG_FILE",
global = true
)] )]
config_file: Option<PathBuf>, config_file: Option<PathBuf>,
#[serde(skip_serializing_if = "Option::is_none")] #[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<PathBuf>, data_dir: Option<PathBuf>,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
#[arg(short, long, value_name = "DOMAIN")] #[arg(short, long, value_name = "DOMAIN", global = true)]
domain: Option<String>, domain: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
#[arg(short, long, value_name = "PORT")] #[arg(short, long, value_name = "PORT", global = true)]
port: Option<u16>, port: Option<u16>,
#[serde(skip_serializing_if = "Option::is_none")]
#[arg(short, long = "log", value_name = "LOG_LEVEL", global = true)]
log_level: Option<LogLevel>,
} }
#[derive(Subcommand)] #[derive(Subcommand)]

View File

@ -1,9 +1,13 @@
use std::time::Duration; use std::time::Duration;
use tracing_subscriber::util::SubscriberInitExt;
use crate::server; use crate::server;
pub fn serve(config: &crate::config::Config) -> u8 { 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"); tracing::info!("Initializing database and running migrations");

View File

@ -1,6 +1,27 @@
use std::path::PathBuf; 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<LogLevel> 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)] #[derive(Deserialize)]
pub struct Config { pub struct Config {
@ -12,6 +33,8 @@ pub struct Config {
pub port: u16, pub port: u16,
#[serde(default = "default_session_cleanup_interval")] #[serde(default = "default_session_cleanup_interval")]
pub session_cleanup_interval: u64, pub session_cleanup_interval: u64,
#[serde(default = "default_log_level")]
pub log_level: LogLevel,
} }
fn default_data_dir() -> PathBuf { fn default_data_dir() -> PathBuf {
@ -30,3 +53,7 @@ fn default_session_cleanup_interval() -> u64 {
// Default is once a day // Default is once a day
60 * 60 * 24 60 * 60 * 24
} }
fn default_log_level() -> LogLevel {
LogLevel::Warn
}