From 1391b8a594321863c5dfc721e18f96434b8dc7c5 Mon Sep 17 00:00:00 2001 From: kieran Date: Tue, 4 Mar 2025 11:59:59 +0000 Subject: [PATCH] feat: log with fern (disk logging) --- .gitignore | 3 ++- Cargo.lock | 58 +++++++++----------------------------------------- Cargo.toml | 2 +- src/bin/api.rs | 47 +++++++++++++++++++++++++++++++++++----- 4 files changed, 55 insertions(+), 55 deletions(-) diff --git a/.gitignore b/.gitignore index 2390d92..8cbb1a6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ **/target .idea/ -*.config.yaml \ No newline at end of file +*.config.yaml +**/*.log \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 5ea7904..cdfab75 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -933,19 +933,6 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "env_logger" -version = "0.10.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4cd405aab171cb85d6735e5c8d9db038c17d3ca007a4d2c25f337935c3d90580" -dependencies = [ - "humantime", - "is-terminal", - "log", - "regex", - "termcolor", -] - [[package]] name = "equivalent" version = "1.0.2" @@ -1010,6 +997,15 @@ dependencies = [ "tower 0.4.13", ] +[[package]] +name = "fern" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4316185f709b23713e41e3195f90edef7fb00c3ed4adc79769cf09cc762a3b29" +dependencies = [ + "log", +] + [[package]] name = "ff" version = "0.13.0" @@ -1499,12 +1495,6 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" -[[package]] -name = "humantime" -version = "2.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" - [[package]] name = "hyper" version = "0.14.32" @@ -2016,6 +2006,7 @@ dependencies = [ "clap", "config", "fedimint-tonic-lnd", + "fern", "futures", "hex", "ipnetwork", @@ -2025,7 +2016,6 @@ dependencies = [ "native-tls", "nostr", "nostr-sdk", - "pretty_env_logger", "rand 0.9.0", "reqwest", "rocket", @@ -2707,16 +2697,6 @@ dependencies = [ "zerocopy 0.7.35", ] -[[package]] -name = "pretty_env_logger" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "865724d4dbe39d9f3dd3b52b88d859d66bcb2d6a0acfd5ea68a65fb66d4bdc1c" -dependencies = [ - "env_logger", - "log", -] - [[package]] name = "prettyplease" version = "0.2.29" @@ -3998,15 +3978,6 @@ dependencies = [ "windows-sys 0.59.0", ] -[[package]] -name = "termcolor" -version = "1.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" -dependencies = [ - "winapi-util", -] - [[package]] name = "thiserror" version = "1.0.69" @@ -4813,15 +4784,6 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" -[[package]] -name = "winapi-util" -version = "0.1.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" -dependencies = [ - "windows-sys 0.59.0", -] - [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index 4ebc396..4afeee0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,7 +22,7 @@ tokio = { version = "1.37.0", features = ["rt", "rt-multi-thread", "macros", "sy anyhow = "1.0.83" config = { version = "0.15.8", features = ["yaml"] } log = "0.4.21" -pretty_env_logger = "0.5.0" +fern = "0.7.1" serde = { version = "1.0.213", features = ["derive"] } serde_json = "1.0.132" rocket = { version = "0.5.1", features = ["json"] } diff --git a/src/bin/api.rs b/src/bin/api.rs index ac3e97a..84a7e57 100644 --- a/src/bin/api.rs +++ b/src/bin/api.rs @@ -1,4 +1,5 @@ use anyhow::Error; +use chrono::{DateTime, Utc}; use clap::Parser; use config::{Config, File}; use lnvps::api; @@ -10,30 +11,66 @@ use lnvps::settings::Settings; use lnvps::status::VmStateCache; use lnvps::worker::{WorkJob, Worker}; use lnvps_db::{LNVpsDb, LNVpsDbMysql}; -use log::error; +use log::{error, LevelFilter}; use nostr::Keys; use nostr_sdk::Client; use rocket_okapi::swagger_ui::{make_swagger_ui, SwaggerUIConfig}; use std::net::{IpAddr, SocketAddr}; +use std::path::PathBuf; use std::sync::Arc; use std::time::Duration; +use tokio::fs::create_dir_all; use tokio::time::sleep; #[derive(Parser)] #[clap(about, version, author)] struct Args { + /// Path to the config file #[clap(short, long)] - config: Option, + config: Option, + + /// Where to write the log file + #[clap(long)] + log: Option, } #[rocket::main] async fn main() -> Result<(), Error> { - pretty_env_logger::init(); + let log_level = std::env::var("RUST_LOG") + .unwrap_or_else(|_| "info".to_string()) // Default to "info" if not set + .to_lowercase(); + + let max_level = match log_level.as_str() { + "trace" => LevelFilter::Trace, + "debug" => LevelFilter::Debug, + "info" => LevelFilter::Info, + "warn" => LevelFilter::Warn, + "error" => LevelFilter::Error, + "off" => LevelFilter::Off, + _ => LevelFilter::Info, + }; let args = Args::parse(); + fern::Dispatch::new() + .level(max_level) + .level_for("rocket", LevelFilter::Error) + .chain(fern::log_file( + args.log.unwrap_or(PathBuf::from(".")).join("main.log"), + )?) + .chain(std::io::stdout()) + .format(|out, message, record| { + out.finish(format_args!( + "[{}] [{}] {}", + Utc::now().format("%Y-%m-%d %H:%M:%S"), + record.level(), + message + )) + }) + .apply()?; + let settings: Settings = Config::builder() - .add_source(File::with_name( - &args.config.unwrap_or("config.yaml".to_string()), + .add_source(File::from( + args.config.unwrap_or(PathBuf::from("config.yaml")), )) .build()? .try_deserialize()?;