-
Couldn't load subscription status.
- Fork 4
Log unhandled exceptions #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,8 +12,9 @@ module Effectful.Log | |
| , module Log | ||
| ) where | ||
|
|
||
| import Control.Exception.Lifted | ||
| 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 | ||
|
|
@@ -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 | ||
|
||
| 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 | ||
|
||
| where | ||
| reader = runReader LoggerEnv | ||
| { leLogger = logger | ||
|
|
@@ -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)] | ||
|
||
| throw e | ||
|
||
|
|
||
| -- | Orphan, canonical instance. | ||
| instance Log :> es => MonadLog (Eff es) where | ||
|
|
||
There was a problem hiding this comment.
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.