@@ -13,6 +13,7 @@ module Main
1313
1414-------------------------------------------------------------------------------
1515import qualified Control.Applicative as A
16+import Control.Exception
1617import Control.Monad.Base
1718import Control.Monad.Reader
1819import Control.Monad.Trans.Control
@@ -32,28 +33,29 @@ main = do
3233 -- fields permitted for Verbosity 2 and will throw out Debug
3334 -- messages entirely. Note that katip provides facilities like
3435 -- 'unregisterScribe' and 'registerScribe' to make it possible to
35− -- hot-swap scribes at runtime if you need to.
36+ -- hot-swap scribes at runtime if you need to. 'closeScribes' is
37+ -- blocking and flushes all messages out of a scribe and cleans up
38+ -- resources that were allocated at creation.
3639 handleScribe <- mkHandleScribe ColorIfTerminal stdout InfoS V2
37− le <- registerScribe "stdout" handleScribe defaultScribeSettings =<< initLogEnv "MyApp" "production"
38− let s = MyState M.mempty mempty le
39− runStack s $ do
40− $(logTM) InfoS "Started"
41− -- this will add "confrabulation" to the current namespace, making
42− -- logs made under this block have the namespace of
43− -- "main.confrabulation". Further, ConfrabLogCTX's key/value
44− -- context will also get merged with the context above it. You can
45− -- use this to stack up various contextual details throughout your
46− -- code and they will be flattened out and combined in the log
47− -- output.
48− katipAddNamespace "confrabulation" $ katipAddContext (ConfrabLogCTX 42) $ do
49− $(logTM) DebugS "Confrabulating widgets, with extra namespace and context"
50− confrabulateWidgets
51− $(logTM) InfoS "Namespace and context are back to normal"
52− katipNoLogging $
53− $(logTM) DebugS "You'll never see this log message!"
54− -- This blocking call flushes all in-flight messages through
55− -- handlers and then finalizes each scribe.
56− void (closeScribes le)
40+ let mkLogEnv = registerScribe "stdout" handleScribe defaultScribeSettings =<< initLogEnv "MyApp" "production"
41+ bracket mkLogEnv closeScribes $ \le -> do
42+ let s = MyState M.mempty mempty le
43+ runStack s $ do
44+ $(logTM) InfoS "Started"
45+ -- this will add "confrabulation" to the current namespace, making
46+ -- logs made under this block have the namespace of
47+ -- "main.confrabulation". Further, ConfrabLogCTX's key/value
48+ -- context will also get merged with the context above it. You can
49+ -- use this to stack up various contextual details throughout your
50+ -- code and they will be flattened out and combined in the log
51+ -- output.
52+ katipAddNamespace "confrabulation" $ katipAddContext (ConfrabLogCTX 42) $ do
53+ $(logTM) DebugS "Confrabulating widgets, with extra namespace and context"
54+ confrabulateWidgets
55+ $(logTM) InfoS "Namespace and context are back to normal"
56+ katipNoLogging $
57+ $(logTM) DebugS "You'll never see this log message!"
58+
5759
5860-------------------------------------------------------------------------------
5961newtype ConfrabLogCTX = ConfrabLogCTX Int