Replace all event! by a specific macro: warn!, error!, etc.

This commit is contained in:
Greg Burri 2025-04-30 01:50:50 +02:00
parent 1485110204
commit 8152d3d9b0
7 changed files with 43 additions and 82 deletions

View file

@ -21,7 +21,7 @@ use tower_http::{
services::{ServeDir, ServeFile},
trace::TraceLayer,
};
use tracing::{Level, event};
use tracing::warn;
use crate::{
config::Config,
@ -422,13 +422,13 @@ async fn get_current_user(
match connection.load_user(user_id).await {
Ok(user) => user,
Err(error) => {
event!(Level::WARN, "Error during authentication: {}", error);
warn!("Error during authentication: {}", error);
None
}
}
}
Err(error) => {
event!(Level::WARN, "Error during authentication: {}", error);
warn!("Error during authentication: {}", error);
None
}
},

View file

@ -3,7 +3,7 @@ use std::path::PathBuf;
use async_compression::tokio::bufread::GzipEncoder;
use chrono::{NaiveTime, TimeDelta};
use tokio::{fs::File, io::BufReader};
use tracing::{Level, event};
use tracing::{debug, error, info};
use super::db;
@ -27,7 +27,7 @@ where
if time_to_wait < TimeDelta::zero() {
time_to_wait += TimeDelta::days(1);
}
event!(Level::DEBUG, "Backup in {}s", time_to_wait.num_seconds());
debug!("Backup in {}s", time_to_wait.num_seconds());
tokio::time::sleep(time_to_wait.to_std().unwrap()).await;
start(&db_connection, &path).await;
@ -46,14 +46,13 @@ where
async fn start(db_connection: &db::Connection, path: &PathBuf) {
let path_compressed = path.with_extension("sqlite.gz");
event!(
Level::INFO,
info!(
"Starting backup process to {}...",
path_compressed.display()
);
if let Err(error) = db_connection.backup(&path).await {
event!(Level::ERROR, "Error when backing up database: {}", error);
error!("Error when backing up database: {}", error);
}
// Compress the backup file.
@ -64,36 +63,20 @@ async fn start(db_connection: &db::Connection, path: &PathBuf) {
match File::create(&path_compressed).await {
Ok(mut file_output) => {
if let Err(error) = tokio::io::copy(&mut encoder, &mut file_output).await {
event!(
Level::ERROR,
"Error when compressing backup file: {}",
error
);
error!("Error when compressing backup file: {}", error);
} else if let Err(error) = std::fs::remove_file(path) {
event!(
Level::ERROR,
"Error when removing uncompressed backup file: {}",
error
);
error!("Error when removing uncompressed backup file: {}", error);
} else {
event!(Level::INFO, "Backup done: {}", path_compressed.display());
info!("Backup done: {}", path_compressed.display());
}
}
Err(error) => {
event!(
Level::ERROR,
"Error when creating compressed backup file: {}",
error
);
error!("Error when creating compressed backup file: {}", error);
}
}
}
Err(error) => {
event!(
Level::ERROR,
"Error when opening backup file for compression: {}",
error
);
error!("Error when opening backup file for compression: {}", error);
}
}
}

View file

