aboutsummaryrefslogtreecommitdiff
path: root/hsm-core/Hsm/Core/Log.hs
blob: 6930e90b7b716d2ea7a85fc7f51b54d160237885 (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
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

-- Helper function allows logging within IO, Useful during `resourcet`
-- allocation and release operations.
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