From 9d67dc99603ac16a7904eb45df1ddb52c5e642d6 Mon Sep 17 00:00:00 2001 From: Vilsol Date: Wed, 17 Apr 2024 22:54:07 +0300 Subject: [PATCH] fix: configurable log level --- cfg/config.json | 6 ++++++ src/main.rs | 16 ++++++++++++---- src/server.rs | 7 +++++-- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/cfg/config.json b/cfg/config.json index 023dbed..f468ad4 100644 --- a/cfg/config.json +++ b/cfg/config.json @@ -1,4 +1,10 @@ { + "logger": { + "console": { + "level": "info" + } + }, + "server": { "host": "0.0.0.0", "port": 50062 diff --git a/src/main.rs b/src/main.rs index 70be1cb..e7bdf0d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ use config::{Case, Config, File}; use env_logger::WriteStyle; use log::info; +use std::str::FromStr; use std::{env, error::Error, net::ToSocketAddrs}; use tokio::sync::mpsc; @@ -21,8 +22,17 @@ mod types; #[tokio::main] async fn main() -> Result<(), Box> { + let config = load_config(); + + let level = log::LevelFilter::from_str( + config + .get_string("logger.console.level") + .unwrap_or("info".to_string()) + .as_str(), + ); + env_logger::builder() - .filter_level(log::LevelFilter::Debug) + .filter_level(level.expect("invalid level")) .write_style(WriteStyle::Always) .init(); @@ -41,8 +51,6 @@ async fn main() -> Result<(), Box> { .set_service_status("readiness", ServingStatus::Serving) .await; - let config = load_config(); - let (tx, rx) = mpsc::channel::(32); let pdf_server = PDFServer { @@ -88,7 +96,7 @@ fn logging(req: Request<()>) -> Result, Status> { fn load_config() -> Config { let run_mode = env::var("NODE_ENV").unwrap_or_else(|_| "development".into()); - info!("Running in mode: {}", run_mode); + println!("Running in mode: {}", run_mode); Config::builder() .add_source(File::with_name("cfg/config")) diff --git a/src/server.rs b/src/server.rs index a2b8a64..ba809eb 100644 --- a/src/server.rs +++ b/src/server.rs @@ -133,8 +133,11 @@ impl PDFServer { let mut out_data = data.clone(); if output.is_some() { - out_data = add_pdf_metadata(out_data.clone(), output.clone().unwrap().meta_data) - .expect("failed adding meta"); + out_data = add_pdf_metadata( + out_data.clone(), + output.clone().unwrap().meta_data, + ) + .expect("failed adding meta"); } out.push(