Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions log-effectful.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ common language
LambdaCase
MultiParamTypeClasses
NoStarIsType
OverloadedStrings
RankNTypes
RoleAnnotations
ScopedTypeVariables
Expand All @@ -56,6 +57,7 @@ library
build-depends: base <5
, aeson >=2.0.0.0
, effectful-core >=1.0.0.0 && <3.0.0.0
, lifted-base
, log-base >=0.12.0.0
, text
, time
Expand Down
35 changes: 22 additions & 13 deletions src/Effectful/Log.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ module Effectful.Log
, module Log
) where

import Control.Exception.Lifted
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use Effectful.Exception.

import Data.Aeson.Types
import Data.Text (Text)
import Data.Text (Text, pack)
import Data.Time.Clock
import Effectful.Dispatch.Dynamic
import Effectful.Reader.Static
Expand Down Expand Up @@ -44,18 +45,19 @@ runLog
-> Eff (Log : es) a
-- ^ The computation to run.
-> Eff es a
runLog component logger maxLogLevel = reinterpret reader $ \env -> \case
LogMessageOp level message data_ -> do
time <- liftIO getCurrentTime
logEnv <- ask
liftIO $ logMessageIO logEnv time level message data_
LocalData data_ action -> localSeqUnlift env $ \unlift -> do
(`local` unlift action) $ \logEnv -> logEnv { leData = data_ ++ leData logEnv }
LocalDomain domain action -> localSeqUnlift env $ \unlift -> do
(`local` unlift action) $ \logEnv -> logEnv { leDomain = leDomain logEnv ++ [domain] }
LocalMaxLogLevel level action -> localSeqUnlift env $ \unlift -> do
(`local` unlift action) $ \logEnv -> logEnv { leMaxLogLevel = level }
GetLoggerEnv -> ask
runLog component logger maxLogLevel =
reinterpret reader (\env -> \case
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I think putting the handler in the where clause will be better for readability - right now it's somewhat hard to see the "handle" stuff at the end.

LogMessageOp level message data_ -> do
time <- liftIO getCurrentTime
logEnv <- ask
liftIO $ logMessageIO logEnv time level message data_
LocalData data_ action -> localSeqUnlift env $ \unlift -> do
(`local` unlift action) $ \logEnv -> logEnv { leData = data_ ++ leData logEnv }
LocalDomain domain action -> localSeqUnlift env $ \unlift -> do
(`local` unlift action) $ \logEnv -> logEnv { leDomain = leDomain logEnv ++ [domain] }
LocalMaxLogLevel level action -> localSeqUnlift env $ \unlift -> do
(`local` unlift action) $ \logEnv -> logEnv { leMaxLogLevel = level }
GetLoggerEnv -> ask) . handle logException
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use withException instead of handle (needs effectful-core-2.6.0.0).

where
reader = runReader LoggerEnv
{ leLogger = logger
Expand All @@ -64,6 +66,13 @@ runLog component logger maxLogLevel = reinterpret reader $ \env -> \case
, leData = []
, leMaxLogLevel = maxLogLevel
}
logException :: (IOE :> es, Log :> es) => SomeException -> Eff es a
logException (SomeException e) = do
time <- liftIO getCurrentTime
logEnv <- getLoggerEnv
liftIO $
logMessageIO logEnv time LogAttention "Uncaught exception" $ object ["error" .= (pack . show $ e)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use logAttention here (and you don't need pack, same as in the log-base PR).

throw e
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throwIO


-- | Orphan, canonical instance.
instance Log :> es => MonadLog (Eff es) where
Expand Down