on master help sign in
Update examples
be5deaaMichael Xavier
katip/examples/example.hs+5−22
@@ -45,11 +45,11 @@ main = do
45 -- use this to stack up various contextual details throughout your
45 -- use this to stack up various contextual details throughout your
46 -- code and they will be flattened out and combined in the log
46 -- code and they will be flattened out and combined in the log
47 -- output.
47 -- output.
48− addNamespace "confrabulation" $ addContext (ConfrabLogCTX 42) $ do
48+ katipAddNamespace "confrabulation" $ katipAddContext (ConfrabLogCTX 42) $ do
49 $(logTM) DebugS "Confrabulating widgets, with extra namespace and context"
49 $(logTM) DebugS "Confrabulating widgets, with extra namespace and context"
50 confrabulateWidgets
50 confrabulateWidgets
51 $(logTM) InfoS "Namespace and context are back to normal"
51 $(logTM) InfoS "Namespace and context are back to normal"
52− noLogging $
52+ katipNoLogging $
53 $(logTM) DebugS "You'll never see this log message!"
53 $(logTM) DebugS "You'll never see this log message!"
54 -- This blocking call flushes all in-flight messages through
54 -- This blocking call flushes all in-flight messages through
55 -- handlers and then finalizes each scribe.
55 -- handlers and then finalizes each scribe.
@@ -111,31 +111,14 @@ instance MonadBaseControl b m => MonadBaseControl b (MyStack m) where
111
111
112instance (MonadIO m) => Katip (MyStack m) where
112instance (MonadIO m) => Katip (MyStack m) where
113 getLogEnv = asks msLogEnv
113 getLogEnv = asks msLogEnv
114+ localLogEnv f (MyStack m) = MyStack (local (\s -> s { msLogEnv = f (msLogEnv s)}) m)
114
115
115
116
116instance (MonadIO m) => KatipContext (MyStack m) where
117instance (MonadIO m) => KatipContext (MyStack m) where
117 getKatipContext = asks msKContext
118 getKatipContext = asks msKContext
119+ localKatipContext f (MyStack m) = MyStack (local (\s -> s { msKContext = f (msKContext s)}) m)
118 getKatipNamespace = asks msKNamespace
120 getKatipNamespace = asks msKNamespace
119−
120−
121−-------------------------------------------------------------------------------
122−-- | Merge some context into the log only for the given block
123−addContext :: (LogItem i, MonadReader MyState m) => i -> m a -> m a
124−addContext i = local (\r -> r { msKContext = msKContext r <> ctxs })
125− where
126− ctxs = liftPayload i
127−
128−
129−-------------------------------------------------------------------------------
130−-- | Add a layer of namespace to the logs only for the given block
131−addNamespace :: (MonadReader MyState m) => Namespace -> m a -> m a
132−addNamespace ns = local (\r -> r { msKNamespace = msKNamespace r <> ns })
121+ localKatipNamespace f (MyStack m) = MyStack (local (\s -> s { msKNamespace = f (msKNamespace s)}) m)
133−
134−
135−-------------------------------------------------------------------------------
136−-- | Disable all log output temporarily
137−noLogging :: (MonadReader MyState m) => m a -> m a
138−noLogging = local (\r -> r { msLogEnv = clearScribes (msLogEnv r)})
139
122
140
123
141-------------------------------------------------------------------------------
124-------------------------------------------------------------------------------
katip/examples/example_lens.hs+5−22
@@ -60,11 +60,11 @@ main = do
60 -- use this to stack up various contextual details throughout your
60 -- use this to stack up various contextual details throughout your
61 -- code and they will be flattened out and combined in the log
61 -- code and they will be flattened out and combined in the log
62 -- output.
62 -- output.
63− addNamespace "confrabulation" $ addContext (ConfrabLogCTX 42) $ do
63+ katipAddNamespace "confrabulation" $ katipAddContext (ConfrabLogCTX 42) $ do
64 $(logTM) DebugS "Confrabulating widgets, with extra namespace and context"
64 $(logTM) DebugS "Confrabulating widgets, with extra namespace and context"
65 confrabulateWidgets
65 confrabulateWidgets
66 $(logTM) InfoS "Namespace and context are back to normal"
66 $(logTM) InfoS "Namespace and context are back to normal"
67− noLogging $
67+ katipNoLogging $
68 $(logTM) DebugS "You'll never see this log message!"
68 $(logTM) DebugS "You'll never see this log message!"
69 -- This blocking call flushes all in-flight messages through
69 -- This blocking call flushes all in-flight messages through
70 -- handlers and then finalizes each scribe.
70 -- handlers and then finalizes each scribe.
@@ -118,31 +118,14 @@ instance MonadBaseControl b m => MonadBaseControl b (MyStack m) where
118
118
119instance (MonadIO m) => Katip (MyStack m) where
119instance (MonadIO m) => Katip (MyStack m) where
120 getLogEnv = view msLogEnv
120 getLogEnv = view msLogEnv
121+ localLogEnv f (MyStack m) = MyStack (local (over msLogEnv f) m)
121
122
122
123
123instance (MonadIO m) => KatipContext (MyStack m) where
124instance (MonadIO m) => KatipContext (MyStack m) where
124 getKatipContext = view msKContext
125 getKatipContext = view msKContext
126+ localKatipContext f (MyStack m) = MyStack (local (over msKContext f) m)
125 getKatipNamespace = view msKNamespace
127 getKatipNamespace = view msKNamespace
126−
128+ localKatipNamespace f (MyStack m) = MyStack (local (over msKNamespace f) m)
127−
128−-------------------------------------------------------------------------------
129−-- | Merge some context into the log only for the given block
130−addContext :: (LogItem i, MonadReader r m, HasMyState r) => i -> m a -> m a
131−addContext i = local (\r -> r & msKContext <>~ ctxs)
132− where
133− ctxs = liftPayload i
134−
135−
136−-------------------------------------------------------------------------------
137−-- | Add a layer of namespace to the logs only for the given block
138−addNamespace :: (MonadReader r m, HasMyState r) => Namespace -> m a -> m a
139−addNamespace ns = local (\r -> r & msKNamespace <>~ ns)
140−
141−
142−-------------------------------------------------------------------------------
143−-- | Disable all log output temporarily
144−noLogging :: (MonadReader r m, HasMyState r) => m a -> m a
145−noLogging = local (\r -> r & msLogEnv %~ clearScribes)
146
129
147
130
148-------------------------------------------------------------------------------
131-------------------------------------------------------------------------------