From 0d5ec3626773edb7d32884c594230c7cc03ae0e7 Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 2 Jun 2024 12:53:20 +0200 Subject: Simplify logging init --- src/controller/login.rs | 2 +- src/db/categories.rs | 10 +++++----- src/db/incomes.rs | 26 +++++++++++++------------- src/db/jobs.rs | 4 ++-- src/db/payments.rs | 26 +++++++++++++------------- src/db/users.rs | 12 ++++++------ src/jobs/mod.rs | 4 ++-- src/jobs/weekly_report.rs | 2 +- src/mail.rs | 10 +++++----- src/main.rs | 11 +++-------- src/templates.rs | 2 +- 11 files changed, 52 insertions(+), 57 deletions(-) diff --git a/src/controller/login.rs b/src/controller/login.rs index dd50a3a..a1bf466 100644 --- a/src/controller/login.rs +++ b/src/controller/login.rs @@ -75,7 +75,7 @@ pub async fn login( } Ok(false) => not_authorized(assets, templates).await, Err(err) => { - error!("Error verifying bcrypt password: {:?}", err); + log::error!("Error verifying bcrypt password: {:?}", err); server_error(assets, templates, "Erreur serveur").await } }, diff --git a/src/db/categories.rs b/src/db/categories.rs index 0e2a28c..fafe459 100644 --- a/src/db/categories.rs +++ b/src/db/categories.rs @@ -23,7 +23,7 @@ ORDER BY match res { Ok(categories) => categories, Err(err) => { - error!("Error listing categories: {:?}", err); + log::error!("Error listing categories: {:?}", err); vec![] } } @@ -50,7 +50,7 @@ WHERE match res { Ok(p) => Some(p), Err(err) => { - error!("Error looking for category {}: {:?}", id, err); + log::error!("Error looking for category {}: {:?}", id, err); None } } @@ -73,7 +73,7 @@ VALUES match res { Ok(x) => Some(x.last_insert_rowid()), Err(err) => { - error!("Error creating category: {:?}", err); + log::error!("Error creating category: {:?}", err); None } } @@ -101,7 +101,7 @@ WHERE match res { Ok(_) => true, Err(err) => { - error!("Error updating category {}: {:?}", id, err); + log::error!("Error updating category {}: {:?}", id, err); false } } @@ -125,7 +125,7 @@ WHERE match res { Ok(_) => true, Err(err) => { - error!("Error deleting category {}: {:?}", id, err); + log::error!("Error deleting category {}: {:?}", id, err); false } } diff --git a/src/db/incomes.rs b/src/db/incomes.rs index e5c3164..97cb2b7 100644 --- a/src/db/incomes.rs +++ b/src/db/incomes.rs @@ -26,7 +26,7 @@ WHERE match res { Ok(count) => count, Err(err) => { - error!("Error counting incomes: {:?}", err); + log::error!("Error counting incomes: {:?}", err); 0 } } @@ -62,7 +62,7 @@ OFFSET ? match res { Ok(incomes) => incomes, Err(err) => { - error!("Error listing incomes: {:?}", err); + log::error!("Error listing incomes: {:?}", err); vec![] } } @@ -73,7 +73,7 @@ pub async fn get_row(pool: &SqlitePool, id: i64) -> i64 { SELECT row FROM ( - SELECT + SELECT ROW_NUMBER () OVER (ORDER BY date DESC) AS row, id FROM @@ -94,7 +94,7 @@ WHERE match res { Ok(count) => count, Err(err) => { - error!("Error getting income row: {:?}", err); + log::error!("Error getting income row: {:?}", err); 1 } } @@ -123,7 +123,7 @@ WHERE match res { Ok(p) => Some(p), Err(err) => { - error!("Error looking for income {}: {:?}", id, err); + log::error!("Error looking for income {}: {:?}", id, err); None } } @@ -147,7 +147,7 @@ VALUES match res { Ok(x) => Some(x.last_insert_rowid()), Err(err) => { - error!("Error creating income: {:?}", err); + log::error!("Error creating income: {:?}", err); None } } @@ -180,7 +180,7 @@ WHERE Ok(ids) => ids, Err(Error::RowNotFound) => vec![], Err(err) => { - error!("Error looking if income is defined: {:?}", err); + log::error!("Error looking if income is defined: {:?}", err); vec![] } } @@ -210,7 +210,7 @@ WHERE match res { Ok(_) => true, Err(err) => { - error!("Error updating income {}: {:?}", id, err); + log::error!("Error updating income {}: {:?}", id, err); false } } @@ -234,7 +234,7 @@ WHERE match res { Ok(_) => true, Err(err) => { - error!("Error deleting income {}: {:?}", id, err); + log::error!("Error deleting income {}: {:?}", id, err); false } } @@ -272,7 +272,7 @@ ON match res { Ok(d) => d, Err(err) => { - error!("Error looking for incomes defined for all: {:?}", err); + log::error!("Error looking for incomes defined for all: {:?}", err); None } } @@ -290,7 +290,7 @@ pub async fn cumulative( match res { Ok(incomes) => HashMap::from_iter(incomes), Err(err) => { - error!("Error computing cumulative income: {:?}", err); + log::error!("Error computing cumulative income: {:?}", err); HashMap::new() } } @@ -435,7 +435,7 @@ FROM match res { Ok(xs) => xs, Err(err) => { - error!("Error listing incomes for statistics: {:?}", err); + log::error!("Error listing incomes for statistics: {:?}", err); vec![] } } @@ -486,7 +486,7 @@ ORDER BY match res { Ok(payments) => payments, Err(err) => { - error!("Error listing payments for report: {:?}", err); + log::error!("Error listing payments for report: {:?}", err); vec![] } } diff --git a/src/db/jobs.rs b/src/db/jobs.rs index 3f84fc8..a80ef68 100644 --- a/src/db/jobs.rs +++ b/src/db/jobs.rs @@ -28,7 +28,7 @@ WHERE Ok(_) => true, Err(Error::RowNotFound) => false, Err(err) => { - error!("Error looking if job should run: {:?}", err); + log::error!("Error looking if job should run: {:?}", err); false } } @@ -48,6 +48,6 @@ WHERE match res { Ok(_) => (), - Err(err) => error!("Error actualizing job last execution: {:?}", err), + Err(err) => log::error!("Error actualizing job last execution: {:?}", err), } } diff --git a/src/db/payments.rs b/src/db/payments.rs index bebc69d..b415a28 100644 --- a/src/db/payments.rs +++ b/src/db/payments.rs @@ -82,7 +82,7 @@ WHERE match res { Ok(count) => count, Err(err) => { - error!("Error counting payments: {:?}", err); + log::error!("Error counting payments: {:?}", err); Count { count: 0, total_cost: 0, @@ -169,7 +169,7 @@ OFFSET ? match res { Ok(payments) => payments, Err(err) => { - error!("Error listing payments: {:?}", err); + log::error!("Error listing payments: {:?}", err); vec![] } } @@ -308,7 +308,7 @@ GROUP BY match result { Ok(payments) => payments, Err(err) => { - error!("Error listing payments for statistics: {:?}", err); + log::error!("Error listing payments for statistics: {:?}", err); vec![] } } @@ -342,7 +342,7 @@ WHERE match res { Ok(count) => count, Err(err) => { - error!("Error getting payment row: {:?}", err); + log::error!("Error getting payment row: {:?}", err); 1 } } @@ -373,7 +373,7 @@ WHERE match res { Ok(p) => Some(p), Err(err) => { - error!("Error looking for payment {}: {:?}", id, err); + log::error!("Error looking for payment {}: {:?}", id, err); None } } @@ -400,7 +400,7 @@ VALUES match res { Ok(x) => Some(x.last_insert_rowid()), Err(err) => { - error!("Error creating payment: {:?}", err); + log::error!("Error creating payment: {:?}", err); None } } @@ -434,7 +434,7 @@ WHERE match res { Ok(_) => true, Err(err) => { - error!("Error updating payment {}: {:?}", id, err); + log::error!("Error updating payment {}: {:?}", id, err); false } } @@ -458,7 +458,7 @@ WHERE match res { Ok(_) => true, Err(err) => { - error!("Error deleting payment {}: {:?}", id, err); + log::error!("Error deleting payment {}: {:?}", id, err); false } } @@ -493,7 +493,7 @@ ORDER BY Ok(category) => Some(category), Err(Error::RowNotFound) => None, Err(err) => { - error!( + log::error!( "Error looking for the category of {}: {:?}", payment_name, err ); @@ -521,7 +521,7 @@ LIMIT Ok(_) => true, Err(Error::RowNotFound) => false, Err(err) => { - error!( + log::error!( "Error looking if category {} is used: {:?}", category_id, err ); @@ -560,7 +560,7 @@ ON match res { Ok(costs) => HashMap::from_iter(costs), Err(err) => { - error!("Error getting payments repartition: {:?}", err); + log::error!("Error getting payments repartition: {:?}", err); HashMap::new() } } @@ -588,7 +588,7 @@ WHERE match res { Ok(_) => (), - Err(err) => error!("Error creating monthly payments: {:?}", err), + Err(err) => log::error!("Error creating monthly payments: {:?}", err), } } @@ -640,7 +640,7 @@ ORDER BY match res { Ok(payments) => payments, Err(err) => { - error!("Error listing payments for report: {:?}", err); + log::error!("Error listing payments for report: {:?}", err); vec![] } } diff --git a/src/db/users.rs b/src/db/users.rs index 82326a9..f463421 100644 --- a/src/db/users.rs +++ b/src/db/users.rs @@ -23,7 +23,7 @@ ORDER BY match res { Ok(users) => users, Err(err) => { - error!("Error listing users: {:?}", err); + log::error!("Error listing users: {:?}", err); vec![] } } @@ -53,7 +53,7 @@ WHERE match res { Ok(_) => true, Err(err) => { - error!("Error updating login token: {:?}", err); + log::error!("Error updating login token: {:?}", err); false } } @@ -78,7 +78,7 @@ WHERE match res { Ok(_) => true, Err(err) => { - error!("Error removing login token: {:?}", err); + log::error!("Error removing login token: {:?}", err); false } } @@ -91,7 +91,7 @@ pub async fn get_by_login_token( let res = sqlx::query_as::<_, User>( r#" SELECT - id, + id, name, email FROM @@ -108,7 +108,7 @@ WHERE Ok(user) => Some(user), Err(Error::RowNotFound) => None, Err(err) => { - error!("Error getting user from login token: {:?}", err); + log::error!("Error getting user from login token: {:?}", err); None } } @@ -137,7 +137,7 @@ WHERE Ok(hash) => Some(hash), Err(Error::RowNotFound) => None, Err(err) => { - error!("Error getting password hash: {:?}", err); + log::error!("Error getting password hash: {:?}", err); None } } diff --git a/src/jobs/mod.rs b/src/jobs/mod.rs index 5f95597..17df58c 100644 --- a/src/jobs/mod.rs +++ b/src/jobs/mod.rs @@ -11,14 +11,14 @@ use crate::model::job::Job; pub async fn start(config: Config, pool: SqlitePool, templates: Tera) { loop { if db::jobs::should_run(&pool, Job::WeeklyReport).await { - info!("Starting weekly report job"); + log::info!("Starting weekly report job"); if weekly_report::send(&config, &pool, &templates).await { db::jobs::actualize_last_execution(&pool, Job::WeeklyReport) .await; } } if db::jobs::should_run(&pool, Job::MonthlyPayment).await { - info!("Starting monthly payment job"); + log::info!("Starting monthly payment job"); db::payments::create_monthly_payments(&pool).await; db::jobs::actualize_last_execution(&pool, Job::MonthlyPayment) .await; diff --git a/src/jobs/weekly_report.rs b/src/jobs/weekly_report.rs index b482f0d..0c10143 100644 --- a/src/jobs/weekly_report.rs +++ b/src/jobs/weekly_report.rs @@ -30,7 +30,7 @@ pub async fn send( .await } Err(err) => { - error!("Error preparing weekly report from template: {:?}", err); + log::error!("Error preparing weekly report from template: {:?}", err); false } } diff --git a/src/mail.rs b/src/mail.rs index 9fe0ed8..c77e2ad 100644 --- a/src/mail.rs +++ b/src/mail.rs @@ -23,7 +23,7 @@ pub async fn send( ) -> bool { let headers = format_headers(recipients.clone(), subject); - info!( + log::info!( "Sending mail{}\n{}", if config.mock_mails { " (MOCK)" } else { "" }, headers.clone() @@ -52,18 +52,18 @@ pub async fn send( match spawn(command, &message.into_bytes()).await { Ok(output) => { if output.status.success() { - info!("Mail sent"); + log::info!("Mail sent"); true } else { match String::from_utf8(output.stderr) { - Ok(error) => error!("Error sending email: {}", error), - _ => error!("Error sending email"), + Ok(error) => log::error!("Error sending email: {}", error), + _ => log::error!("Error sending email"), }; false } } Err(err) => { - error!("Error spawning command: {:?}", err); + log::error!("Error spawning command: {:?}", err); false } } diff --git a/src/main.rs b/src/main.rs index 5f3b8c6..2b3aebd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,9 +4,6 @@ use hyper_util::rt::TokioIo; use sqlx::sqlite::SqlitePool; use tokio::net::TcpListener; -#[macro_use] -extern crate log; - mod assets; mod controller; mod crypto; @@ -25,10 +22,8 @@ use model::config; #[tokio::main] async fn main() -> Result<(), Box> { - env_logger::Builder::from_env( - env_logger::Env::default().default_filter_or("warn"), - ) - .init(); + + env_logger::init(); let config = config::from_env() .unwrap_or_else(|err| panic!("Error reading config: {err}")); @@ -44,7 +39,7 @@ async fn main() -> Result<(), Box> { tokio::spawn(jobs::start(config.clone(), pool.clone(), templates.clone())); let listener = TcpListener::bind(config.socket_address).await?; - info!("Starting server at {}", config.socket_address); + log::info!("Starting server at {}", config.socket_address); loop { let config = config.clone(); diff --git a/src/templates.rs b/src/templates.rs index c537ac7..1f86717 100644 --- a/src/templates.rs +++ b/src/templates.rs @@ -20,7 +20,7 @@ pub fn get() -> Tera { let mut tera = match Tera::new("templates/**/*") { Ok(t) => t, Err(e) => { - error!("Parsing error(s): {}", e); + log::error!("Parsing error(s): {}", e); ::std::process::exit(1); } }; -- cgit v1.2.3