From 5a2dc4070c1d14f22a7671c6f82ca2f4578369bd Mon Sep 17 00:00:00 2001 From: Nathan Perry Date: Fri, 16 Aug 2024 06:40:25 -0400 Subject: rework logging to use tracing via grate --- src/commands/meme/create.rs | 20 ++++++++------------ src/commands/meme/delete.rs | 4 ++-- src/commands/meme/history.rs | 25 ++++++++++++------------- src/commands/meme/invoke.rs | 8 ++++---- src/commands/meme/mod.rs | 4 ++-- 5 files changed, 28 insertions(+), 33 deletions(-) (limited to 'src/commands/meme') diff --git a/src/commands/meme/create.rs b/src/commands/meme/create.rs index 185e1f6..a4e9656 100644 --- a/src/commands/meme/create.rs +++ b/src/commands/meme/create.rs @@ -2,11 +2,7 @@ use std::process::Stdio; use anyhow::anyhow; use diesel::result::Error as DieselError; -use log::{ - debug, - error, - warn, -}; +use grate::tracing; use serenity::all::ReactionType; use tap::Pipe; use tokio::{ @@ -40,7 +36,7 @@ pub async fn addmeme( let image = util::msg(ctx).and_then(|msg| msg.attachments.first()); if image.is_none() && text.is_none() { - warn!("tried to create non-audio meme with no image or text"); + tracing::warn!("tried to create non-audio meme with no image or text"); util::reply(ctx, "hahAA it's empty xdddd").await?; return Ok(()); @@ -74,7 +70,7 @@ pub async fn addmeme( if let Some(DieselError::DatabaseError(DatabaseErrorKind::UniqueViolation, _)) = e.downcast_ref::() { - error!("tried to create meme that already exists"); + tracing::error!("tried to create meme that already exists"); util::react(ctx, '❌').await?; util::reply(ctx, "that meme already exists").await?; @@ -97,7 +93,7 @@ pub async fn addaudiomeme( audio_str: String, #[rest] text: Option, ) -> anyhow::Result<()> { - debug!("running addaudiomeme"); + tracing::debug!("running addaudiomeme"); let elems = audio_str.split_whitespace().collect::>(); @@ -113,7 +109,7 @@ pub async fn addaudiomeme( util::react(ctx, '🔃').await?; let youtube_url = util::ytdl_url(audio_link.as_str()).await?; - debug!("got download url: {youtube_url}"); + tracing::debug!("got download url: {youtube_url}"); let duration_opts = if let Some(e) = end { vec![ @@ -166,10 +162,10 @@ pub async fn addaudiomeme( let mut audio_data = Vec::new(); let bytes = audio_reader.read_to_end(&mut audio_data).await?; - debug!("downloaded audio ({} bytes)", audio_data.len()); + tracing::debug!("downloaded audio ({} bytes)", audio_data.len()); if bytes == 0 { - debug!("read 0 bytes from audio reader"); + tracing::debug!("read 0 bytes from audio reader"); util::unreact(ctx, '🔃').await?; util::reply(ctx, "🔇🔇🔇🔕🔕🔕🔕🔕🔇🔕🔕🔇🔕🔕📣📢📣📢📣").await?; @@ -200,7 +196,7 @@ pub async fn addaudiomeme( if let Some(DieselError::DatabaseError(DatabaseErrorKind::UniqueViolation, _)) = e.downcast_ref::() { - error!("tried to create meme that already exists"); + tracing::error!("tried to create meme that already exists"); util::react(ctx, ReactionType::Unicode("❌".to_owned())).await?; util::reply(ctx, "that meme already exists").await?; diff --git a/src/commands/meme/delete.rs b/src/commands/meme/delete.rs index 4769cc8..560da28 100644 --- a/src/commands/meme/delete.rs +++ b/src/commands/meme/delete.rs @@ -2,7 +2,7 @@ use diesel::{ result::Error as DieselError, NotFound, }; -use log::info; +use grate::tracing; use serenity::all::ReactionType; use crate::{ @@ -26,7 +26,7 @@ pub async fn delmeme(ctx: PoiseContext<'_>, title: String) -> anyhow::Result<()> }, Err(e) => { if let Some(NotFound) = e.downcast_ref::() { - info!("attempted to delete nonexistent meme: '{}'", title); + tracing::info!("attempted to delete nonexistent meme: '{}'", title); util::react(ctx, ReactionType::Unicode("❓".to_owned())).await?; util::reply(ctx, "nice try").await?; diff --git a/src/commands/meme/history.rs b/src/commands/meme/history.rs index 68416e5..e3d0de3 100644 --- a/src/commands/meme/history.rs +++ b/src/commands/meme/history.rs @@ -3,13 +3,9 @@ use diesel::{ result::Error as DieselError, NotFound, }; +use grate::tracing; use itertools::Itertools; use lazy_static::lazy_static; -use log::{ - debug, - error, - info, -}; use serenity::{ futures::{ StreamExt, @@ -58,7 +54,7 @@ pub async fn wat(ctx: PoiseContext<'_>) -> anyhow::Result<()> { Ok(x) => x, Err(e) => { if let Some(NotFound) = e.downcast_ref::() { - info!("found no memes in history"); + tracing::info!("found no memes in history"); util::reply(ctx, "no one has ever memed before").await?; return Ok(()); @@ -89,7 +85,7 @@ pub async fn wat(ctx: PoiseContext<'_>) -> anyhow::Result<()> { }, Err(e) => { if let Some(NotFound) = e.downcast_ref::() { - info!("last meme not found in database"); + tracing::info!("last meme not found in database"); util::reply(ctx, "heuueueeeeh?").await?; return Ok(()); @@ -110,7 +106,10 @@ pub async fn history(ctx: PoiseContext<'_>, n: Option) -> anyhow::Result< let n = n.unwrap_or(CONFIG.default_hist); if n > CONFIG.max_hist { - debug!("user requested more than MAX_HIST ({}) items from history", CONFIG.max_hist); + tracing::debug!( + "user requested more than MAX_HIST ({}) items from history", + CONFIG.max_hist + ); util::reply(ctx, "YER PUSHIN ME OVER THE FUCKIN LINE").await?; } @@ -122,13 +121,13 @@ pub async fn history(ctx: PoiseContext<'_>, n: Option) -> anyhow::Result< }; if records.is_empty() { - info!("no memes in history"); + tracing::info!("no memes in history"); util::reply(ctx, "i don't remember anything :(").await?; return Ok(()); } - info!("reporting meme history (len {})", n); + tracing::info!("reporting meme history (len {})", n); let resp = serenity::futures::stream::iter(records.into_iter().enumerate().rev()) .then(|(i, rec)| async move { @@ -182,7 +181,7 @@ pub async fn history(ctx: PoiseContext<'_>, n: Option) -> anyhow::Result< Err(e) => { if let Some(variant) = e.downcast_ref::() { if *variant != NotFound { - error!("error encountered loading meme history: {}", e); + tracing::error!("error encountered loading meme history: {}", e); } } @@ -213,7 +212,7 @@ pub async fn stats(ctx: PoiseContext<'_>) -> anyhow::Result<()> { let mut conn = connection().await?; let stats = db::stats(&mut conn).await?; - debug!("reporting stats"); + tracing::debug!("reporting stats"); let rand_user: User = UserId::new(stats.most_random_meme_user).to_user(&ctx).await?; let direct_user: User = UserId::new(stats.most_directly_named_meme_user).to_user(&ctx).await?; @@ -408,7 +407,7 @@ pub async fn query(ctx: PoiseContext<'_>, rest: util::RestVec) -> anyhow::Result .join("\n"); if result.is_empty() { - info!("no memes matched query"); + tracing::info!("no memes matched query"); util::reply(ctx, "no match").await?; return Ok(()); diff --git a/src/commands/meme/invoke.rs b/src/commands/meme/invoke.rs index 1d9040d..37a78f5 100644 --- a/src/commands/meme/invoke.rs +++ b/src/commands/meme/invoke.rs @@ -2,7 +2,7 @@ use diesel::{ result::Error as DieselError, NotFound, }; -use log::info; +use grate::tracing; use crate::{ commands::meme::send_meme, @@ -89,7 +89,7 @@ pub(crate) async fn _meme( }, Err(e) => { return if let Some(NotFound) = e.downcast_ref::() { - info!("requested meme not found in database"); + tracing::info!("requested meme not found in database"); util::reply(ctx, "c'mon baby, guesstimate").await?; Ok(()) @@ -123,7 +123,7 @@ async fn rand_meme(ctx: PoiseContext<'_>, audio_playback: AudioPlayback) -> anyh }, Err(e) => { if let Some(NotFound) = e.downcast_ref::() { - info!("random meme not found"); + tracing::info!("random meme not found"); util::reply(ctx, "i don't know any :(").await?; return Ok(()); @@ -151,7 +151,7 @@ pub async fn rare_meme(ctx: PoiseContext<'_>) -> anyhow::Result<()> { }, Err(e) => { if let Some(NotFound) = e.downcast_ref::() { - info!("rare meme not found"); + tracing::info!("rare meme not found"); util::reply(ctx, "i don't know any :(").await?; return Ok(()); diff --git a/src/commands/meme/mod.rs b/src/commands/meme/mod.rs index ca7714a..4819c1d 100644 --- a/src/commands/meme/mod.rs +++ b/src/commands/meme/mod.rs @@ -1,5 +1,5 @@ use diesel_async::AsyncPgConnection; -use log::debug; +use grate::tracing; use rand::random; use serenity::{ all::ReactionType, @@ -70,7 +70,7 @@ async fn send_meme( let should_tts = t.content.as_ref().map(|t| !t.is_empty()).unwrap_or(false) && random::() % 25 == 0; - debug!("sending meme (tts: {}): {:?}", should_tts, t); + tracing::debug!("sending meme (tts: {}): {:?}", should_tts, t); let image = t.image(conn).await; let audio = t.audio(conn).await; -- cgit v1.3.1