@@ -44,7 +44,8 @@ import Control.Monad.Trans.Either
44import Control.Monad.Trans.Except
44import Control.Monad.Trans.Except
45import Control.Monad.Trans.Maybe
45import Control.Monad.Trans.Maybe
46import Control.Monad.Trans.Reader
46import Control.Monad.Trans.Reader
47−import Control.Monad.Trans.Resource (ResourceT, transResourceT)
47+import Control.Monad.Trans.Resource (MonadResource, ResourceT,
48import Control.Monad.Trans.RWS.Lazy (RWST, mapRWST)
49import Control.Monad.Trans.RWS.Lazy (RWST, mapRWST)
49import qualified Control.Monad.Trans.RWS.Strict as Strict (RWST, mapRWST)
50import qualified Control.Monad.Trans.RWS.Strict as Strict (RWST, mapRWST)
50import Control.Monad.Trans.State.Lazy (StateT, mapStateT)
51import Control.Monad.Trans.State.Lazy (StateT, mapStateT)
@@ -664,7 +665,7 @@ makeLenses ''LogEnv
666-------------------------------------------------------------------------------
667-------------------------------------------------------------------------------
667−-- | Create a reasonable default InitLogEnv. Uses an 'AutoUdate' which
668+-- | Create a reasonable default InitLogEnv. Uses an 'AutoUpdate' which
668-- updates the timer every 1ms. If you need even more timestamp
669-- updates the timer every 1ms. If you need even more timestamp
669-- precision at the cost of performance, consider setting
670-- precision at the cost of performance, consider setting
670-- '_logEnvTimer' with 'getCurrentTime'.
671-- '_logEnvTimer' with 'getCurrentTime'.
@@ -879,7 +880,8 @@ instance (Katip m) => Katip (ResourceT m) where
879-- opposed to implementing 'Katip' for your monad.
880-- opposed to implementing 'Katip' for your monad.
880newtype KatipT m a = KatipT { unKatipT :: ReaderT LogEnv m a }
881newtype KatipT m a = KatipT { unKatipT :: ReaderT LogEnv m a }
881 deriving ( Functor, Applicative, Monad, MonadIO
882 deriving ( Functor, Applicative, Monad, MonadIO
882− , MonadMask, MonadCatch, MonadThrow, MonadTrans, MonadBase b)
883+ , MonadMask, MonadCatch, MonadThrow, MonadResource, MonadTrans
885instance MonadIO m => Katip (KatipT m) where
887instance MonadIO m => Katip (KatipT m) where
@@ -901,9 +903,14 @@ instance (MonadBaseControl b m) => MonadBaseControl b (KatipT m) where
901 restoreM = defaultRestoreM
903 restoreM = defaultRestoreM
903instance MonadUnliftIO m => MonadUnliftIO (KatipT m) where
905instance MonadUnliftIO m => MonadUnliftIO (KatipT m) where
906+#if MIN_VERSION_unliftio_core(0, 2, 0)
907+ withRunInIO inner = KatipT $ ReaderT $ \le -> withRunInIO $ \run ->
908+ inner (run . runKatipT le)
904 askUnliftIO = KatipT $
910 askUnliftIO = KatipT $
905− withUnliftIO $ \u ->
911+ withUnliftIO $ \u ->
906− pure (UnliftIO (unliftIO u . unKatipT))
912+ pure (UnliftIO (unliftIO u . unKatipT))
908#if MIN_VERSION_base(4, 9, 0)
915#if MIN_VERSION_base(4, 9, 0)
909instance MF.MonadFail m => MF.MonadFail (KatipT m) where
916instance MF.MonadFail m => MF.MonadFail (KatipT m) where
@@ -941,19 +948,29 @@ logItem
942logItem a ns loc sev msg = do
949logItem a ns loc sev msg = do
943 LogEnv{..} <- getLogEnv
950 LogEnv{..} <- getLogEnv
944− liftIO $ do
951+ logKatipItem =<< liftIO
946− <$> pure _logEnvApp
952+ (Item <$> pure _logEnvApp
947− <*> pure _logEnvEnv
953+ <*> pure _logEnvEnv
948− <*> pure sev
954+ <*> pure sev
949− <*> (mkThreadIdText <$> myThreadId)
955+ <*> (mkThreadIdText <$> myThreadId)
950− <*> pure _logEnvHost
956+ <*> pure _logEnvHost
951− <*> pure _logEnvPid
957+ <*> pure _logEnvPid
952− <*> pure a
958+ <*> pure a
953− <*> pure msg
959+ <*> pure msg
954− <*> _logEnvTimer
960+ <*> _logEnvTimer
955− <*> pure (_logEnvApp <> ns)
961+ <*> pure (_logEnvApp <> ns)
956− <*> pure loc
962+ <*> pure loc)
964+-- | Log already constructed 'Item'. This is the lowest level function that other log*
966+-- It can be useful when implementing centralised logging services.
968+ :: (A.Applicative m, LogItem a, Katip m)
971+logKatipItem item = do
972+ LogEnv{..} <- getLogEnv
957 FT.forM_ (M.elems _logEnvScribes) $ \ ScribeHandle {..} -> do
974 FT.forM_ (M.elems _logEnvScribes) $ \ ScribeHandle {..} -> do
958 whenM (scribePermitItem shScribe item) $
975 whenM (scribePermitItem shScribe item) $
959 void $ atomically (tryWriteTBQueue shChan (NewItem item))
976 void $ atomically (tryWriteTBQueue shChan (NewItem item))
@@ -1002,7 +1019,7 @@ logException
1002logException a ns sev action = action `catchAny` \e -> f e >> throwM e
1019logException a ns sev action = action `catchAny` \e -> f e >> throwM e
1004 f e = logF a ns sev (msg e)
1021 f e = logF a ns sev (msg e)
1005− msg e = ls (T.pack "An exception has occured: ") <> showLS e
1022+ msg e = ls (T.pack "An exception has occurred: ") <> showLS e
1008-------------------------------------------------------------------------------
1025-------------------------------------------------------------------------------