From 1f03c01d1daf7161ac43fc38a5104272ebaa632d Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Sun, 3 Nov 2024 12:11:33 +0100 Subject: [PATCH] Add a trace layer --- TODO.md | 2 +- backend/src/data/db.rs | 5 +++-- backend/src/email.rs | 3 ++- backend/src/main.rs | 35 ++++++++++++++++++++--------------- backend/src/services.rs | 2 -- 5 files changed, 26 insertions(+), 21 deletions(-) diff --git a/TODO.md b/TODO.md index b3186cc..f26af2a 100644 --- a/TODO.md +++ b/TODO.md @@ -1,4 +1,3 @@ -* Clean the old code + commit * How to log error to journalctl or elsewhere + debug log? * Try using WASM for all the client logic (test on editing/creating a recipe) * Understand the example here: @@ -17,6 +16,7 @@ .service(services::webapi::set_recipe_description) * Add support to translations into db model. +[ok] Clean the old code + commit [ok] Reactivate sign up/in/out [ok] Change all id to i64 [ok] Check cookie lifetime -> Session by default diff --git a/backend/src/data/db.rs b/backend/src/data/db.rs index c84f72b..a2f7057 100644 --- a/backend/src/data/db.rs +++ b/backend/src/data/db.rs @@ -13,6 +13,7 @@ use sqlx::{ Pool, Sqlite, Transaction, }; use thiserror::Error; +use tracing::{event, Level}; use crate::{ consts, @@ -156,7 +157,7 @@ WHERE [type] = 'table' AND [name] = 'Version' let next_version = current_version + 1; if next_version <= CURRENT_DB_VERSION { - println!("Update to version {}...", next_version); + event!(Level::INFO, "Update to version {}...", next_version); } async fn update_version(to_version: u32, tx: &mut Transaction<'_, Sqlite>) -> Result<()> { @@ -171,7 +172,7 @@ WHERE [type] = 'table' AND [name] = 'Version' fn ok(updated: bool) -> Result { if updated { - println!("Version updated"); + event!(Level::INFO, "Version updated"); } Ok(updated) } diff --git a/backend/src/email.rs b/backend/src/email.rs index a167e86..171555a 100644 --- a/backend/src/email.rs +++ b/backend/src/email.rs @@ -3,6 +3,7 @@ use lettre::{ transport::smtp::{authentication::Credentials, AsyncSmtpTransport}, AsyncTransport, Message, Tokio1Executor, }; +use tracing::{event, Level}; use crate::consts; @@ -57,7 +58,7 @@ pub async fn send_validation( .build(); if let Err(error) = mailer.send(email).await { - eprintln!("Error when sending E-mail:\n{:?}", &error); + event!(Level::ERROR, "Error when sending E-mail: {}", &error); } Ok(()) diff --git a/backend/src/main.rs b/backend/src/main.rs index 65a778b..3d3fc56 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -11,7 +11,8 @@ use axum_extra::extract::cookie::CookieJar; use chrono::prelude::*; use clap::Parser; use config::Config; -use tower_http::services::ServeDir; +use tower_http::{services::ServeDir, trace::TraceLayer}; +use tracing::{event, Level}; use data::db; @@ -42,6 +43,12 @@ impl FromRef for db::Connection { } } +#[cfg(debug_assertions)] +const TRACING_LEVEL: tracing::Level = tracing::Level::DEBUG; + +#[cfg(not(debug_assertions))] +const TRACING_LEVEL: tracing::Level = tracing::Level::INFO; + // TODO: Should main returns 'Result'? #[tokio::main] async fn main() { @@ -49,14 +56,16 @@ async fn main() { return; } - println!("Starting Recipes as web server..."); + tracing_subscriber::fmt() + .with_max_level(TRACING_LEVEL) + .init(); + + event!(Level::INFO, "Starting Recipes as web server..."); let config = config::load(); let port = config.port; - println!("Configuration: {:?}", config); - - tracing_subscriber::fmt::init(); + event!(Level::INFO, "Configuration: {:?}", config); let db_connection = db::Connection::new().await.unwrap(); @@ -78,6 +87,7 @@ async fn main() { get(services::sign_in_get).post(services::sign_in_post), ) .route("/signout", get(services::sign_out)) + .layer(TraceLayer::new_for_http()) .route_layer(middleware::from_fn_with_state( state.clone(), user_authentication, @@ -117,23 +127,18 @@ async fn get_current_user( .authentication(token_cookie.value(), &client_ip, &client_user_agent) .await { - Ok(db::AuthenticationResult::NotValidToken) => { - // TODO: remove cookie? - None - } + Ok(db::AuthenticationResult::NotValidToken) => None, Ok(db::AuthenticationResult::Ok(user_id)) => { match connection.load_user(user_id).await { Ok(user) => user, Err(error) => { - // TODO: Return 'Result'? - println!("Error during authentication: {}", error); + event!(Level::WARN, "Error during authentication: {}", error); None } } } Err(error) => { - // TODO: Return 'Result'? - println!("Error during authentication: {}", error); + event!(Level::WARN, "Error during authentication: {}", error); None } }, @@ -176,7 +181,7 @@ async fn process_args() -> bool { match db::Connection::new().await { Ok(con) => { if let Err(error) = con.execute_file("sql/data_test.sql").await { - eprintln!("{}", error); + event!(Level::ERROR, "{}", error); } // Set the creation datetime to 'now'. con.execute_sql( @@ -188,7 +193,7 @@ async fn process_args() -> bool { .unwrap(); } Err(error) => { - eprintln!("{}", error); + event!(Level::ERROR, "{}", error); } } diff --git a/backend/src/services.rs b/backend/src/services.rs index a8f440d..db521b2 100644 --- a/backend/src/services.rs +++ b/backend/src/services.rs @@ -305,8 +305,6 @@ pub async fn sign_up_post( ) }; - println!("{}", &url); - let email = form_data.email.clone(); match email::send_validation( &url, -- 2.45.2