Skip to content

Commit

Permalink
fix: configurable log level
Browse files Browse the repository at this point in the history
  • Loading branch information
Vilsol committed Apr 17, 2024
1 parent 55126fd commit 9d67dc9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
6 changes: 6 additions & 0 deletions cfg/config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{
"logger": {
"console": {
"level": "info"
}
},

"server": {
"host": "0.0.0.0",
"port": 50062
Expand Down
16 changes: 12 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -21,8 +22,17 @@ mod types;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
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();

Expand All @@ -41,8 +51,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
.set_service_status("readiness", ServingStatus::Serving)
.await;

let config = load_config();

let (tx, rx) = mpsc::channel::<InternalRequest>(32);

let pdf_server = PDFServer {
Expand Down Expand Up @@ -88,7 +96,7 @@ fn logging(req: Request<()>) -> Result<Request<()>, 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"))
Expand Down
7 changes: 5 additions & 2 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 9d67dc9

Please sign in to comment.