@@ -10,30 +10,30 @@
11-- | Provides support for treating payloads and namespaces as
11-- | Provides support for treating payloads and namespaces as
12-- composable contexts. The common pattern would be to provide a
12-- composable contexts. The common pattern would be to provide a
13−-- 'ContextualLog' instance for your base monad. When your program
13+-- 'KatipContext' instance for your base monad. When your program
14-- changes to a more detailed context, say to the database, you can
14-- changes to a more detailed context, say to the database, you can
15−-- use 'ContextualLogT' to tack on a typed context and a namespace
15+-- use 'LogT' to tack on a typed context and a namespace
16-- which will be merged into the enclosing monad's context and
16-- which will be merged into the enclosing monad's context and
17-- namespace.
17-- namespace.
18module Katip.Monadic
18module Katip.Monadic
20 -- * Monadic variants of logging functions from "Katip.Core"
20 -- * Monadic variants of logging functions from "Katip.Core"
23 , logItemM
23 , logItemM
25 -- * Machinery for merging typed log payloads/contexts
25 -- * Machinery for merging typed log payloads/contexts
26− , ContextualLog(..)
26+ , KatipContext(..)
27 , AnyLogContext
27 , AnyLogContext
28 , LogContexts
28 , LogContexts
29 , liftPayload
29 , liftPayload
31 -- * Transformer for appending to a log context
31 -- * Transformer for appending to a log context
32− , ContextualLogT'
32+ , LogT'
33− , ContextualLogT
33+ , LogT
34− , runContextualLogT
34+ , runLogT
35− , BlankLogContextT
35+ , BlankLogT
36− , runBlankLogContextT
36+ , runBlankLogT
@@ -71,7 +71,7 @@ import Katip.Core
71-- | A wrapper around a log context that erases type information so
71-- | A wrapper around a log context that erases type information so
72-- that contexts from multiple layers can be combined intelligently.
72-- that contexts from multiple layers can be combined intelligently.
73data AnyLogContext where
73data AnyLogContext where
74− AnyLogContext :: (LogContext a) => a -> AnyLogContext
74+ AnyLogContext :: (LogItem a) => a -> AnyLogContext
77-------------------------------------------------------------------------------
77-------------------------------------------------------------------------------
@@ -85,7 +85,7 @@ instance ToJSON LogContexts where
86instance ToObject LogContexts
86instance ToObject LogContexts
88−instance LogContext LogContexts where
88+instance LogItem LogContexts where
89 payloadKeys verb (LogContexts vs) = mconcat $ map payloadKeys' vs
89 payloadKeys verb (LogContexts vs) = mconcat $ map payloadKeys' vs
91 -- To ensure AllKeys doesn't leak keys from other values when
91 -- To ensure AllKeys doesn't leak keys from other values when
@@ -98,28 +98,28 @@ instance LogContext LogContexts where
99-------------------------------------------------------------------------------
99-------------------------------------------------------------------------------
100-- | Lift a log context into the generic wrapper
100-- | Lift a log context into the generic wrapper
101−liftPayload :: (LogContext a) => a -> LogContexts
101+liftPayload :: (LogItem a) => a -> LogContexts
102liftPayload = LogContexts . (:[]) . AnyLogContext
102liftPayload = LogContexts . (:[]) . AnyLogContext
105-------------------------------------------------------------------------------
105-------------------------------------------------------------------------------
106-- | A monadic context that has an inherant way to get logging
106-- | A monadic context that has an inherant way to get logging
107-- context and namespace. Examples include a web application monad or
107-- context and namespace. Examples include a web application monad or
108−-- database monad. Combine with 'ContextualLogT' to nest.
108+-- database monad. Combine with 'LogT' to nest.
109−class ContextualLog m where
109+class Katip m => KatipContext m where
110− getLogContexts :: m LogContexts
110+ getKatipContext :: m LogContexts
111− getNamespace :: m Namespace
111+ getKatipNamespace :: m Namespace
113--TODO: is this INLINABLE?
113--TODO: is this INLINABLE?
114#define TRANS(T) \
114#define TRANS(T) \
115− instance (ContextualLog m, Monad m) => ContextualLog (T m) where \
115+ instance (KatipContext m, Katip (T m)) => KatipContext (T m) where \
116− getLogContexts = lift getLogContexts; \
116+ getKatipContext = lift getKatipContext; \
117− getNamespace = lift getNamespace
117+ getKatipNamespace = lift getKatipNamespace
119#define TRANS_CTX(CTX, T) \
119#define TRANS_CTX(CTX, T) \
120− instance (CTX, ContextualLog m, Monad m) => ContextualLog (T m) where \
120+ instance (CTX, KatipContext m, Katip (T m)) => KatipContext (T m) where \
121− getLogContexts = lift getLogContexts; \
121+ getKatipContext = lift getKatipContext; \
122− getNamespace = lift getNamespace
122+ getKatipNamespace = lift getKatipNamespace
124TRANS(IdentityT)
124TRANS(IdentityT)
125TRANS(MaybeT)
125TRANS(MaybeT)
@@ -134,21 +134,21 @@ TRANS_CTX(Monoid w, WriterT w)
134TRANS_CTX(Monoid w, Strict.RWST r w s)
134TRANS_CTX(Monoid w, Strict.RWST r w s)
135TRANS_CTX(Monoid w, RWST r w s)
135TRANS_CTX(Monoid w, RWST r w s)
137−deriving instance (Monad m, ContextualLog m) => ContextualLog (KatipT m)
137+deriving instance (Monad m, KatipContext m) => KatipContext (KatipT m)
139-------------------------------------------------------------------------------
139-------------------------------------------------------------------------------
140-- | Log with everything, including a source code location. This is
140-- | Log with everything, including a source code location. This is
141−-- very low level and you typically can use 'logtM' in its
141+-- very low level and you typically can use 'logTM' in its
142-- place. Automaticallysupplies payload and namespace.
142-- place. Automaticallysupplies payload and namespace.
144− :: (Applicative m, ContextualLog m, Katip m)
144+ :: (Applicative m, KatipContext m, Katip m)
145 => Maybe Loc
145 => Maybe Loc
146 -> Severity
146 -> Severity
147 -> LogStr
147 -> LogStr
149logItemM loc sev msg = do
149logItemM loc sev msg = do
150− ctx <- getLogContexts
150+ ctx <- getKatipContext
151− ns <- getNamespace
151+ ns <- getKatipNamespace
152 logItem ctx ns loc sev msg
152 logItem ctx ns loc sev msg
@@ -156,39 +156,39 @@ logItemM loc sev msg = do
156-------------------------------------------------------------------------------
156-------------------------------------------------------------------------------
157-- | Log with full context, but without any code
157-- | Log with full context, but without any code
158-- location. Automatically supplies payload and namespace.
158-- location. Automatically supplies payload and namespace.
160− :: (Applicative m, ContextualLog m, Katip m)
160+ :: (Applicative m, KatipContext m, Katip m)
161 => Severity
161 => Severity
162 -- ^ Severity of the message
162 -- ^ Severity of the message
163 -> LogStr
163 -> LogStr
164 -- ^ The log message
164 -- ^ The log message
166−logfM sev msg = do
166+logFM sev msg = do
167− ctx <- getLogContexts
167+ ctx <- getKatipContext
168− ns <- getNamespace
168+ ns <- getKatipNamespace
169− logf ctx ns sev msg
169+ logF ctx ns sev msg
172-------------------------------------------------------------------------------
172-------------------------------------------------------------------------------
173-- | 'Loc'-tagged logging when using template-haskell is OK. Automatically supplies payload and namespace.
173-- | 'Loc'-tagged logging when using template-haskell is OK. Automatically supplies payload and namespace.
175-- @$(logt) InfoS "Hello world"@
175-- @$(logt) InfoS "Hello world"@
176−logtM :: ExpQ
176+logTM :: ExpQ
177−logtM = [| logItemM (Just $(getLoc)) |]
177+logTM = [| logItemM (Just $(getLoc)) |]
180-------------------------------------------------------------------------------
180-------------------------------------------------------------------------------
181-- | Wrapper over 'ReaderT' that provides a passthrough instance for
181-- | Wrapper over 'ReaderT' that provides a passthrough instance for
182-- 'MonadReader' so your stack will not be affected. Also provides a
182-- 'MonadReader' so your stack will not be affected. Also provides a
183−-- 'ContextualLog' instance which combines with the inner monad's
183+-- 'KatipContext' instance which combines with the inner monad's
184-- instance.
184-- instance.
187−-- instance ContextualLog m WebMonad where
187+-- instance KatipContext m WebMonad where
188−-- getLogContexts = ...
188+-- getKatipContext = ...
189−-- getNamespace = Namespace ["web"]
189+-- getKatipNamespace = Namespace ["web"]
191−-- dbCallInWeb = runContextualLogT getDBContext (Namespace ["db"]) $ do
191+-- dbCallInWeb = runLogT getDBContext (Namespace ["db"]) $ do
192-- someStuff
192-- someStuff
@@ -196,10 +196,10 @@ logtM = [| logItemM (Just $(getLoc)) |]
196-- the (optionally) monadic log context for the db, combine it with
196-- the (optionally) monadic log context for the db, combine it with
197-- the web server's and will use the namespace @Namespace ["web","db"]@.
197-- the web server's and will use the namespace @Namespace ["web","db"]@.
199−-- 'ContextualLogT' is exported which fixes @n ~ m@. The two type
199+-- 'LogT' is exported which fixes @n ~ m@. The two type
200-- variables must be present for defining a 'MonadTransControl' instance.
200-- variables must be present for defining a 'MonadTransControl' instance.
201−newtype ContextualLogT' n m a = ContextualLogT {
201+newtype LogT' n m a = LogT {
202− unContextualLogT :: ReaderT (n LogContexts, n Namespace) m a
202+ unLogT :: ReaderT (n LogContexts, n Namespace) m a
203 } deriving ( Functor
203 } deriving ( Functor
204 , Applicative
204 , Applicative
@@ -216,53 +216,53 @@ newtype ContextualLogT' n m a = ContextualLogT {
219−type ContextualLogT m a = ContextualLogT' m m a
219+type LogT m a = LogT' m m a
221−instance MonadTrans (ContextualLogT' n) where
221+instance MonadTrans (LogT' n) where
222− lift = ContextualLogT . lift
222+ lift = LogT . lift
224-- Reader is a passthrough. We don't expose our internal reader so as not to conflict
224-- Reader is a passthrough. We don't expose our internal reader so as not to conflict
225−instance (MonadReader r m) => MonadReader r (ContextualLogT' n m) where
225+instance (MonadReader r m) => MonadReader r (LogT' n m) where
226 ask = lift ask
226 ask = lift ask
227− local f (ContextualLogT (ReaderT m)) = ContextualLogT $ ReaderT $ \r ->
227+ local f (LogT (ReaderT m)) = LogT $ ReaderT $ \r ->
228 local f (m r)
228 local f (m r)
230−instance MonadTransControl (ContextualLogT' n) where
230+instance MonadTransControl (LogT' n) where
231− type StT (ContextualLogT' n) a = StT (ReaderT (n LogContexts)) a
231+ type StT (LogT' n) a = StT (ReaderT (n LogContexts)) a
232− liftWith = defaultLiftWith ContextualLogT unContextualLogT
232+ liftWith = defaultLiftWith LogT unLogT
233− restoreT = defaultRestoreT ContextualLogT
233+ restoreT = defaultRestoreT LogT
234 {-# INLINE liftWith #-}
234 {-# INLINE liftWith #-}
235 {-# INLINE restoreT #-}
235 {-# INLINE restoreT #-}
237−instance (MonadBaseControl b m) => MonadBaseControl b (ContextualLogT' n m) where
237+instance (MonadBaseControl b m) => MonadBaseControl b (LogT' n m) where
238− type StM ((ContextualLogT' n) m) a = ComposeSt (ContextualLogT' n) m a
238+ type StM ((LogT' n) m) a = ComposeSt (LogT' n) m a
239 liftBaseWith = defaultLiftBaseWith
239 liftBaseWith = defaultLiftBaseWith
240 restoreM = defaultRestoreM
240 restoreM = defaultRestoreM
242−instance (ContextualLog m, Monad m) => ContextualLog (ContextualLogT' m m) where
242+instance (KatipContext m, Monad m) => KatipContext (LogT' m m) where
243− getLogContexts = ContextualLogT $ ReaderT $ \(gctxs, _) -> do
243+ getKatipContext = LogT $ ReaderT $ \(gctxs, _) -> do
244 ctxs <- gctxs
244 ctxs <- gctxs
245− ctxs' <- getLogContexts
245+ ctxs' <- getKatipContext
246 return $ ctxs <> ctxs'
246 return $ ctxs <> ctxs'
247− getNamespace = ContextualLogT $ ReaderT $ \(_, getNsInner) -> do
247+ getKatipNamespace = LogT $ ReaderT $ \(_, getNsInner) -> do
248− nsOuter <- getNamespace
248+ nsOuter <- getKatipNamespace
249 nsInner <- getNsInner
249 nsInner <- getNsInner
250 return $ nsOuter <> nsInner
250 return $ nsOuter <> nsInner
252−runContextualLogT
252+runLogT
253− :: (Functor m, LogContext a)
253+ :: (Functor m, LogItem a)
255 -> m Namespace
255 -> m Namespace
256− -> ContextualLogT m b
256+ -> LogT m b
258−runContextualLogT lgtr nsgtr m = runReaderT (unContextualLogT m) (liftPayload <$> lgtr, nsgtr)
258+runLogT lgtr nsgtr m = runReaderT (unLogT m) (liftPayload <$> lgtr, nsgtr)
261-------------------------------------------------------------------------------
261-------------------------------------------------------------------------------
262-- | Type analogous to IdentityT that provides an empty log
262-- | Type analogous to IdentityT that provides an empty log
263-- context. Use this if your root monad doesn't have a LogContext instance.
263-- context. Use this if your root monad doesn't have a LogContext instance.
264−newtype BlankLogContextT m a = BlankLogContextT {
264+newtype BlankLogT m a = BlankLogT {
265− runBlankLogContextT :: m a
265+ runBlankLogT :: m a
266 } deriving ( Functor
266 } deriving ( Functor
267 , Applicative
267 , Applicative
@@ -280,21 +280,21 @@ newtype BlankLogContextT m a = BlankLogContextT {
283−instance Monad m => ContextualLog (BlankLogContextT m) where
283+instance Katip m => KatipContext (BlankLogT m) where
284− getLogContexts = return mempty
284+ getKatipContext = return mempty
285− getNamespace = return mempty
285+ getKatipNamespace = return mempty
287−instance MonadTrans BlankLogContextT where
287+instance MonadTrans BlankLogT where
288− lift = BlankLogContextT
288+ lift = BlankLogT
290−instance MonadTransControl BlankLogContextT where
290+instance MonadTransControl BlankLogT where
291− type StT BlankLogContextT a = a
291+ type StT BlankLogT a = a
292− liftWith f = BlankLogContextT $ f runBlankLogContextT
292+ liftWith f = BlankLogT $ f runBlankLogT
293− restoreT = BlankLogContextT
293+ restoreT = BlankLogT
294 {-# INLINABLE liftWith #-}
294 {-# INLINABLE liftWith #-}
295 {-# INLINABLE restoreT #-}
295 {-# INLINABLE restoreT #-}
297−instance (MonadBaseControl b m) => MonadBaseControl b (BlankLogContextT m) where
297+instance (MonadBaseControl b m) => MonadBaseControl b (BlankLogT m) where
298− type StM (BlankLogContextT m) a = ComposeSt BlankLogContextT m a
298+ type StM (BlankLogT m) a = ComposeSt BlankLogT m a
299 liftBaseWith = defaultLiftBaseWith
299 liftBaseWith = defaultLiftBaseWith
300 restoreM = defaultRestoreM
300 restoreM = defaultRestoreM