aboutsummaryrefslogtreecommitdiff
path: root/hsm-log
diff options
context:
space:
mode:
authorPaul Oliver <contact@pauloliver.dev>2025-08-25 02:35:34 +0000
committerPaul Oliver <contact@pauloliver.dev>2025-08-25 02:35:34 +0000
commit6f80d2d579d0be8773829ee277086af087e85862 (patch)
tree93a52fed4d72f6076f800e7aeea1a3ca663c1e06 /hsm-log
parent6db2d77345b1d3da432a73d9eaf0be34165567c3 (diff)
Improves formatting of logs
Diffstat (limited to 'hsm-log')
-rw-r--r--hsm-log/Hsm/Log.hs44
-rw-r--r--hsm-log/hsm-log.cabal1
2 files changed, 32 insertions, 13 deletions
diff --git a/hsm-log/Hsm/Log.hs b/hsm-log/Hsm/Log.hs
index 3c25501..a1ffdb4 100644
--- a/hsm-log/Hsm/Log.hs
+++ b/hsm-log/Hsm/Log.hs
@@ -5,27 +5,29 @@
module Hsm.Log
( Severity(Attention, Info, Trace)
, Log
- , getLoggerIO
+ , makeLoggerIO
, logMsg
+ , logBlock
, runLog
) where
import Control.Monad (when)
import Data.Function (applyWhen)
+import Data.List (intercalate)
import Data.Proxy (Proxy(Proxy))
import Data.Time.Clock (getCurrentTime)
import Data.Time.ISO8601 (formatISO8601Millis)
import Effectful (Dispatch(Static), DispatchOf, Eff, Effect, IOE, (:>))
import Effectful.Dispatch.Static (SideEffects(WithSideEffects), StaticRep, evalStaticRep, getStaticRep, unsafeEff_)
+import GHC.Conc.Sync (fromThreadId, myThreadId)
import GHC.TypeLits (KnownSymbol, Symbol, symbolVal)
-import GHC.TypeLits.Printf (printf)
-import String.ANSI (red)
+import String.ANSI (blue, green, red, white)
data Severity
= Attention
| Info
| Trace
- deriving (Enum, Eq, Ord, Show)
+ deriving (Enum, Eq, Ord)
data Log (d :: Symbol) (a :: * -> *) (b :: *)
@@ -34,22 +36,40 @@ type instance DispatchOf (Log d) = Static WithSideEffects
newtype instance StaticRep (Log d) =
Log Severity
-getLoggerIO ::
+makeLoggerIO ::
forall d es. (KnownSymbol d, Log d :> es)
=> Eff es (Severity -> String -> IO ())
-getLoggerIO = do
- Log level <- getStaticRep
- return $ \severity message ->
- when (severity <= level) $ do
- time <- formatISO8601Millis <$> getCurrentTime
- putStrLn . applyWhen (severity == Attention) red $ printf "%s %s [%s] %s" time (symbolVal $ Proxy @d) (show severity) message
+makeLoggerIO = getStaticRep >>= \(Log level) -> return $ loggerIO level
+ where
+ loggerIO level severity message =
+ when (severity <= level) $ do
+ time <- formatISO8601Millis <$> getCurrentTime
+ threadId <- show . fromThreadId <$> myThreadId
+ putStrLn $ unwords [time, fmtDomainAndThreadId threadId, fmtSeverity, fmtMessage]
+ where
+ fmtDomainAndThreadId threadId = white $ symbolVal (Proxy @d) <> ":" <> threadId
+ fmtSeverity =
+ case severity of
+ Attention -> red "ATTENTION"
+ Info -> green "INFO"
+ Trace -> blue "TRACE"
+ fmtMessage = applyWhen (severity == Attention) red message
logMsg ::
forall d es. (KnownSymbol d, Log d :> es)
=> Severity
-> String
-> Eff es ()
-logMsg severity message = getLoggerIO >>= \loggerIO -> unsafeEff_ $ loggerIO severity message
+logMsg severity message = makeLoggerIO >>= \loggerIO -> unsafeEff_ $ loggerIO severity message
+
+logBlock ::
+ forall d es. (KnownSymbol d, Log d :> es)
+ => Severity
+ -> String
+ -> Eff es ()
+logBlock severity message = logMsg severity $ separator <> intercalate separator (lines message)
+ where
+ separator = "\n ... "
runLog ::
forall d es a. IOE :> es
diff --git a/hsm-log/hsm-log.cabal b/hsm-log/hsm-log.cabal
index 7aab0de..e8c3972 100644
--- a/hsm-log/hsm-log.cabal
+++ b/hsm-log/hsm-log.cabal
@@ -11,7 +11,6 @@ library
, iso8601-time
, text-ansi
, time
- , typelits-printf
default-language: GHC2024
exposed-modules: Hsm.Log