Matthias Beyer: 2 Introduce logging via log crate doc: Add note on how to enable logging 11 files changed, 71 insertions(+), 24 deletions(-)
Copy & paste the following snippet into your terminal to import this patchset into git:
curl -s https://lists.sr.ht/~vpzom/lotide/patches/16126/mbox | git am -3Learn more about email & git
This patch introduces logging via the `log` and `env_logger` crate. All `eprintln!()` calls were replaced with `log::error!()` and all `println!()` calls with `log::debug!()`. Signed-off-by: Matthias Beyer <mail@beyermatthias.de> Tested-by: Matthias Beyer <mail@beyermatthias.de> --- Cargo.lock | 41 ++++++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ src/apub_util/ingest.rs | 2 +- src/apub_util/mod.rs | 8 +++---- src/main.rs | 17 +++++++------- src/migrate.rs | 4 ++-- src/routes/api/mod.rs | 12 +++++----- src/routes/apub/communities.rs | 2 +- src/routes/apub/mod.rs | 2 +- src/tasks.rs | 2 +- 10 files changed, 68 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cb5296d..7d72fd9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -102,6 +102,17 @@ dependencies = [ "syn", ] +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi 0.3.9", +] + [[package]] name = "autocfg" version = "1.0.0" @@ -438,6 +449,19 @@ dependencies = [ "cfg-if 0.1.10", ] +[[package]] +name = "env_logger" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26ecb66b4bdca6c1409b40fb255eefc2bd4f6d135dab3c3124f80ffa2a9661e" +dependencies = [ + "atty", + "humantime", + "log", + "regex", + "termcolor", +] + [[package]] name = "error-chain" version = "0.12.4" @@ -865,6 +889,12 @@ version = "1.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cd179ae861f0c2e53da70d892f5f3029f9594be0c41dc5269cd371691b1dc2f9" +[[package]] +name = "humantime" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c1ad908cc71012b7bea4d0c53ba96a8cba9962f048fa68d143376143d863b7a" + [[package]] name = "hyper" version = "0.13.6" @@ -1110,6 +1140,7 @@ dependencies = [ "chrono", "deadpool-postgres", "either", + "env_logger", "fast_chemail", "fluent", "fluent-langneg", @@ -1122,6 +1153,7 @@ dependencies = [ "hyper-tls", "lazy_static", "lettre", + "log", "migrant_lib", "mime", "openssl", @@ -2133,6 +2165,15 @@ dependencies = [ "utf-8", ] +[[package]] +name = "termcolor" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4" +dependencies = [ + "winapi-util", +] + [[package]] name = "thiserror" version = "1.0.20" diff --git a/Cargo.toml b/Cargo.toml index 5a8d1b3..3d965ec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,6 +52,8 @@ base64 = "0.13.0" pulldown-cmark = "0.8.0" migrant_lib = { version = "0.30.0", features = ["d-postgres"] } pdcm-linkify = "0.1.0" +log = "0.4" +env_logger = "0.8" [dev-dependencies] rand = "0.7.3" diff --git a/src/apub_util/ingest.rs b/src/apub_util/ingest.rs index fb23e5a..e85cdfd 100644 --- a/src/apub_util/ingest.rs +++ b/src/apub_util/ingest.rs @@ -194,7 +194,7 @@ pub async fn ingest_object( ); } } else { - eprintln!("Warning: recieved follow for unknown community"); + log::error!("Warning: recieved follow for unknown community"); } } } diff --git a/src/apub_util/mod.rs b/src/apub_util/mod.rs index 61f9532..7b5414d 100644 --- a/src/apub_util/mod.rs +++ b/src/apub_util/mod.rs @@ -726,7 +726,7 @@ pub fn spawn_announce_community_post( match local_community_post_announce_ap(community, post_local_id, post_ap_id, &ctx.host_url_apub) { Err(err) => { - eprintln!("Failed to create Announce: {:?}", err); + log::error!("Failed to create Announce: {:?}", err); } Ok(announce) => { crate::spawn_task(enqueue_send_to_community_followers( @@ -879,7 +879,7 @@ pub fn spawn_enqueue_send_community_follow_accept( }; let accept = community_follow_accept_to_ap(community_ap_id, follower, follow_ap_id)?; - println!("{:?}", accept); + log::debug!("{:?}", accept); let body = serde_json::to_string(&accept)?; @@ -1400,8 +1400,8 @@ pub async fn check_signature_for_actor( }) }).transpose()?; - println!("signature: {:?}", signature); - println!("found_key: {:?}", found_key.is_some()); + log::debug!("signature: {:?}", signature); + log::debug!("found_key: {:?}", found_key.is_some()); let signature = hancock::Signature::parse(signature)?; diff --git a/src/main.rs b/src/main.rs index b149e19..f7a6c3f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -544,7 +544,7 @@ impl Translator { &mut errors, ); if !errors.is_empty() { - eprintln!("Errors in translation: {:?}", errors); + log::error!("Errors in translation: {:?}", errors); } out @@ -577,7 +577,7 @@ pub fn get_lang_for_req(req: &impl ReqParts) -> Translator { match err { fluent::FluentError::Overriding { .. } => {} _ => { - eprintln!("Failed to add language resource: {:?}", err); + log::error!("Failed to add language resource: {:?}", err); break; } } @@ -660,7 +660,7 @@ pub async fn is_local_user(db: &tokio_postgres::Client, user: UserLocalID) -> Re pub fn spawn_task<F: std::future::Future<Output = Result<(), Error>> + Send + 'static>(task: F) { use futures::future::TryFutureExt; tokio::spawn(task.map_err(|err| { - eprintln!("Error in task: {:?}", err); + log::error!("Error in task: {:?}", err); })); } @@ -681,7 +681,7 @@ pub fn on_local_community_add_post( post_ap_id: url::Url, ctx: Arc<crate::RouteContext>, ) { - println!("on_community_add_post"); + log::debug!("on_community_add_post"); crate::apub_util::spawn_announce_community_post(community, post_local_id, post_ap_id, ctx); } @@ -702,7 +702,7 @@ pub fn on_local_community_add_comment( pub fn on_post_add_comment(comment: CommentInfo<'static>, ctx: Arc<crate::RouteContext>) { use futures::future::TryFutureExt; - println!("on_post_add_comment"); + log::debug!("on_post_add_comment"); spawn_task(async move { let db = ctx.db_pool.get().await?; @@ -909,6 +909,7 @@ pub fn on_post_add_comment(comment: CommentInfo<'static>, ctx: Arc<crate::RouteC } fn main() -> Result<(), Box<dyn std::error::Error>> { + env_logger::init(); let mut args = std::env::args(); args.next(); // discard first element match args.next().as_deref() { @@ -1099,7 +1100,7 @@ async fn run() -> Result<(), Box<dyn std::error::Error>> { Err(Error::UserError(res)) => res, Err(Error::RoutingError(err)) => err.to_simple_response(), Err(Error::Internal(err)) => { - eprintln!("Error: {:?}", err); + log::error!("Error: {:?}", err); simple_response( hyper::StatusCode::INTERNAL_SERVER_ERROR, @@ -1107,7 +1108,7 @@ async fn run() -> Result<(), Box<dyn std::error::Error>> { ) } Err(Error::InternalStr(err)) => { - eprintln!("Error: {}", err); + log::error!("Error: {}", err); simple_response( hyper::StatusCode::INTERNAL_SERVER_ERROR, @@ -1115,7 +1116,7 @@ async fn run() -> Result<(), Box<dyn std::error::Error>> { ) } Err(Error::InternalStrStatic(err)) => { - eprintln!("Error: {}", err); + log::error!("Error: {}", err); simple_response( hyper::StatusCode::INTERNAL_SERVER_ERROR, diff --git a/src/migrate.rs b/src/migrate.rs index 1d2ccd0..b3b754d 100644 --- a/src/migrate.rs +++ b/src/migrate.rs @@ -74,7 +74,7 @@ pub fn run(mut args: std::env::Args) { match action { "up" => { - println!("Applying migrations..."); + log::debug!("Applying migrations..."); migrant_lib::Migrator::with_config(&config) .all(true) .swallow_completion(true) @@ -82,7 +82,7 @@ pub fn run(mut args: std::env::Args) { .expect("Failed to apply migrations"); } "down" => { - println!("Unapplying migration..."); + log::debug!("Unapplying migration..."); migrant_lib::Migrator::with_config(&config) .direction(migrant_lib::Direction::Down) .all(false) diff --git a/src/routes/api/mod.rs b/src/routes/api/mod.rs index ca54ca9..b72f814 100644 --- a/src/routes/api/mod.rs +++ b/src/routes/api/mod.rs @@ -274,7 +274,7 @@ async fn route_unstable_actors_lookup( _req: hyper::Request<hyper::Body>, ) -> Result<hyper::Response<hyper::Body>, crate::Error> { let (query,) = params; - println!("lookup {}", query); + log::debug!("lookup {}", query); let lookup = parse_lookup(&query)?; @@ -289,14 +289,14 @@ async fn route_unstable_actors_lookup( rel: Some("self".into()), })? ); - println!("{}", uri); + log::debug!("{}", uri); let res = ctx .http_client .request(hyper::Request::get(uri).body(Default::default())?) .await?; if res.status() == hyper::StatusCode::NOT_FOUND { - println!("not found"); + log::debug!("not found"); None } else { let res = crate::res_to_error(res).await?; @@ -578,7 +578,7 @@ async fn route_unstable_objects_lookup( _req: hyper::Request<hyper::Body>, ) -> Result<hyper::Response<hyper::Body>, crate::Error> { let (query,) = params; - println!("lookup {}", query); + log::debug!("lookup {}", query); let lookup = parse_lookup(&query)?; @@ -593,14 +593,14 @@ async fn route_unstable_objects_lookup( rel: Some("self".into()), })? ); - println!("{}", uri); + log::debug!("{}", uri); let res = ctx .http_client .request(hyper::Request::get(uri).body(Default::default())?) .await?; if res.status() == hyper::StatusCode::NOT_FOUND { - println!("not found"); + log::debug!("not found"); None } else { let res = crate::res_to_error(res).await?; diff --git a/src/routes/apub/communities.rs b/src/routes/apub/communities.rs index 4a6e5f0..dd249f7 100644 --- a/src/routes/apub/communities.rs +++ b/src/routes/apub/communities.rs @@ -112,7 +112,7 @@ async fn handler_communities_get( .and_then(|bytes| match std::str::from_utf8(bytes) { Ok(key) => Some(key), Err(err) => { - eprintln!("Warning: public_key is not UTF-8: {:?}", err); + log::error!("Warning: public_key is not UTF-8: {:?}", err); None } }); diff --git a/src/routes/apub/mod.rs b/src/routes/apub/mod.rs index e5178ce..d6a9583 100644 --- a/src/routes/apub/mod.rs +++ b/src/routes/apub/mod.rs @@ -143,7 +143,7 @@ async fn handler_users_get( .and_then(|bytes| match std::str::from_utf8(bytes) { Ok(key) => Some(key), Err(err) => { - eprintln!("Warning: public_key is not UTF-8: {:?}", err); + log::error!("Warning: public_key is not UTF-8: {:?}", err); None } }); diff --git a/src/tasks.rs b/src/tasks.rs index be866bd..b802f35 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -82,7 +82,7 @@ impl<'a> TaskDef for DeliverToInbox<'a> { let res = crate::res_to_error(ctx.http_client.request(req).await?).await?; - println!("{:?}", res); + log::debug!("{:?}", res); Ok(()) } -- 2.29.2
Signed-off-by: Matthias Beyer <mail@beyermatthias.de> --- doc/INSTALL.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/doc/INSTALL.md b/doc/INSTALL.md index 2a77ca6..8715690 100644 --- a/doc/INSTALL.md +++ b/doc/INSTALL.md @@ -58,6 +58,9 @@ The following environment variables are required (*An environment variable manag - HOST_URL_ACTIVITYPUB - Typically `http://localhost:3333/apub` for dev instances - HOST_URL_API - Typically `http://localhost:3333/api` for dev instances +Logging can be enabled via the +[env_logger configuration environment variables](https://docs.rs/env_logger/). + Run `cargo run -- migrate setup`, then `cargo run -- migrate` to update the database schema. To build and run lotide, run `cargo run` in the lotide directory. -- 2.29.2