From 2ab33f22eee2c44394f9de501643a7d612f1e63d Mon Sep 17 00:00:00 2001 From: silverpill Date: Tue, 21 Dec 2021 00:14:12 +0000 Subject: [PATCH] Add log_level config parameter and improve logging of activities --- Cargo.lock | 5 +++-- Cargo.toml | 4 ++-- src/activitypub/deliverer.rs | 11 +++++++---- src/activitypub/receiver.rs | 4 ++-- src/activitypub/views.rs | 16 +++++++++------- src/bin/mitractl.rs | 2 +- src/config.rs | 6 ++++++ src/logger.rs | 13 ++++++++++--- src/main.rs | 2 +- 9 files changed, 41 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6a401d6..7f66489 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -868,9 +868,9 @@ dependencies = [ [[package]] name = "env_logger" -version = "0.8.4" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a19187fea3ac7e84da7dacf48de0c45d63c6a76f9490dae389aead16c243fce3" +checksum = "0b2cf0344971ee6c64c31be0d530793fba457d322dfec2810c453d0ef228f9c3" dependencies = [ "log", ] @@ -1526,6 +1526,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" dependencies = [ "cfg-if 1.0.0", + "serde", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index adc5b16..63b0a31 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,8 +34,8 @@ dotenv = "0.15.0" # Used to work with hexadecimal strings hex = "0.4.3" # Used for logging -log = "0.4.14" -env_logger = { version = "0.8.4", default-features = false } +log = { version = "0.4.14", features = ["serde"] } +env_logger = { version = "0.9.0", default-features = false } # Used to guess media type of a file mime_guess = "2.0.3" mime-sniffer = "0.1.2" diff --git a/src/activitypub/deliverer.rs b/src/activitypub/deliverer.rs index 461751e..d26c5ae 100644 --- a/src/activitypub/deliverer.rs +++ b/src/activitypub/deliverer.rs @@ -35,7 +35,6 @@ async fn send_activity( activity_json: &str, inbox_url: &str, ) -> Result<(), DelivererError> { - log::info!("sending activity to {}: {}", inbox_url, activity_json); let headers = create_http_signature( Method::POST, inbox_url, @@ -54,7 +53,7 @@ async fn send_activity( .body(activity_json.to_owned()); if instance.is_private { - log::info!( + log::debug!( "private mode: not sending activity to {}", inbox_url, ); @@ -94,6 +93,7 @@ async fn deliver_activity_worker( .collect(); inboxes.sort(); inboxes.dedup(); + log::info!("sending activity to {} inboxes: {}", inboxes.len(), activity_json); for inbox_url in inboxes { // TODO: retry on error if let Err(err) = send_activity( @@ -103,8 +103,8 @@ async fn deliver_activity_worker( &activity_json, &inbox_url, ).await { - log::error!("{}", err); - } + log::error!("failed to deliver activity to {}: {}", inbox_url, err); + }; }; Ok(()) } @@ -115,6 +115,9 @@ pub fn deliver_activity( activity: Activity, recipients: Vec, ) -> () { + if recipients.is_empty() { + return; + }; let instance = config.instance(); let sender = sender.clone(); actix_rt::spawn(async move { diff --git a/src/activitypub/receiver.rs b/src/activitypub/receiver.rs index 71c664f..0e7b11b 100644 --- a/src/activitypub/receiver.rs +++ b/src/activitypub/receiver.rs @@ -338,9 +338,9 @@ pub async fn process_note( pub async fn receive_activity( config: &Config, db_pool: &Pool, - activity_raw: Value, + activity_raw: &Value, ) -> Result<(), HttpError> { - let activity: Activity = serde_json::from_value(activity_raw) + let activity: Activity = serde_json::from_value(activity_raw.clone()) .map_err(|_| ValidationError("invalid activity"))?; let activity_type = activity.activity_type; let maybe_object_type = activity.object.get("type") diff --git a/src/activitypub/views.rs b/src/activitypub/views.rs index 0048d05..3ff9ac1 100644 --- a/src/activitypub/views.rs +++ b/src/activitypub/views.rs @@ -92,20 +92,22 @@ async fn inbox( request: HttpRequest, activity: web::Json, ) -> Result { + let activity_type = activity["type"].as_str().unwrap_or("Unknown"); + log::info!("received in {}: {}", request.uri().path(), activity_type); + log::debug!("received activity: {}", activity); let signature_verified = verify_http_signature(&config, &db_pool, &request).await; - if activity["type"].as_str() == Some(DELETE) && signature_verified.is_err() { - // Don't log Delete() activities if HTTP signature is not valid + if activity_type == DELETE && signature_verified.is_err() { + // Ignore Delete() activities if HTTP signature is not valid log::info!("received in {}: Delete", request.uri().path()); - } else { - log::info!("received in {}: {}", request.uri().path(), activity); + return Ok(HttpResponse::Ok().finish()); }; match signature_verified { - Ok(signer_id) => log::info!("activity signed by {}", signer_id), + Ok(signer_id) => log::debug!("activity signed by {}", signer_id), Err(err) => log::warn!("invalid signature: {}", err), }; - receive_activity(&config, &db_pool, activity.into_inner()).await + receive_activity(&config, &db_pool, &activity).await .map_err(|err| { - log::info!("failed to process activity: {}", err); + log::warn!("failed to process activity: {}; {}", err, activity); err })?; Ok(HttpResponse::Ok().finish()) diff --git a/src/bin/mitractl.rs b/src/bin/mitractl.rs index ad2d304..6ecf66a 100644 --- a/src/bin/mitractl.rs +++ b/src/bin/mitractl.rs @@ -86,7 +86,7 @@ async fn main() { subcmd => { // Other commands require initialized app let config = config::parse_config(); - configure_logger(); + configure_logger(config.log_level); let db_pool = create_pool(&config.database_url); apply_migrations(&db_pool).await; let db_client = &mut **get_database_client(&db_pool).await.unwrap(); diff --git a/src/config.rs b/src/config.rs index 8523450..0f7c91c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,6 +1,7 @@ use std::path::PathBuf; use std::str::FromStr; +use log::{Level as LogLevel}; use rsa::RsaPrivateKey; use serde::{de, Deserialize, Deserializer}; use url::Url; @@ -61,6 +62,8 @@ fn parse_env() -> EnvConfig { fn default_environment() -> Environment { Environment::Development } +fn default_log_level() -> LogLevel { LogLevel::Info } + #[derive(Clone, Deserialize)] pub struct EthereumContract { pub address: String, @@ -84,6 +87,9 @@ pub struct Config { pub http_host: String, pub http_port: u32, + #[serde(default = "default_log_level")] + pub log_level: LogLevel, + // Instance info instance_uri: String, pub instance_title: String, diff --git a/src/logger.rs b/src/logger.rs index 2b80a82..a5c68bb 100644 --- a/src/logger.rs +++ b/src/logger.rs @@ -1,7 +1,13 @@ -use chrono::Local; use std::io::Write; -pub fn configure_logger() -> () { +use log::Level; +use chrono::Local; + +pub fn configure_logger(base_level: Level) -> () { + let actix_level = match base_level { + Level::Info => Level::Warn, + other_level => other_level, + }; env_logger::Builder::new() .format(|buf, record| { writeln!(buf, @@ -12,6 +18,7 @@ pub fn configure_logger() -> () { record.args(), ) }) - .filter(None, log::LevelFilter::Info) + .filter_level(base_level.to_level_filter()) + .filter_module("actix_web::middleware::logger", actix_level.to_level_filter()) .init(); } diff --git a/src/main.rs b/src/main.rs index cfc6917..7746a1e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,7 +30,7 @@ const MAX_UPLOAD_SIZE: usize = 1024 * 1024 * 10; #[actix_web::main] async fn main() -> std::io::Result<()> { let config = parse_config(); - configure_logger(); + configure_logger(config.log_level); let db_pool = create_pool(&config.database_url); apply_migrations(&db_pool).await; if !config.media_dir().exists() {