aboutsummaryrefslogtreecommitdiff
path: root/hsm-core/Hsm/Core/Log.hs
blob: c097e0f990059cf36d93e0fbf1058f8568de4ffa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
module Hsm.Core.Log
  ( withLogIO
  , logTup
  , flushLogger
  ) where

import Data.Aeson.Types (emptyObject)
import Data.Text (Text)
import Data.Time.Clock (getCurrentTime)
import Effectful (Eff, (:>))
import Effectful.Dispatch.Static (unsafeEff_)
import Effectful.Log qualified as L

-- This helper function enables logging within the IO monad, which is
-- particularly useful during resource allocation and release operations with
-- `resourcet`.
withLogIO :: L.Log :> es => Eff es (L.LogLevel -> Text -> IO ())
withLogIO = do
  logIO <- L.getLoggerIO
  return $ \level message -> do
    now <- getCurrentTime
    logIO now level message emptyObject

logTup :: L.Log :> es => (L.LogLevel, Text) -> Eff es ()
logTup (level, message) = L.logMessage level message emptyObject

flushLogger :: L.Log :> es => Eff es ()
flushLogger = L.getLoggerEnv >>= unsafeEff_ . L.waitForLogger . L.leLogger