From f735b90e741333eaaae7ad8ad28bdbb804b6f328 Mon Sep 17 00:00:00 2001 From: me Date: Tue, 17 Dec 2024 10:47:00 +0200 Subject: [PATCH] pretty json object logs --- app/Routes.hs | 4 ++-- fedi.cabal | 1 + src/Fedi/Helpers.hs | 9 +++++++++ src/Fedi/Requests.hs | 3 +++ 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/app/Routes.hs b/app/Routes.hs index 75d810c..82a2b0a 100644 --- a/app/Routes.hs +++ b/app/Routes.hs @@ -130,7 +130,7 @@ noteToCreate note = Fedi.makeCreateNote note handleInbox :: DB -> FilePath -> Fedi.AnyActivity -> Twain.ResponderM Twain.Response handleInbox db detailsFile activity = do details <- liftIO $ fetchUserDetails detailsFile - Log.logDebug (Fedi.pShow activity) + Log.logDebug (Fedi.pJson activity) case activity of Fedi.ActivityFollow follow -> do let @@ -197,7 +197,7 @@ handleInbox db detailsFile activity = do sendFollowers :: Fedi.UserDetails -> DB -> Fedi.AnyActivity -> IO () sendFollowers details db message = do - Log.logDebug $ "Sending to followers: " <> Fedi.pShow message + Log.logDebug $ "Sending to followers: " <> Fedi.pJson message followers <- db.getFollowers Fedi.for_ followers \follower -> do Async.async $ do diff --git a/fedi.cabal b/fedi.cabal index 4adf968..3921133 100644 --- a/fedi.cabal +++ b/fedi.cabal @@ -66,6 +66,7 @@ library , http-types , simple-logger , pretty-simple + , aeson-pretty hs-source-dirs: src default-language: GHC2021 diff --git a/src/Fedi/Helpers.hs b/src/Fedi/Helpers.hs index 72cad65..c74d195 100644 --- a/src/Fedi/Helpers.hs +++ b/src/Fedi/Helpers.hs @@ -19,6 +19,9 @@ import Data.Function as Export import Control.Monad.Catch as Export (throwM, Exception, MonadThrow) import Text.Pretty.Simple qualified as PS import Data.Text.Lazy qualified as TL +import Data.Text.Lazy.Encoding qualified as TL +import Data.Aeson qualified as A +import Data.Aeson.Encode.Pretty qualified as AP data Error = Error String @@ -29,3 +32,9 @@ throw = throwM . Error pShow :: Show a => a -> Text pShow = TL.toStrict . PS.pShow + +pJson :: A.ToJSON a => a -> Text +pJson = + TL.toStrict + . TL.decodeUtf8 + . AP.encodePretty' AP.defConfig { AP.confIndent = AP.Spaces 2 } diff --git a/src/Fedi/Requests.hs b/src/Fedi/Requests.hs index 960500b..372a4b4 100644 --- a/src/Fedi/Requests.hs +++ b/src/Fedi/Requests.hs @@ -21,6 +21,9 @@ sendPost -> IO ByteString sendPost details url payload = do uri <- URI.mkURI $ fromString url + Log.logDebug "To: " <> fromString url + Log.logDebug "Sending: " <> pJson payload + let encoded = BSL.toStrict $ A.encode payload httpSignature <- makeHttpSignature details uri encoded Log.logDebug $ "http signature: " <> pShow httpSignature