on master help sign in
Renames
commit45b9512ac706b0ec916183fe49975af38f214d52 authorMichael Xavier <michael@michaelxavier.net> dateFri 13 Mar 2015, 12:17 (12 years ago) parent015b82b
6 files changed+120−120
katip/bench/Main.hs+1−1 katip/examples/Main.hs+12−12 katip/src/Katip.hs+11−11 katip/src/Katip/Core.hs+17−17 katip/src/Katip/Monadic.hs+77−77 katip/src/Katip/Scribes/Handle.hs+2−2
katip/bench/Main.hs+1−1
@@ -60,7 +60,7 @@ data ExPayload = ExPayload
6060instance ToJSON ExPayload where
6161 toJSON _ = Object mempty
6262
63−instance LogContext ExPayload where
63+instance LogItem ExPayload where
6464 payloadKeys _ _ = AllKeys
6565
6666
katip/examples/Main.hs+12−12
@@ -21,9 +21,9 @@ import Katip.Scribes.Handle
2121
2222main :: IO ()
2323main = runKatipT _ioLogEnv $ do
24− logm "example" InfoS "Easy to emit from IO directly!"
25− logf myContext "example" InfoS "Here's a more stateful item."
26− $(logt) myContext "example" InfoS "Here's one with code location."
24+ logMsg "example" InfoS "Easy to emit from IO directly!"
25+ logF myContext "example" InfoS "Here's a more stateful item."
26+ $(logT) myContext "example" InfoS "Here's one with code location."
2727 runHandler handler
2828
2929
@@ -41,7 +41,7 @@ instance ToJSON MyContext where
4141
4242instance ToObject MyContext
4343
44−instance LogContext MyContext where
44+instance LogItem MyContext where
4545 payloadKeys _ _ = AllKeys
4646
4747-------------------------------------------------------------------------------
@@ -59,13 +59,13 @@ withDB f = f $ ConnInfo "1234" "exampledb" ()
5959
6060handler :: Handler ()
6161handler = do
62− $(logtM) InfoS "got request"
62+ $(logTM) InfoS "got request"
6363 withDB $ \ci -> do
6464 let dbctx = DBLog (ciConnId ci)
6565 let ns = Namespace ["db", ciConnDB ci]
66− runContextualLogT (return dbctx) (return ns) $ do
66+ runLogT (return dbctx) (return ns) $ do
6767 liftIO $ putStrLn "DB things!"
68− $(logtM) InfoS "calling db"
68+ $(logTM) InfoS "calling db"
6969
7070-------------------------------------------------------------------------------
7171data Request = Request {
@@ -82,7 +82,7 @@ instance ToJSON Request where
8282
8383instance ToObject Request
8484
85−instance LogContext Request where
85+instance LogItem Request where
8686 payloadKeys V3 _ = AllKeys
8787 payloadKeys _ _ = SomeKeys ["method", "path"]
8888
@@ -97,9 +97,9 @@ newtype WebT m a = WebT {
9797 , Katip
9898 )
9999
100−instance Monad m => ContextualLog (WebT m) where
101− getLogContexts = liftM liftPayload ask
102− getNamespace = return $ Namespace ["myApp"]
100+instance Katip m => KatipContext (WebT m) where
101+ getKatipContext = liftM liftPayload ask
102+ getKatipNamespace = return $ Namespace ["myApp"]
103103
104104
105105-------------------------------------------------------------------------------
@@ -118,5 +118,5 @@ instance ToJSON DBLog where
118118
119119instance ToObject DBLog
120120
121−instance LogContext DBLog where
121+instance LogItem DBLog where
122122 payloadKeys _ _ = AllKeys
katip/src/Katip.hs+11−11
@@ -9,11 +9,11 @@ module Katip
99 , Severity (..), renderSeverity
1010 , Verbosity (..)
1111 , ToObject (..)
12− , LogContext (..)
12+ , LogItem (..)
1313 , PayloadSelection (..)
1414 , Scribe (..)
1515 , LogEnv (..)
16− , ContextualLog(..)
16+ , KatipContext(..)
1717 , AnyLogContext
1818 , LogContexts, liftPayload
1919
@@ -30,19 +30,19 @@ module Katip
3030 , LogStr (..)
3131 , logStr, ls, showLS
3232
33− , logf
34− , logm
35− , logt
33+ , logF
34+ , logMsg
35+ , logT
3636 , logItem
37− , logfM
38− , logtM
37+ , logFM
38+ , logTM
3939 , logItemM
4040
4141 -- * Transformer for appending to a log context
42− , ContextualLogT
43− , runContextualLogT
44− , BlankLogContextT
45− , runBlankLogContextT
42+ , LogT
43+ , runLogT
44+ , BlankLogT
45+ , runBlankLogT
4646
4747 -- * Included Scribes
4848 , mkHandleScribe
katip/src/Katip/Core.hs+17−17
@@ -129,7 +129,7 @@ logStr t = LogStr (B.fromText $ toS t)
129129
130130
131131-------------------------------------------------------------------------------
132−-- | Shorthand for 'logm'
132+-- | Shorthand for 'logMsg'
133133ls :: StringConv a Text => a -> LogStr
134134ls = logStr
135135
@@ -224,19 +224,19 @@ instance ToObject A.Object
224224-- keys for higher levels of verbosity. Each level of verbosity
225225-- automatically and recursively contains all keys from the level
226226-- before it.
227−class ToObject a => LogContext a where
227+class ToObject a => LogItem a where
228228
229229 -- | List of keys in the JSON object that should be included in message.
230230 payloadKeys :: Verbosity -> a -> PayloadSelection
231231
232232
233−instance LogContext () where payloadKeys _ _ = SomeKeys []
233+instance LogItem () where payloadKeys _ _ = SomeKeys []
234234
235235
236236-------------------------------------------------------------------------------
237237-- | Constrain payload based on verbosity. Backends should use this to
238238-- automatically bubble higher verbosity levels to lower ones.
239−payloadObject :: LogContext a => Verbosity -> a -> A.Object
239+payloadObject :: LogItem a => Verbosity -> a -> A.Object
240240payloadObject verb a = case foldMap (flip payloadKeys a) [(V0)..verb] of
241241 AllKeys -> toObject a
242242 SomeKeys ks -> HM.filterWithKey (\ k _ -> k `elem` ks) $ toObject a
@@ -246,7 +246,7 @@ payloadObject verb a = case foldMap (flip payloadKeys a) [(V0)..verb] of
246246-- | Convert log item to its JSON representation while trimming its
247247-- payload based on the desired verbosity. Backends that push JSON
248248-- messages should use this to obtain their payload.
249−itemJson :: LogContext a => Verbosity -> Item a -> A.Value
249+itemJson :: LogItem a => Verbosity -> Item a -> A.Value
250250itemJson verb a = toJSON $ a & itemPayload %~ payloadObject verb
251251
252252
@@ -254,7 +254,7 @@ itemJson verb a = toJSON $ a & itemPayload %~ payloadObject verb
254254-- | Scribes are handlers of incoming items. Each registered scribe
255255-- knows how to push a log item somewhere.
256256data Scribe = Scribe {
257− lhPush :: forall a. LogContext a => Item a -> IO ()
257+ lhPush :: forall a. LogItem a => Item a -> IO ()
258258 }
259259
260260
@@ -357,9 +357,9 @@ runKatipT le (KatipT f) = runReaderT f le
357357
358358-------------------------------------------------------------------------------
359359-- | Log with everything, including a source code location. This is
360−-- very low level and you typically can use 'logt' in its place.
360+-- very low level and you typically can use 'logT' in its place.
361361logItem
362− :: (Applicative m, LogContext a, Katip m)
362+ :: (Applicative m, LogItem a, Katip m)
363363 => a
364364 -> Namespace
365365 -> Maybe Loc
@@ -385,8 +385,8 @@ logItem a ns loc sev msg = do
385385
386386-------------------------------------------------------------------------------
387387-- | Log with full context, but without any code location.
388−logf
389− :: (Applicative m, LogContext a, Katip m)
388+logF
389+ :: (Applicative m, LogItem a, Katip m)
390390 => a
391391 -- ^ Contextual payload for the log
392392 -> Namespace
@@ -396,18 +396,18 @@ logf
396396 -> LogStr
397397 -- ^ The log message
398398 -> m ()
399−logf a ns sev msg = logItem a ns Nothing sev msg
399+logF a ns sev msg = logItem a ns Nothing sev msg
400400
401401
402402-------------------------------------------------------------------------------
403403-- | Log a message without any payload/context or code location.
404−logm
404+logMsg
405405 :: (Applicative m, Katip m)
406406 => Namespace
407407 -> Severity
408408 -> LogStr
409409 -> m ()
410−logm ns sev msg = logf () ns sev msg
410+logMsg ns sev msg = logF () ns sev msg
411411
412412
413413instance TH.Lift Namespace where
@@ -447,7 +447,7 @@ liftLoc (Loc a b c (d1, d2) (e1, e2)) = [|Loc
447447
448448-------------------------------------------------------------------------------
449449-- | For use when you want to include location in your logs. This will
450−-- fill the 'Maybe Loc' gap in 'logf' of this module.
450+-- fill the 'Maybe Loc' gap in 'logF' of this module.
451451getLoc :: Q Exp
452452getLoc = [| $(location >>= liftLoc) |]
453453
@@ -455,9 +455,9 @@ getLoc = [| $(location >>= liftLoc) |]
455455-------------------------------------------------------------------------------
456456-- | 'Loc'-tagged logging when using template-haskell is OK.
457457--
458−-- @$(logt) obj mempty InfoS "Hello world"@
459−logt :: ExpQ
460−logt = [| \ a ns sev msg -> logItem a ns (Just $(getLoc)) sev msg |]
458+-- @$(logT) obj mempty InfoS "Hello world"@
459+logT :: ExpQ
460+logT = [| \ a ns sev msg -> logItem a ns (Just $(getLoc)) sev msg |]
461461
462462
463463-- taken from the file-location package
katip/src/Katip/Monadic.hs+77−77
@@ -10,30 +10,30 @@
1010
1111-- | Provides support for treating payloads and namespaces as
1212-- 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
1414-- 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
1616-- which will be merged into the enclosing monad's context and
1717-- namespace.
1818module Katip.Monadic
1919 (
2020 -- * Monadic variants of logging functions from "Katip.Core"
21− logfM
22− , logtM
21+ logFM
22+ , logTM
2323 , logItemM
2424
2525 -- * Machinery for merging typed log payloads/contexts
26− , ContextualLog(..)
26+ , KatipContext(..)
2727 , AnyLogContext
2828 , LogContexts
2929 , liftPayload
3030
3131 -- * Transformer for appending to a log context
32− , ContextualLogT'
33− , ContextualLogT
34− , runContextualLogT
35− , BlankLogContextT
36− , runBlankLogContextT
32+ , LogT'
33+ , LogT
34+ , runLogT
35+ , BlankLogT
36+ , runBlankLogT
3737 ) where
3838
3939
@@ -71,7 +71,7 @@ import Katip.Core
7171-- | A wrapper around a log context that erases type information so
7272-- that contexts from multiple layers can be combined intelligently.
7373data AnyLogContext where
74− AnyLogContext :: (LogContext a) => a -> AnyLogContext
74+ AnyLogContext :: (LogItem a) => a -> AnyLogContext
7575
7676
7777-------------------------------------------------------------------------------
@@ -85,7 +85,7 @@ instance ToJSON LogContexts where
8585
8686instance ToObject LogContexts
8787
88−instance LogContext LogContexts where
88+instance LogItem LogContexts where
8989 payloadKeys verb (LogContexts vs) = mconcat $ map payloadKeys' vs
9090 where
9191 -- To ensure AllKeys doesn't leak keys from other values when
@@ -98,28 +98,28 @@ instance LogContext LogContexts where
9898
9999-------------------------------------------------------------------------------
100100-- | Lift a log context into the generic wrapper
101−liftPayload :: (LogContext a) => a -> LogContexts
101+liftPayload :: (LogItem a) => a -> LogContexts
102102liftPayload = LogContexts . (:[]) . AnyLogContext
103103
104104
105105-------------------------------------------------------------------------------
106106-- | A monadic context that has an inherant way to get logging
107107-- context and namespace. Examples include a web application monad or
108−-- database monad. Combine with 'ContextualLogT' to nest.
109−class ContextualLog m where
110− getLogContexts :: m LogContexts
111− getNamespace :: m Namespace
108+-- database monad. Combine with 'LogT' to nest.
109+class Katip m => KatipContext m where
110+ getKatipContext :: m LogContexts
111+ getKatipNamespace :: m Namespace
112112
113113--TODO: is this INLINABLE?
114114#define TRANS(T) \
115− instance (ContextualLog m, Monad m) => ContextualLog (T m) where \
116− getLogContexts = lift getLogContexts; \
117− getNamespace = lift getNamespace
115+ instance (KatipContext m, Katip (T m)) => KatipContext (T m) where \
116+ getKatipContext = lift getKatipContext; \
117+ getKatipNamespace = lift getKatipNamespace
118118
119119#define TRANS_CTX(CTX, T) \
120− instance (CTX, ContextualLog m, Monad m) => ContextualLog (T m) where \
121− getLogContexts = lift getLogContexts; \
122− getNamespace = lift getNamespace
120+ instance (CTX, KatipContext m, Katip (T m)) => KatipContext (T m) where \
121+ getKatipContext = lift getKatipContext; \
122+ getKatipNamespace = lift getKatipNamespace
123123
124124TRANS(IdentityT)
125125TRANS(MaybeT)
@@ -134,21 +134,21 @@ TRANS_CTX(Monoid w, WriterT w)
134134TRANS_CTX(Monoid w, Strict.RWST r w s)
135135TRANS_CTX(Monoid w, RWST r w s)
136136
137−deriving instance (Monad m, ContextualLog m) => ContextualLog (KatipT m)
137+deriving instance (Monad m, KatipContext m) => KatipContext (KatipT m)
138138
139139-------------------------------------------------------------------------------
140140-- | 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
142142-- place. Automaticallysupplies payload and namespace.
143143logItemM
144− :: (Applicative m, ContextualLog m, Katip m)
144+ :: (Applicative m, KatipContext m, Katip m)
145145 => Maybe Loc
146146 -> Severity
147147 -> LogStr
148148 -> m ()
149149logItemM loc sev msg = do
150− ctx <- getLogContexts
151− ns <- getNamespace
150+ ctx <- getKatipContext
151+ ns <- getKatipNamespace
152152 logItem ctx ns loc sev msg
153153
154154
@@ -156,39 +156,39 @@ logItemM loc sev msg = do
156156-------------------------------------------------------------------------------
157157-- | Log with full context, but without any code
158158-- location. Automatically supplies payload and namespace.
159−logfM
160− :: (Applicative m, ContextualLog m, Katip m)
159+logFM
160+ :: (Applicative m, KatipContext m, Katip m)
161161 => Severity
162162 -- ^ Severity of the message
163163 -> LogStr
164164 -- ^ The log message
165165 -> m ()
166−logfM sev msg = do
167− ctx <- getLogContexts
168− ns <- getNamespace
169− logf ctx ns sev msg
166+logFM sev msg = do
167+ ctx <- getKatipContext
168+ ns <- getKatipNamespace
169+ logF ctx ns sev msg
170170
171171
172172-------------------------------------------------------------------------------
173173-- | 'Loc'-tagged logging when using template-haskell is OK. Automatically supplies payload and namespace.
174174--
175175-- @$(logt) InfoS "Hello world"@
176−logtM :: ExpQ
177−logtM = [| logItemM (Just $(getLoc)) |]
176+logTM :: ExpQ
177+logTM = [| logItemM (Just $(getLoc)) |]
178178
179179
180180-------------------------------------------------------------------------------
181181-- | Wrapper over 'ReaderT' that provides a passthrough instance for
182182-- '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
184184-- instance.
185185--
186186-- @
187−-- instance ContextualLog m WebMonad where
188−-- getLogContexts = ...
189−-- getNamespace = Namespace ["web"]
187+-- instance KatipContext m WebMonad where
188+-- getKatipContext = ...
189+-- getKatipNamespace = Namespace ["web"]
190190--
191−-- dbCallInWeb = runContextualLogT getDBContext (Namespace ["db"]) $ do
191+-- dbCallInWeb = runLogT getDBContext (Namespace ["db"]) $ do
192192-- someStuff
193193-- @
194194--
@@ -196,10 +196,10 @@ logtM = [| logItemM (Just $(getLoc)) |]
196196-- the (optionally) monadic log context for the db, combine it with
197197-- the web server's and will use the namespace @Namespace ["web","db"]@.
198198--
199−-- 'ContextualLogT' is exported which fixes @n ~ m@. The two type
199+-- 'LogT' is exported which fixes @n ~ m@. The two type
200200-- variables must be present for defining a 'MonadTransControl' instance.
201−newtype ContextualLogT' n m a = ContextualLogT {
202− unContextualLogT :: ReaderT (n LogContexts, n Namespace) m a
201+newtype LogT' n m a = LogT {
202+ unLogT :: ReaderT (n LogContexts, n Namespace) m a
203203 } deriving ( Functor
204204 , Applicative
205205 , Monad
@@ -216,53 +216,53 @@ newtype ContextualLogT' n m a = ContextualLogT {
216216 , Katip
217217 )
218218
219−type ContextualLogT m a = ContextualLogT' m m a
219+type LogT m a = LogT' m m a
220220
221−instance MonadTrans (ContextualLogT' n) where
222− lift = ContextualLogT . lift
221+instance MonadTrans (LogT' n) where
222+ lift = LogT . lift
223223
224224-- 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
226226 ask = lift ask
227− local f (ContextualLogT (ReaderT m)) = ContextualLogT $ ReaderT $ \r ->
227+ local f (LogT (ReaderT m)) = LogT $ ReaderT $ \r ->
228228 local f (m r)
229229
230−instance MonadTransControl (ContextualLogT' n) where
231− type StT (ContextualLogT' n) a = StT (ReaderT (n LogContexts)) a
232− liftWith = defaultLiftWith ContextualLogT unContextualLogT
233− restoreT = defaultRestoreT ContextualLogT
230+instance MonadTransControl (LogT' n) where
231+ type StT (LogT' n) a = StT (ReaderT (n LogContexts)) a
232+ liftWith = defaultLiftWith LogT unLogT
233+ restoreT = defaultRestoreT LogT
234234 {-# INLINE liftWith #-}
235235 {-# INLINE restoreT #-}
236236
237−instance (MonadBaseControl b m) => MonadBaseControl b (ContextualLogT' n m) where
238− type StM ((ContextualLogT' n) m) a = ComposeSt (ContextualLogT' n) m a
237+instance (MonadBaseControl b m) => MonadBaseControl b (LogT' n m) where
238+ type StM ((LogT' n) m) a = ComposeSt (LogT' n) m a
239239 liftBaseWith = defaultLiftBaseWith
240240 restoreM = defaultRestoreM
241241
242−instance (ContextualLog m, Monad m) => ContextualLog (ContextualLogT' m m) where
243− getLogContexts = ContextualLogT $ ReaderT $ \(gctxs, _) -> do
242+instance (KatipContext m, Monad m) => KatipContext (LogT' m m) where
243+ getKatipContext = LogT $ ReaderT $ \(gctxs, _) -> do
244244 ctxs <- gctxs
245− ctxs' <- getLogContexts
245+ ctxs' <- getKatipContext
246246 return $ ctxs <> ctxs'
247− getNamespace = ContextualLogT $ ReaderT $ \(_, getNsInner) -> do
248− nsOuter <- getNamespace
247+ getKatipNamespace = LogT $ ReaderT $ \(_, getNsInner) -> do
248+ nsOuter <- getKatipNamespace
249249 nsInner <- getNsInner
250250 return $ nsOuter <> nsInner
251251
252−runContextualLogT
253− :: (Functor m, LogContext a)
252+runLogT
253+ :: (Functor m, LogItem a)
254254 => m a
255255 -> m Namespace
256− -> ContextualLogT m b
256+ -> LogT m b
257257 -> m b
258−runContextualLogT lgtr nsgtr m = runReaderT (unContextualLogT m) (liftPayload <$> lgtr, nsgtr)
258+runLogT lgtr nsgtr m = runReaderT (unLogT m) (liftPayload <$> lgtr, nsgtr)
259259
260260
261261-------------------------------------------------------------------------------
262262-- | Type analogous to IdentityT that provides an empty log
263263-- context. Use this if your root monad doesn't have a LogContext instance.
264−newtype BlankLogContextT m a = BlankLogContextT {
265− runBlankLogContextT :: m a
264+newtype BlankLogT m a = BlankLogT {
265+ runBlankLogT :: m a
266266 } deriving ( Functor
267267 , Applicative
268268 , Monad
@@ -280,21 +280,21 @@ newtype BlankLogContextT m a = BlankLogContextT {
280280 , Katip
281281 )
282282
283−instance Monad m => ContextualLog (BlankLogContextT m) where
284− getLogContexts = return mempty
285− getNamespace = return mempty
283+instance Katip m => KatipContext (BlankLogT m) where
284+ getKatipContext = return mempty
285+ getKatipNamespace = return mempty
286286
287−instance MonadTrans BlankLogContextT where
288− lift = BlankLogContextT
287+instance MonadTrans BlankLogT where
288+ lift = BlankLogT
289289
290−instance MonadTransControl BlankLogContextT where
291− type StT BlankLogContextT a = a
292− liftWith f = BlankLogContextT $ f runBlankLogContextT
293− restoreT = BlankLogContextT
290+instance MonadTransControl BlankLogT where
291+ type StT BlankLogT a = a
292+ liftWith f = BlankLogT $ f runBlankLogT
293+ restoreT = BlankLogT
294294 {-# INLINABLE liftWith #-}
295295 {-# INLINABLE restoreT #-}
296296
297−instance (MonadBaseControl b m) => MonadBaseControl b (BlankLogContextT m) where
298− type StM (BlankLogContextT m) a = ComposeSt BlankLogContextT m a
297+instance (MonadBaseControl b m) => MonadBaseControl b (BlankLogT m) where
298+ type StM (BlankLogT m) a = ComposeSt BlankLogT m a
299299 liftBaseWith = defaultLiftBaseWith
300300 restoreM = defaultRestoreM
katip/src/Katip/Scribes/Handle.hs+2−2
@@ -25,7 +25,7 @@ brackets m = fromText "[" <> m <> fromText "]"
2525
2626
2727-------------------------------------------------------------------------------
28−getKeys :: LogContext s => Verbosity -> s -> [Builder]
28+getKeys :: LogItem s => Verbosity -> s -> [Builder]
2929getKeys verb a = payloadObject verb a ^..
3030 to HM.toList . traverse . to rendPair
3131 where
@@ -61,7 +61,7 @@ mkHandleScribe cs h sev verb = do
6161
6262
6363-------------------------------------------------------------------------------
64−formatItem :: LogContext a => Bool -> Verbosity -> Item a -> Builder
64+formatItem :: LogItem a => Bool -> Verbosity -> Item a -> Builder
6565formatItem withColor verb Item{..} =
6666 brackets nowStr <>
6767 brackets (mconcat $ map fromText $ intercalateNs _itemNamespace) <>