From 1e8c9e61d646aa60a6c6c8cca08c85f7d74dcdd8 Mon Sep 17 00:00:00 2001 From: Loudbook Date: Thu, 12 Dec 2024 14:09:38 -0500 Subject: [PATCH] Improve logging --- backend/src/database/aws_service.rs | 5 +++-- backend/src/database/migration_service.rs | 21 +++++++++++---------- backend/src/database/mongodb_service.rs | 5 +++-- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/backend/src/database/aws_service.rs b/backend/src/database/aws_service.rs index 7a5073f..bca9f39 100644 --- a/backend/src/database/aws_service.rs +++ b/backend/src/database/aws_service.rs @@ -2,6 +2,7 @@ use anyhow::Result; use aws_sdk_s3::config::{Credentials, Region, SharedCredentialsProvider}; use aws_sdk_s3::primitives::ByteStream; use aws_sdk_s3::{Client, Config}; +use log::info; pub struct AWSService { client: Client, @@ -10,7 +11,7 @@ pub struct AWSService { impl AWSService { pub async fn new(endpoint: &str, bucket_name: &str, access_key: &str, secret_key: &str) -> Result { - println!("Connecting to AWS S3..."); + info!("Connecting to AWS S3..."); let region = Region::new("auto"); let credentials = Credentials::from_keys(access_key, secret_key, None); let shared_credentials = SharedCredentialsProvider::new(credentials); @@ -25,7 +26,7 @@ impl AWSService { let client = Client::from_conf(config); - println!("Connected to AWS S3."); + info!("Connected to AWS S3."); Ok(Self { client, diff --git a/backend/src/database/migration_service.rs b/backend/src/database/migration_service.rs index 65d404b..4a026ab 100644 --- a/backend/src/database/migration_service.rs +++ b/backend/src/database/migration_service.rs @@ -1,6 +1,7 @@ use crate::models::paste::Paste; use crate::models::user::User; use futures_util::StreamExt; +use log::{error, info}; use mongodb::bson::{doc, Document}; use mongodb::{Collection, Database}; @@ -22,17 +23,17 @@ impl MigrationService { } pub async fn run_migrations(&self) { - self.user_migration_12_11_24().await.map_err(|e| println!("Error migrating users: {}", e)).ok(); - self.paste_migration_12_11_24().await.map_err(|e| println!("Error migrating pastes: {}", e)).ok(); + self.user_migration_12_11_24().await.map_err(|e| error!("Error migrating users: {}", e)).ok(); + self.paste_migration_12_11_24().await.map_err(|e| error!("Error migrating pastes: {}", e)).ok(); } pub async fn user_migration_12_11_24(&self) -> Result<(), mongodb::error::Error> { if !self.database.list_collection_names().await?.contains(&"user".to_string()) { - println!("No users to migrate."); + info!("No users to migrate."); return Ok(()); } - println!("Migrating users..."); + info!("Migrating users..."); let old_user_collection = self.database.collection::("user"); let mut amount = 0; @@ -48,7 +49,7 @@ impl MigrationService { let last_visit = old_user.get_i64("lastVisit").unwrap_or_default(); if user_ip.is_empty() || user_id.is_empty() { - println!("Skipping user with empty IP or ID."); + info!("Skipping user with empty IP or ID."); continue; } @@ -65,7 +66,7 @@ impl MigrationService { amount += 1; } - println!("Migrated {} out of {} users", amount, target_amount); + info!("Migrated {} out of {} users", amount, target_amount); let qualified_old_database_name = self.database.name().to_owned() + ".user"; let qualified_new_database_name = self.database.name().to_owned() + ".user_migrated_12_11_24"; @@ -81,11 +82,11 @@ impl MigrationService { pub async fn paste_migration_12_11_24(&self) -> Result<(), mongodb::error::Error> { if !self.database.list_collection_names().await?.contains(&"pastePrivateDTO".to_string()) { - println!("No pastes to migrate."); + info!("No pastes to migrate."); return Ok(()); } - println!("Migrating pastes..."); + info!("Migrating pastes..."); let old_paste_collection = self.database.collection::("pastePrivateDTO"); let mut amount = 0; @@ -102,7 +103,7 @@ impl MigrationService { let paste_wrap = old_paste.get_bool("wrap").unwrap_or_default(); if paste_id.is_empty() { - println!("Skipping paste with empty ID."); + info!("Skipping paste with empty ID."); continue; } @@ -121,7 +122,7 @@ impl MigrationService { amount += 1; } - println!("Migrated {} out of {} pastes", amount, target_amount); + info!("Migrated {} out of {} pastes", amount, target_amount); let qualified_old_database_name = self.database.name().to_owned() + ".pastePrivateDTO"; let qualified_new_database_name = self.database.name().to_owned() + ".pastePrivateDTO_migrated_12_11_24"; diff --git a/backend/src/database/mongodb_service.rs b/backend/src/database/mongodb_service.rs index 771ec2c..7bb7222 100644 --- a/backend/src/database/mongodb_service.rs +++ b/backend/src/database/mongodb_service.rs @@ -1,3 +1,4 @@ +use log::info; use mongodb::bson::{doc, uuid}; use mongodb::{Client, Collection, Cursor}; use mongodb::options::ClientOptions; @@ -13,7 +14,7 @@ pub struct MongoService { impl MongoService { pub async fn new(uri: &str, db_name: &str) -> MongoResult { - println!("Connecting to MongoDB..."); + info!("Connecting to MongoDB..."); let client_options = ClientOptions::parse(uri).await?; let client = Client::with_options(client_options)?; @@ -22,7 +23,7 @@ impl MongoService { let user_collection = database.collection::("users"); let paste_collection = database.collection::("pastes"); - println!("Connected to MongoDB."); + info!("Connected to MongoDB."); let migration_service = MigrationService::new(&database, &admin_database, &user_collection, &paste_collection); migration_service.run_migrations().await;