@ -11,7 +11,7 @@ use sqlx::{
Pool, Sqlite, Transaction,
sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePoolOptions, SqliteSynchronous},
};
use tracing::{Level, event};
use tracing::info;
use crate::consts;
@ -149,7 +149,7 @@ WHERE [type] = 'table' AND [name] = 'Version'
let next_version = current_version + 1;
if next_version <= CURRENT_DB_VERSION {
event!(Level::INFO, "Update to version {}...", next_version);
info!("Update to version {}...", next_version);
}
async fn update_version(to_version: u32, tx: &mut Transaction<'_, Sqlite>) -> Result<()> {
@ -164,7 +164,7 @@ WHERE [type] = 'table' AND [name] = 'Version'
fn ok(updated: bool) -> Result<bool> {
if updated {
event!(Level::INFO, "Version updated");
info!("Version updated");
}
Ok(updated)
}

View file

@ -2,7 +2,7 @@ use lettre::{
AsyncTransport, Message, Tokio1Executor,
transport::smtp::{AsyncSmtpTransport, authentication::Credentials},
};
use tracing::{Level, event};
use tracing::error;
use crate::consts;
@ -43,7 +43,7 @@ pub async fn send_email(
.build();
if let Err(error) = mailer.send(email).await {
event!(Level::ERROR, "Error when sending E-mail: {}", &error);
error!("Error when sending E-mail: {}", &error);
}
Ok(())

View file

@ -4,7 +4,7 @@ use std::{net::SocketAddr, path::Path};
use clap::Parser;
use tokio::signal;
use tracing::{Level, event};
use tracing::{error, info};
use recipes::{
app, config, consts,
@ -17,17 +17,17 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = config::load();
let log = Log::new_to_directory(&config.logs_directory);
event!(Level::INFO, "Configuration: {:?}", config);
info!("Configuration: {:?}", config);
if !process_args(&config.database_directory).await {
return Ok(());
}
event!(Level::INFO, "Starting Recipes as web server...");
info!("Starting Recipes as web server...");
let db_connection = db::Connection::new(&config.database_directory)
.await
.inspect_err(|err| event!(Level::ERROR, "Unable to connect to the database: {}", err))?;
.inspect_err(|err| error!("Unable to connect to the database: {}", err))?;
if let Some(backup_time) = config.backup_time {
backup::start_task(
@ -36,7 +36,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
backup_time,
);
} else {
event!(Level::INFO, "Backups disabled by config");
info!("Backups disabled by config");
}
let port = config.port;
@ -56,7 +56,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.with_graceful_shutdown(shutdown_signal())
.await?;
event!(Level::INFO, "Recipes stopped");
info!("Recipes stopped");
Ok(())
}
@ -105,16 +105,13 @@ where
match db::Connection::new(database_directory).await {
Ok(con) => {
if let Err(error) = con.execute_file("sql/data_test.sql").await {
event!(Level::ERROR, "{}", error);
error!("{}", error);
}
event!(
Level::INFO,
"A new test database has been created successfully"
);
info!("A new test database has been created successfully");
}
Err(error) => {
event!(Level::ERROR, "{}", error);
error!("{}", error);
}
}

View file

@ -17,7 +17,7 @@ use chrono::Duration;
use lettre::Address;
use serde::Deserialize;
use strum_macros::Display;
use tracing::{Level, event};
use tracing::{error, warn};
use crate::{
app::{AppState, Context, Result},
@ -94,11 +94,9 @@ pub async fn sign_up_post(
form_data: &SignUpFormData,
context: Context,
) -> Result<Response> {
event!(
Level::WARN,
warn!(
"Unable to sign up with email {}: {}",
form_data.email,
error
form_data.email, error
);
let invalid_password_mess = &context.tr.tp(
@ -213,8 +211,7 @@ pub async fn sign_up_validation(
) -> Result<(CookieJar, impl IntoResponse)> {
let mut jar = CookieJar::from_headers(&headers);
if let Some(ref user) = context.user {
event!(
Level::WARN,
warn!(
"Unable to validate: user already logged. Email: {}",
user.email
);
@ -261,11 +258,7 @@ pub async fn sign_up_validation(
))
}
db::user::ValidationResult::ValidationExpired => {
event!(
Level::WARN,
"Unable to validate: validation expired. Token: {}",
token
);
warn!("Unable to validate: validation expired. Token: {}", token);
Ok((
jar,
Html(
@ -279,11 +272,7 @@ pub async fn sign_up_validation(
))
}
db::user::ValidationResult::UnknownUser => {
event!(
Level::WARN,
"Unable to validate: unknown user. Token: {}",
token
);
warn!("Unable to validate: unknown user. Token: {}", token);
Ok((
jar,
Html(
@ -299,7 +288,7 @@ pub async fn sign_up_validation(
}
}
None => {
event!(Level::WARN, "Unable to validate: no token provided");
warn!("Unable to validate: no token provided");
Ok((
jar,
Html(
@ -356,11 +345,9 @@ pub async fn sign_in_post(
.await?
{
error @ db::user::SignInResult::AccountNotValidated => {
event!(
Level::WARN,
warn!(
"Account not validated, email: {}: {}",
form_data.email,
error
form_data.email, error
);
Ok((
jar,
@ -376,7 +363,7 @@ pub async fn sign_in_post(
))
}
error @ (db::user::SignInResult::UserNotFound | db::user::SignInResult::WrongPassword) => {
event!(Level::WARN, "Email: {}: {}", form_data.email, error);
warn!("Email: {}: {}", form_data.email, error);
Ok((
jar,
Html(
@ -564,7 +551,7 @@ pub async fn ask_reset_password_post(
}
}
Err(error) => {
event!(Level::ERROR, "{}", error);
error!("{}", error);
error_response(
AskResetPasswordError::DatabaseError,
&form_data.email,
@ -649,8 +636,7 @@ pub async fn reset_password_post(
form_data: &ResetPasswordForm,
context: Context,
) -> Result<Response> {
event!(
Level::WARN,
warn!(
"Email: {}: {}",
if let Some(ref user) = context.user {
&user.email
@ -779,8 +765,7 @@ pub async fn edit_user_post(
form_data: &EditUserForm,
context: Context,
) -> Result<Response> {
event!(
Level::WARN,
warn!(
"Email: {}: {}",
if let Some(ref user) = context.user {
&user.email
@ -978,7 +963,7 @@ pub async fn email_revalidation(
))
}
error @ db::user::ValidationResult::ValidationExpired => {
event!(Level::WARN, "Token: {}: {}", token, error);
warn!("Token: {}: {}", token, error);
Ok((
jar,
Html(
@ -992,7 +977,7 @@ pub async fn email_revalidation(
))
}
error @ db::user::ValidationResult::UnknownUser => {
event!(Level::WARN, "Email: {}: {}", token, error);
warn!("Email: {}: {}", token, error);
Ok((
jar,
Html(

View file

@ -6,7 +6,7 @@ use ron::de::from_reader;
use serde::Deserialize;
use strum::EnumCount;
use strum_macros::EnumCount;
use tracing::{Level, event};
use tracing::warn;
use crate::consts;
@ -349,11 +349,7 @@ fn get_language_translation(code: &str) -> &'static Language {
}
}
event!(
Level::WARN,
"Unable to find translation for language {}",
code
);
warn!("Unable to find translation for language {}", code);
if code != DEFAULT_LANGUAGE_CODE {
get_language_translation(DEFAULT_LANGUAGE_CODE)