diff --git a/config-dist.toml b/config-dist.toml index 2350c577..9b1f65d4 100644 --- a/config-dist.toml +++ b/config-dist.toml @@ -21,3 +21,9 @@ tokens = ["live777"] # [[auth.accounts]] # username = "live777" # password = "live777" + +[log] +# Env: `LOG_LEVEL` +# Default: info +# Values: off, error, warn, info, debug, trace +# level = "warn" diff --git a/src/config.rs b/src/config.rs index 6e4a95d6..4ffd95e9 100644 --- a/src/config.rs +++ b/src/config.rs @@ -14,6 +14,8 @@ pub struct Config { pub ice_servers: Vec, #[serde(default)] pub auth: Auth, + #[serde(default)] + pub log: Log, } #[derive(Debug, Clone, Default, Serialize, Deserialize)] pub struct Auth { @@ -27,6 +29,12 @@ pub struct Account { pub password: String, } +#[derive(Debug, Clone, Default, Serialize, Deserialize)] +pub struct Log { + #[serde(default = "default_log_level")] + pub level: String, +} + fn default_listen() -> String { format!("[::]:{}", env::var("PORT").unwrap_or(String::from("7777"))) } @@ -40,6 +48,16 @@ fn default_ice_servers() -> Vec { }] } +fn default_log_level() -> String { + env::var("LOG_LEVEL").unwrap_or_else(|_| + if cfg!(debug_assertions) { + "debug".to_string() + } else { + "info".to_string() + } + ) +} + #[derive(Debug, Clone, Deserialize, Serialize)] pub struct IceServer { #[serde(default)] @@ -128,6 +146,7 @@ impl Config { ice_servers: default_ice_servers(), listen: default_listen(), auth: Default::default(), + log: Default::default(), } } } diff --git a/src/main.rs b/src/main.rs index f43e4eed..986451b6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,3 @@ -use std::env; use std::net::SocketAddr; use std::str::FromStr; use std::sync::Arc; @@ -47,27 +46,14 @@ async fn main() { metrics::REGISTRY .register(Box::new(metrics::SUBSCRIBE.clone())) .unwrap(); - let log_level = if cfg!(debug_assertions) { - env::var("LOG_LEVEL").unwrap_or_else(|_| "debug".to_string()) - } else { - env::var("LOG_LEVEL").unwrap_or_else(|_| "info".to_string()) - }; - let level_filter = match log_level.as_str() { - "off" => log::LevelFilter::Off, - "error" => log::LevelFilter::Error, - "warn" => log::LevelFilter::Warn, - "info" => log::LevelFilter::Info, - "debug" => log::LevelFilter::Debug, - "trace" => log::LevelFilter::Trace, - _ => log::LevelFilter::Info, - }; + + let cfg = Config::parse(); env_logger::builder() - .filter_level(level_filter) + .parse_filters(cfg.log.level.as_str()) .filter_module("webrtc", log::LevelFilter::Error) .write_style(env_logger::WriteStyle::Auto) .target(env_logger::Target::Stdout) .init(); - let cfg = Config::parse(); let addr = SocketAddr::from_str(&cfg.listen).expect("invalid listen address"); info!("Server listening on {}", addr); let ice_servers = cfg