From 7f1b33a567c9f3dca36bc4b3c42500ee49b28dda Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Sun, 3 Jan 2021 19:20:35 +0900 Subject: [PATCH 1/6] Add tracing crate to dependences --- Cargo.lock | 15 +++++++++++++++ Cargo.toml | 1 + plume-common/Cargo.toml | 1 + plume-models/Cargo.toml | 1 + 4 files changed, 18 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 70be0aef..46e6bef3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2598,6 +2598,7 @@ dependencies = [ "serde 1.0.118 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.61 (registry+https://github.com/rust-lang/crates.io-index)", "shrinkwraprs 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "tracing 0.1.22 (registry+https://github.com/rust-lang/crates.io-index)", "validator 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "validator_derive 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "webfinger 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2646,6 +2647,7 @@ dependencies = [ "shrinkwraprs 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "syntect 4.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "tokio 0.1.22 (registry+https://github.com/rust-lang/crates.io-index)", + "tracing 0.1.22 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -2704,6 +2706,7 @@ dependencies = [ "serde_json 1.0.61 (registry+https://github.com/rust-lang/crates.io-index)", "shrinkwraprs 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "tantivy 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", + "tracing 0.1.22 (registry+https://github.com/rust-lang/crates.io-index)", "url 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "walkdir 2.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "webfinger 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -4179,9 +4182,20 @@ dependencies = [ "cfg-if 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)", "pin-project-lite 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "tracing-attributes 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", "tracing-core 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "tracing-attributes" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.56 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "tracing-core" version = "0.1.17" @@ -5071,6 +5085,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum toml 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "758664fc71a3a69038656bee8b6be6477d2a6c315a6b81f7081f591bffa4111f" "checksum tower-service 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e987b6bf443f4b5b3b6f38704195592cca41c5bb7aedd3c3693c7081f8289860" "checksum tracing 0.1.22 (registry+https://github.com/rust-lang/crates.io-index)" = "9f47026cdc4080c07e49b37087de021820269d996f581aac150ef9e5583eefe3" +"checksum tracing-attributes 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "80e0ccfc3378da0cce270c946b676a376943f5cd16aeba64568e7939806f4ada" "checksum tracing-core 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)" = "f50de3927f93d202783f4513cda820ab47ef17f624b03c096e86ef00c67e6b5f" "checksum tracing-futures 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "ab7bb6f14721aa00656086e9335d363c5c8747bae02ebe32ea2c7dece5689b4c" "checksum traitobject 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "efd1f82c56340fdf16f2a953d7bda4f8fdffba13d93b00844c25572110b26079" diff --git a/Cargo.toml b/Cargo.toml index b91c1a79..64a50f03 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,6 +30,7 @@ shrinkwraprs = "0.2.1" validator = "0.8" validator_derive = "0.8" webfinger = "0.4.1" +tracing = "0.1.22" [[bin]] name = "plume" diff --git a/plume-common/Cargo.toml b/plume-common/Cargo.toml index 842121c9..e688182b 100644 --- a/plume-common/Cargo.toml +++ b/plume-common/Cargo.toml @@ -23,6 +23,7 @@ shrinkwraprs = "0.3.0" syntect = "4.5.0" tokio = "0.1.22" regex-syntax = { version = "0.6.17", default-features = false, features = ["unicode-perl"] } +tracing = "0.1.22" [dependencies.chrono] features = ["serde"] diff --git a/plume-models/Cargo.toml b/plume-models/Cargo.toml index 74ac134a..3b4e5501 100644 --- a/plume-models/Cargo.toml +++ b/plume-models/Cargo.toml @@ -32,6 +32,7 @@ shrinkwraprs = "0.2.1" diesel-derive-newtype = "0.1.2" glob = "0.3.0" lindera-tantivy = { version = "0.1.3", optional = true } +tracing = "0.1.22" [dependencies.chrono] features = ["serde"] From 29f2239e3f80ae1e4f6f13aa9c944bc36a59ea58 Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Tue, 5 Jan 2021 13:02:05 +0900 Subject: [PATCH 2/6] Extract dotenv initialization from init_pool because it affects not only database --- src/main.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index ba1c17a6..0ab55ec3 100755 --- a/src/main.rs +++ b/src/main.rs @@ -50,12 +50,6 @@ compile_i18n!(); /// Initializes a database pool. fn init_pool() -> Option { - match dotenv::dotenv() { - Ok(path) => println!("Configuration read from {}", path.display()), - Err(ref e) if e.not_found() => eprintln!("no .env was found"), - e => e.map(|_| ()).unwrap(), - } - let manager = ConnectionManager::::new(CONFIG.database_url.as_str()); let mut builder = DbPool::builder() .connection_customizer(Box::new(PragmaForeignKey)) @@ -69,6 +63,12 @@ fn init_pool() -> Option { } fn main() { + match dotenv::dotenv() { + Ok(path) => println!("Configuration read from {}", path.display()), + Err(ref e) if e.not_found() => eprintln!("no .env was found"), + e => e.map(|_| ()).unwrap(), + } + App::new("Plume") .bin_name("plume") .version(env!("CARGO_PKG_VERSION")) From 30093d3a9f80a31a4d656f984aa155adf082e521 Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Tue, 5 Jan 2021 20:39:05 +0900 Subject: [PATCH 3/6] Initialize Rocket early to initialize logger early --- src/main.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 0ab55ec3..d61f57b4 100755 --- a/src/main.rs +++ b/src/main.rs @@ -82,6 +82,8 @@ and https://docs.joinplu.me/installation/init for more info. "#, ) .get_matches(); + // Initialize Rocket early to load its internal logger + let rocket = rocket::custom(CONFIG.rocket.clone().unwrap()); let dbpool = init_pool().expect("main: database pool initialization error"); if IMPORTED_MIGRATIONS .is_pending(&dbpool.get().unwrap()) @@ -175,7 +177,7 @@ Then try to restart Plume println!("Please refer to the documentation to see how to configure it."); } - let rocket = rocket::custom(CONFIG.rocket.clone().unwrap()) + let rocket = rocket .mount( "/", routes![ From ee96d2b7540406f8dad445c0c67df945b34c9085 Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Tue, 5 Jan 2021 20:52:48 +0900 Subject: [PATCH 4/6] Fix a typo --- src/routes/errors.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes/errors.rs b/src/routes/errors.rs index 53baeef0..e15d2319 100644 --- a/src/routes/errors.rs +++ b/src/routes/errors.rs @@ -49,7 +49,7 @@ pub fn server_error(req: &Request<'_>) -> Ructe { #[post("/csrf-violation?")] pub fn csrf_violation(target: Option, rockets: PlumeRocket) -> Ructe { if let Some(uri) = target { - eprintln!("Csrf violation while acceding \"{}\"", uri) + eprintln!("Csrf violation while accessing \"{}\"", uri) } render!(errors::csrf(&rockets.to_context())) } From becb40544cb59ac92208ca356bf4557c70baf285 Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Tue, 5 Jan 2021 21:41:14 +0900 Subject: [PATCH 5/6] Replace println!()s with logging macros --- plume-common/src/activity_pub/mod.rs | 7 ++++--- plume-models/src/migrations.rs | 5 +++-- src/inbox.rs | 5 +++-- src/main.rs | 7 ++++--- src/routes/errors.rs | 3 ++- src/routes/session.rs | 3 ++- src/routes/user.rs | 5 +++-- 7 files changed, 21 insertions(+), 14 deletions(-) diff --git a/plume-common/src/activity_pub/mod.rs b/plume-common/src/activity_pub/mod.rs index 58c270e2..423cc4ff 100644 --- a/plume-common/src/activity_pub/mod.rs +++ b/plume-common/src/activity_pub/mod.rs @@ -1,5 +1,6 @@ use activitypub::{Activity, Link, Object}; use array_tool::vec::Uniq; +use tracing::{debug, warn}; use reqwest::r#async::ClientBuilder; use rocket::{ http::Status, @@ -153,10 +154,10 @@ where .send() .and_then(|r| r.into_body().concat2()) .map(move |response| { - println!("Successfully sent activity to inbox ({})", inbox); - println!("Response: \"{:?}\"\n", response) + debug!("Successfully sent activity to inbox ({})", inbox); + debug!("Response: \"{:?}\"\n", response) }) - .map_err(|e| println!("Error while sending to inbox ({:?})", e)), + .map_err(|e| warn!("Error while sending to inbox ({:?})", e)), ); } rt.run().unwrap(); diff --git a/plume-models/src/migrations.rs b/plume-models/src/migrations.rs index ab8788de..f7fd05dc 100644 --- a/plume-models/src/migrations.rs +++ b/plume-models/src/migrations.rs @@ -1,5 +1,6 @@ use crate::{Connection, Error, Result}; use diesel::connection::{Connection as Conn, SimpleConnection}; +use tracing::info; use migrations_internals::{setup_database, MigrationConnection}; use std::path::Path; @@ -26,7 +27,7 @@ struct ComplexMigration { impl ComplexMigration { fn run(&self, conn: &Connection, path: &Path) -> Result<()> { - println!("Running migration {}", self.name); + info!("Running migration {}", self.name); for step in self.up { step.run(conn, path)? } @@ -34,7 +35,7 @@ impl ComplexMigration { } fn revert(&self, conn: &Connection, path: &Path) -> Result<()> { - println!("Reverting migration {}", self.name); + info!("Reverting migration {}", self.name); for step in self.down { step.run(conn, path)? } diff --git a/src/inbox.rs b/src/inbox.rs index 71270f53..ecb6e0e6 100644 --- a/src/inbox.rs +++ b/src/inbox.rs @@ -1,3 +1,4 @@ +use tracing::warn; use plume_common::activity_pub::{ inbox::FromId, request::Digest, @@ -41,7 +42,7 @@ pub fn handle_incoming( } }) .map_err(|_| { - println!( + warn!( "Rejected invalid activity supposedly from {}, with headers {:?}", actor.username, headers.0 ); @@ -58,7 +59,7 @@ pub fn handle_incoming( Ok(match inbox(&rockets, act) { Ok(_) => String::new(), Err(e) => { - println!("Shared inbox error: {:?}", e); + warn!("Shared inbox error: {:?}", e); format!("Error: {:?}", e) } }) diff --git a/src/main.rs b/src/main.rs index d61f57b4..5b04b4a2 100755 --- a/src/main.rs +++ b/src/main.rs @@ -13,6 +13,7 @@ extern crate validator_derive; use chrono::Utc; use clap::App; use diesel::r2d2::ConnectionManager; +use tracing::warn; use plume_models::{ db_conn::{DbPool, PragmaForeignKey}, instance::Instance, @@ -114,7 +115,7 @@ Then try to restart Plume. .expect("main: error on backing up search index directory for recreating"); if UnmanagedSearcher::create(&CONFIG.search_index, &CONFIG.search_tokenizers).is_ok() { if fs::remove_dir_all(backup_path).is_err() { - eprintln!( + warn!( "error on removing backup directory: {}. it remains", backup_path.display() ); @@ -173,8 +174,8 @@ Then try to restart Plume let mail = mail::init(); if mail.is_none() && CONFIG.rocket.as_ref().unwrap().environment.is_prod() { - println!("Warning: the email server is not configured (or not completely)."); - println!("Please refer to the documentation to see how to configure it."); + warn!("Warning: the email server is not configured (or not completely)."); + warn!("Please refer to the documentation to see how to configure it."); } let rocket = rocket diff --git a/src/routes/errors.rs b/src/routes/errors.rs index e15d2319..8549da80 100644 --- a/src/routes/errors.rs +++ b/src/routes/errors.rs @@ -1,4 +1,5 @@ use crate::template_utils::{IntoContext, Ructe}; +use tracing::warn; use plume_models::{Error, PlumeRocket}; use rocket::{ response::{self, Responder}, @@ -49,7 +50,7 @@ pub fn server_error(req: &Request<'_>) -> Ructe { #[post("/csrf-violation?")] pub fn csrf_violation(target: Option, rockets: PlumeRocket) -> Ructe { if let Some(uri) = target { - eprintln!("Csrf violation while accessing \"{}\"", uri) + warn!("Csrf violation while accessing \"{}\"", uri) } render!(errors::csrf(&rockets.to_context())) } diff --git a/src/routes/session.rs b/src/routes/session.rs index 27902a46..4d1dfe73 100644 --- a/src/routes/session.rs +++ b/src/routes/session.rs @@ -1,5 +1,6 @@ use crate::routes::RespondOrRedirect; use lettre::Transport; +use tracing::warn; use rocket::http::ext::IntoOwned; use rocket::{ http::{uri::Uri, Cookie, Cookies, SameSite}, @@ -155,7 +156,7 @@ pub fn password_reset_request( ) { if let Some(ref mut mail) = *mail.lock().unwrap() { mail.send(message.into()) - .map_err(|_| eprintln!("Couldn't send password reset email")) + .map_err(|_| warn!("Couldn't send password reset email")) .ok(); } } diff --git a/src/routes/user.rs b/src/routes/user.rs index 39466d01..5164442e 100644 --- a/src/routes/user.rs +++ b/src/routes/user.rs @@ -3,6 +3,7 @@ use activitypub::{ collection::{OrderedCollection, OrderedCollectionPage}, }; use diesel::SaveChangesDsl; +use tracing::{info, warn}; use rocket::{ http::{ContentType, Cookies}, request::LenientForm, @@ -67,9 +68,9 @@ pub fn details( Ok(article) => { Post::from_activity(&fetch_rockets, article) .expect("Article from remote user couldn't be saved"); - println!("Fetched article from remote user"); + info!("Fetched article from remote user"); } - Err(e) => println!("Error while fetching articles in background: {:?}", e), + Err(e) => warn!("Error while fetching articles in background: {:?}", e), } } }); From c1abd3d6f99c1f5a6c77f8b2bce5a021dfb3ef1c Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Tue, 5 Jan 2021 21:41:43 +0900 Subject: [PATCH 6/6] Use eprintln! instead of println! for console message --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 5b04b4a2..23e24799 100755 --- a/src/main.rs +++ b/src/main.rs @@ -65,7 +65,7 @@ fn init_pool() -> Option { fn main() { match dotenv::dotenv() { - Ok(path) => println!("Configuration read from {}", path.display()), + Ok(path) => eprintln!("Configuration read from {}", path.display()), Err(ref e) if e.not_found() => eprintln!("no .env was found"), e => e.map(|_| ()).unwrap(), }