on master help sign in
Merge branch 'master' into katip-datadog
8c157b3Michael Xavier
katip-elasticsearch/examples/example.hs+1−1
@@ -27,7 +27,7 @@ main = do
27 bhe
27 bhe
28 (IndexName "all-indices-prefixed-with")
28 (IndexName "all-indices-prefixed-with")
29 (MappingName "application-logs")
29 (MappingName "application-logs")
30− DebugS
30+ (permitItem DebugS)
31 V3
31 V3
32 let mkLogEnv = registerScribe "es" esScribe defaultScribeSettings =<< initLogEnv "MyApp" "production"
32 let mkLogEnv = registerScribe "es" esScribe defaultScribeSettings =<< initLogEnv "MyApp" "production"
33 bracket mkLogEnv closeScribes $ \le -> runKatipT le $ do
33 bracket mkLogEnv closeScribes $ \le -> runKatipT le $ do
katip-elasticsearch/katip-elasticsearch.cabal+2−2
@@ -1,7 +1,7 @@
1name: katip-elasticsearch
1name: katip-elasticsearch
2synopsis: ElasticSearch scribe for the Katip logging framework.
2synopsis: ElasticSearch scribe for the Katip logging framework.
3description: See README.md for more details.
3description: See README.md for more details.
4−version: 0.5.1.1
4+version: 0.6.0.0
5license: BSD3
5license: BSD3
6license-file: LICENSE
6license-file: LICENSE
7author: Ozgun Ataman, Michael Xavier
7author: Ozgun Ataman, Michael Xavier
@@ -35,7 +35,7 @@ library
35 Katip.Scribes.ElasticSearch.Internal
35 Katip.Scribes.ElasticSearch.Internal
36 build-depends:
36 build-depends:
37 base >=4.6 && <5
37 base >=4.6 && <5
38− , katip >= 0.2.0.0 && < 0.8
38+ , katip >= 0.2.0.0 && < 0.9
39 , bloodhound >= 0.13.0.0 && < 0.17
39 , bloodhound >= 0.13.0.0 && < 0.17
40 , uuid >= 1.3.12 && < 1.4
40 , uuid >= 1.3.12 && < 1.4
41 , aeson >=0.6
41 , aeson >=0.6
katip-elasticsearch/src/Katip/Scribes/ElasticSearch/Internal.hs+4−4
@@ -269,10 +269,10 @@ mkEsScribe
269 -> IndexName v
269 -> IndexName v
270 -- ^ Treated as a prefix if index sharding is enabled
270 -- ^ Treated as a prefix if index sharding is enabled
271 -> MappingName v
271 -> MappingName v
272− -> Severity
272+ -> PermitFunc
273 -> Verbosity
273 -> Verbosity
274 -> IO Scribe
274 -> IO Scribe
275−mkEsScribe cfg@EsScribeCfg {..} env ix mapping sev verb = do
275+mkEsScribe cfg@EsScribeCfg {..} env ix mapping permit verb = do
276 q <- newTBMQueueIO $ unEsQueueSize essQueueSize
276 q <- newTBMQueueIO $ unEsQueueSize essQueueSize
277 endSig <- newEmptyMVar
277 endSig <- newEmptyMVar
278
278
@@ -308,10 +308,10 @@ mkEsScribe cfg@EsScribeCfg {..} env ix mapping sev verb = do
308 putMVar endSig ()
308 putMVar endSig ()
309
309
310 let finalizer = putMVar endSig () >> takeMVar endSig
310 let finalizer = putMVar endSig () >> takeMVar endSig
311− return (Scribe (logger q) finalizer)
311+ return (Scribe (logger q) finalizer permit)
312 where
312 where
313 logger :: forall a. LogItem a => TBMQueue (IndexName v, Value) -> Item a -> IO ()
313 logger :: forall a. LogItem a => TBMQueue (IndexName v, Value) -> Item a -> IO ()
314− logger q i = when (_itemSeverity i >= sev) $
314+ logger q i =
315 void $ atomically $ tryWriteTBMQueue q (chooseIxn prx ix essIndexSharding i, itemJson' i)
315 void $ atomically $ tryWriteTBMQueue q (chooseIxn prx ix essIndexSharding i, itemJson' i)
316 prx :: Typeable.Proxy v
316 prx :: Typeable.Proxy v
317 prx = Typeable.Proxy
317 prx = Typeable.Proxy
katip-elasticsearch/test/Main.hs+1−1
@@ -161,7 +161,7 @@ setupSearch
161setupSearch prx modScribeCfg = do
161setupSearch prx modScribeCfg = do
162 bh prx (dropESSchema prx)
162 bh prx (dropESSchema prx)
163 mgr <- newManager defaultManagerSettings
163 mgr <- newManager defaultManagerSettings
164− mkEsScribe cfg (mkBHEnv prx (svr prx) mgr) (ixn prx) (mn prx) DebugS V3
164+ mkEsScribe cfg (mkBHEnv prx (svr prx) mgr) (ixn prx) (mn prx) (permitItem DebugS) V3
165 where
165 where
166 cfg :: EsScribeCfg v
166 cfg :: EsScribeCfg v
167 cfg = modScribeCfg $
167 cfg = modScribeCfg $
katip/bench/Main.hs+5−2
@@ -92,7 +92,7 @@ setup dest = do
92 return (tmp </> "katip-bench.log")
92 return (tmp </> "katip-bench.log")
93 DevNull -> return ("/dev/null")
93 DevNull -> return ("/dev/null")
94 h <- openFile outFile WriteMode
94 h <- openFile outFile WriteMode
95− s <- mkHandleScribe ColorIfTerminal h DebugS V0
95+ s <- mkHandleScribe ColorIfTerminal h (permitItem DebugS) V0
96 let cleanupHandle = hClose h `finally` (when (dest == TempFile) (removeLink outFile))
96 let cleanupHandle = hClose h `finally` (when (dest == TempFile) (removeLink outFile))
97 return s { scribeFinalizer = scribeFinalizer s `finally` cleanupHandle}
97 return s { scribeFinalizer = scribeFinalizer s `finally` cleanupHandle}
98
98
@@ -101,4 +101,7 @@ setup dest = do
101deriving instance NFData ThreadIdText
101deriving instance NFData ThreadIdText
102
102
103instance NFData Scribe where
103instance NFData Scribe where
104− rnf (Scribe a b) = (a :: Item ExPayload -> IO ()) `seq` b `seq` ()
104+ rnf (Scribe a b p) = (a :: Item ExPayload -> IO ())
105+ `seq` b
106+ `seq` (p :: Item ExPayload -> IO Bool)
107+ `seq` ()
katip/examples/example.hs+1−1
@@ -36,7 +36,7 @@ main = do
36 -- hot-swap scribes at runtime if you need to. 'closeScribes' is
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
37 -- blocking and flushes all messages out of a scribe and cleans up
38 -- resources that were allocated at creation.
38 -- resources that were allocated at creation.
39− handleScribe <- mkHandleScribe ColorIfTerminal stdout InfoS V2
39+ handleScribe <- mkHandleScribe ColorIfTerminal stdout (permitItem InfoS) V2
40 let mkLogEnv = registerScribe "stdout" handleScribe defaultScribeSettings =<< initLogEnv "MyApp" "production"
40 let mkLogEnv = registerScribe "stdout" handleScribe defaultScribeSettings =<< initLogEnv "MyApp" "production"
41 bracket mkLogEnv closeScribes $ \le -> do
41 bracket mkLogEnv closeScribes $ \le -> do
42 let s = MyState M.mempty mempty le
42 let s = MyState M.mempty mempty le
katip/examples/example_lens.hs+1−1
@@ -51,7 +51,7 @@ main = do
51 -- hot-swap scribes at runtime if you need to. 'closeScribes' is
51 -- hot-swap scribes at runtime if you need to. 'closeScribes' is
52 -- blocking and flushes all messages out of a scribe and cleans up
52 -- blocking and flushes all messages out of a scribe and cleans up
53 -- resources that were allocated at creation.
53 -- resources that were allocated at creation.
54− handleScribe <- mkHandleScribe ColorIfTerminal stdout InfoS V2
54+ handleScribe <- mkHandleScribe ColorIfTerminal stdout (permitItem InfoS) V2
55 let mkLogEnv = registerScribe "stdout" handleScribe defaultScribeSettings =<< initLogEnv "MyApp" "production"
55 let mkLogEnv = registerScribe "stdout" handleScribe defaultScribeSettings =<< initLogEnv "MyApp" "production"
56 bracket mkLogEnv closeScribes $ \le -> do
56 bracket mkLogEnv closeScribes $ \le -> do
57 let s = MyState M.mempty mempty le
57 let s = MyState M.mempty mempty le
katip/katip.cabal+1−1
@@ -1,5 +1,5 @@
1name: katip
1name: katip
2−version: 0.7.0.0
2+version: 0.8.0.0
3synopsis: A structured logging framework.
3synopsis: A structured logging framework.
4description:
4description:
5 Katip is a structured logging framework. See README.md for more details.
5 Katip is a structured logging framework. See README.md for more details.
katip/src/Katip.hs+6−3
@@ -11,8 +11,8 @@
11--
11--
12-- main :: IO ()
12-- main :: IO ()
13-- main = do
13-- main = do
14−-- handleScribe <- mkHandleScribe ColorIfTerminal stdout InfoS V2
14+-- handleScribe <- mkHandleScribe ColorIfTerminal stdout (permitItem InfoS) V2
15−-- let makeLogEnv = registerScribe "stdout" handleScribe defaultScribeSettings =<< initLogEnv "MyApp" "production"
15+-- let makeLogEnv = registerScribe "stdout" handleScribe defaultScribeSettings =<< initLogEnv \"MyApp\" \"production\"
16-- -- closeScribes will stop accepting new logs, flush existing ones and clean up resources
16-- -- closeScribes will stop accepting new logs, flush existing ones and clean up resources
17-- bracket makeLogEnv closeScribes $ \le -> do
17-- bracket makeLogEnv closeScribes $ \le -> do
18-- let initialContext = () -- this context will be attached to every log in your app and merged w/ subsequent contexts
18-- let initialContext = () -- this context will be attached to every log in your app and merged w/ subsequent contexts
@@ -153,7 +153,7 @@ module Katip
153 , LogStr (..)
153 , LogStr (..)
154 , logStr, ls, showLS
154 , logStr, ls, showLS
155
155
156− -- ** 'Katip' LoggingFunctions
156+ -- ** 'Katip' Logging Functions
157 -- $katiplogging
157 -- $katiplogging
158 , Katip (..)
158 , Katip (..)
159 , logF
159 , logF
@@ -186,6 +186,9 @@ module Katip
186 , jsonFormat
186 , jsonFormat
187
187
188 -- * Tools for implementing Scribes
188 -- * Tools for implementing Scribes
189+ , PermitFunc
190+ , permitAND
191+ , permitOR
189 , permitItem
192 , permitItem
190 , payloadObject
193 , payloadObject
191 , itemJson
194 , itemJson
katip/src/Katip/Core.hs+82−35
@@ -29,7 +29,7 @@ import qualified Control.Concurrent.Async as Async
29import Control.Concurrent.STM
29import Control.Concurrent.STM
30import qualified Control.Concurrent.STM.TBQueue as BQ
30import qualified Control.Concurrent.STM.TBQueue as BQ
31import Control.Exception.Safe
31import Control.Exception.Safe
32−import Control.Monad (unless, void)
32+import Control.Monad (unless, void, when)
33import Control.Monad.Base
33import Control.Monad.Base
34import Control.Monad.IO.Class
34import Control.Monad.IO.Class
35import Control.Monad.IO.Unlift
35import Control.Monad.IO.Unlift
@@ -532,45 +532,86 @@ itemJson verb a = toJSON $ a & itemPayload %~ payloadObject verb
532--
532--
533-- Scribes should always take a 'Severity' and 'Verbosity'.
533-- Scribes should always take a 'Severity' and 'Verbosity'.
534--
534--
535−-- Severity is used to *exclude log messages* that are < the provided
535+-- Severity is used to __exclude log messages__ that are lower than
536−-- Severity. For instance, if the user passes InfoS, DebugS items
536+-- the provided Severity. For instance, if the user passes InfoS,
537−-- should be ignored. Katip provides the 'permitItem' utility for this.
537+-- DebugS items should be ignored. Katip provides the 'permitItem'
538+-- utility for this. The user or the scribe may use 'permitAND' and
539+-- 'permitOR' to further customize this filtering, even dynamically if
540+-- they wish to.
538--
541--
539-- Verbosity is used to select keys from the log item's payload. Each
542-- Verbosity is used to select keys from the log item's payload. Each
540-- 'LogItem' instance describes what keys should be retained for each
543-- 'LogItem' instance describes what keys should be retained for each
541-- Verbosity level. Use the 'payloadObject' utility for extracting the keys
544-- Verbosity level. Use the 'payloadObject' utility for extracting the keys
542−-- that should be permitted.
545+-- that should be written.
543--
546--
544−-- There is no built-in mechanism in katip for telling a scribe that
547+-- Scribes provide a finalizer IO action ('scribeFinalizer') that is
545−-- its time to shut down. 'unregisterScribe' merely drops it from the
548+-- meant to synchronously flush any remaining writes and clean up any
546−-- 'LogEnv'. This means there are 2 ways to handle resources as a scribe:
549+-- resources acquired when the scribe was created. Internally, katip
550+-- keeps a buffer for each scribe's writes. When 'closeScribe' or
551+-- 'closeScribes' is called, that buffer stops accepting new log
552+-- messages and after the last item in its buffer is sent to 'liPush',
553+-- calls the finalizer. Thus, when the finalizer returns, katip can
554+-- assume that all resources are cleaned up and all log messages are
555+-- durably written.
547--
556--
548−-- 1. Pass in the resource when the scribe is created. Handle
557+-- While katip internally buffers messages per 'ScribeSettings', it
549−-- allocation and release of the resource elsewhere. This is what the
558+-- sends them one at a time to the scribe. Depending on the scribe
550−-- Handle scribe does.
559+-- itself, it may make sense for that scribe to keep its own internal
551−--
560+-- buffer to batch-send logs if writing items one at a time is not
552−-- 2. Return a finalizing function that tells the scribe to shut
561+-- efficient. The scribe implementer must be sure that on
553−-- down. @katip-elasticsearch@'s @mkEsScribe@ returns an @IO (Scribe,
562+-- finalization, all writes are committed synchronously.
554−-- IO ())@. The finalizer will flush any queued log messages and shut
563+
555−-- down gracefully before returning. This can be hooked into your
564+-- | Signature of a function passed to `Scribe` constructor and
556−-- application's shutdown routine to ensure you never miss any log
565+-- mkScribe* functions that decides which messages to be
557−-- messages on shutdown.
566+-- logged. Typically filters based on 'Severity', but can be
567+-- combined with other, custom logic with 'permitAND' and 'permitOR'
568+type PermitFunc = forall a. Item a -> IO Bool
569+
570+
571+-- | AND together 2 permit functions
572+permitAND :: PermitFunc -> PermitFunc -> PermitFunc
573+permitAND f1 f2 = \a -> liftA2 (&&) (f1 a) (f2 a)
574+
575+-- | OR together 2 permit functions
576+permitOR :: PermitFunc -> PermitFunc -> PermitFunc
577+permitOR f1 f2 = \a -> liftA2 (||) (f1 a) (f2 a)
578+
579+
558data Scribe = Scribe {
580data Scribe = Scribe {
559 liPush :: forall a. LogItem a => Item a -> IO ()
581 liPush :: forall a. LogItem a => Item a -> IO ()
582+ -- ^ How do we write an item to the scribe's output?
560 , scribeFinalizer :: IO ()
583 , scribeFinalizer :: IO ()
561− -- ^ Provide a *blocking* finalizer to call when your scribe is
584+ -- ^ Provide a __blocking__ finalizer to call when your scribe is
585+ -- removed. All pending writes should be flushed synchronously. If
562− -- removed. If this is not relevant to your scribe, return () is
586+ -- this is not relevant to your scribe, return () is fine.
563− -- fine.
587+ , scribePermitItem :: PermitFunc
588+ -- ^ Provide a filtering function to allow the item to be logged,
589+ -- or not. It can check Severity or some string in item's
590+ -- body. The initial value of this is usually created from
591+ -- 'permitItem'. Scribes and users can customize this by ANDing
592+ -- or ORing onto the default with 'permitAND' or 'permitOR'
564 }
593 }
565
594
566
595
596+whenM :: Monad m => m Bool -> m () -> m ()
597+whenM mbool = (>>=) mbool . flip when
598+
599+
600+-- | Combine two scribes. Publishes to the left scribe if the left
601+-- would permit the item and to the right scribe if the right would
602+-- permit the item. Finalizers are called in sequence from left to
603+-- right.
567instance Semigroup Scribe where
604instance Semigroup Scribe where
568− (Scribe pushA finA) <> (Scribe pushB finB) =
605+ (Scribe pushA finA permitA) <> (Scribe pushB finB permitB) =
606+ Scribe (\item -> whenM (permitA item) (pushA item)
607+ >> whenM (permitB item) (pushB item)
608+ )
569− Scribe (\item -> pushA item >> pushB item) (finA `finally` finB)
609+ (finA `finally` finB)
610+ (permitOR permitA permitB)
570
611
571
612
572instance Monoid Scribe where
613instance Monoid Scribe where
573− mempty = Scribe (const (return ())) (return ())
614+ mempty = Scribe (const (return ())) (return ()) (permitItem DebugS)
574 mappend = (<>)
615 mappend = (<>)
575
616
576
617
@@ -589,8 +630,9 @@ data WorkerMessage where
589
630
590-------------------------------------------------------------------------------
631-------------------------------------------------------------------------------
591-- | Should this item be logged given the user's maximum severity?
632-- | Should this item be logged given the user's maximum severity?
633+-- Most new scribes will use this as a base for their 'PermitFunc'
592−permitItem :: Severity -> Item a -> Bool
634+permitItem :: Monad m => Severity -> Item a -> m Bool
593−permitItem sev i = _itemSeverity i >= sev
635+permitItem sev item = return (_itemSeverity item >= sev)
594
636
595
637
596-------------------------------------------------------------------------------
638-------------------------------------------------------------------------------
@@ -636,7 +678,9 @@ initLogEnv an env = LogEnv
636
678
637-------------------------------------------------------------------------------
679-------------------------------------------------------------------------------
638-- | Add a scribe to the list. All future log calls will go to this
680-- | Add a scribe to the list. All future log calls will go to this
639−-- scribe in addition to the others.
681+-- scribe in addition to the others. Writes will be buffered per the
682+-- ScribeSettings to prevent slow scribes from slowing down
683+-- logging. Writes will be dropped if the buffer fills.
640registerScribe
684registerScribe
641 :: Text
685 :: Text
642 -- ^ Name the scribe
686 -- ^ Name the scribe
@@ -660,7 +704,7 @@ registerScribe nm scribe ScribeSettings {..} le = do
660
704
661-------------------------------------------------------------------------------
705-------------------------------------------------------------------------------
662spawnScribeWorker :: Scribe -> BQ.TBQueue WorkerMessage -> IO (Async.Async ())
706spawnScribeWorker :: Scribe -> BQ.TBQueue WorkerMessage -> IO (Async.Async ())
663−spawnScribeWorker (Scribe write _) queue = Async.async go
707+spawnScribeWorker (Scribe write _ _) queue = Async.async go
664 where
708 where
665 go = do
709 go = do
666 newCmd <- atomically (BQ.readTBQueue queue)
710 newCmd <- atomically (BQ.readTBQueue queue)
@@ -689,7 +733,7 @@ defaultScribeSettings = ScribeSettings 4096
689
733
690
734
691-------------------------------------------------------------------------------
735-------------------------------------------------------------------------------
692−-- | Remove a scribe from the environment. This does *not* finalize
736+-- | Remove a scribe from the environment. This does __not__ finalize
693-- the scribe. This mainly only makes sense to use with something like
737-- the scribe. This mainly only makes sense to use with something like
694-- MonadReader's @local@ function to temporarily disavow a single
738-- MonadReader's @local@ function to temporarily disavow a single
695-- logger for a block of code.
739-- logger for a block of code.
@@ -702,7 +746,7 @@ unregisterScribe nm = logEnvScribes %~ M.delete nm
702
746
703
747
704-------------------------------------------------------------------------------
748-------------------------------------------------------------------------------
705−-- | Unregister *all* scribes. Note that this is *not* for closing or
749+-- | Unregister __all__ scribes. Note that this is __not__ for closing or
706-- finalizing scribes, use 'closeScribes' for that. This mainly only
750-- finalizing scribes, use 'closeScribes' for that. This mainly only
707-- makes sense to use with something like MonadReader's @local@
751-- makes sense to use with something like MonadReader's @local@
708-- function to temporarily disavow any loggers for a block of code.
752-- function to temporarily disavow any loggers for a block of code.
@@ -714,9 +758,10 @@ clearScribes = logEnvScribes .~ mempty
714
758
715-------------------------------------------------------------------------------
759-------------------------------------------------------------------------------
716-- | Finalize a scribe. The scribe is removed from the environment,
760-- | Finalize a scribe. The scribe is removed from the environment,
717−-- its finalizer is called and it can never be written to again. Note
761+-- its finalizer is called so that it can never be written to again
762+-- and all pending writes are flushed. Note that this will throw any
718−-- that this will throw any exceptions yoru finalizer will throw, and
763+-- exceptions yoru finalizer will throw, and that LogEnv is immutable,
719−-- that LogEnv is immutable, so it will not be removed in that case.
764+-- so it will not be removed in that case.
720closeScribe
765closeScribe
721 :: Text
766 :: Text
722 -- ^ Name of the scribe
767 -- ^ Name of the scribe
@@ -896,8 +941,9 @@ logItem a ns loc sev msg = do
896 <*> _logEnvTimer
941 <*> _logEnvTimer
897 <*> pure (_logEnvApp <> ns)
942 <*> pure (_logEnvApp <> ns)
898 <*> pure loc
943 <*> pure loc
944+ FT.forM_ (M.elems _logEnvScribes) $ \ ScribeHandle {..} -> do
945+ whenM (scribePermitItem shScribe item) $
899− FT.forM_ (M.elems _logEnvScribes) $ \ ScribeHandle {..} -> atomically (tryWriteTBQueue shChan (NewItem item))
946+ void $ atomically (tryWriteTBQueue shChan (NewItem item))
900−
901
947
902-------------------------------------------------------------------------------
948-------------------------------------------------------------------------------
903tryWriteTBQueue
949tryWriteTBQueue
@@ -910,6 +956,7 @@ tryWriteTBQueue q a = do
910 unless full (writeTBQueue q a)
956 unless full (writeTBQueue q a)
911 return (not full)
957 return (not full)
912
958
959+
913-------------------------------------------------------------------------------
960-------------------------------------------------------------------------------
914-- | Log with full context, but without any code location.
961-- | Log with full context, but without any code location.
915logF
962logF
katip/src/Katip/Scribes/Handle.hs+17−13
@@ -4,7 +4,6 @@ module Katip.Scribes.Handle where
4import Control.Applicative as A
4import Control.Applicative as A
5import Control.Concurrent
5import Control.Concurrent
6import Control.Exception (bracket_, finally)
6import Control.Exception (bracket_, finally)
7−import Control.Monad
8import Data.Aeson
7import Data.Aeson
9import Data.Text.Lazy (toStrict)
8import Data.Text.Lazy (toStrict)
10import Data.Text.Lazy.Encoding (decodeUtf8)
9import Data.Text.Lazy.Encoding (decodeUtf8)
@@ -63,7 +62,7 @@ data ColorStrategy
63--
62--
64-- Returns the newly-created `Scribe`. The finalizer flushes the
63-- Returns the newly-created `Scribe`. The finalizer flushes the
65-- handle. Handle mode is set to 'LineBuffering' automatically.
64-- handle. Handle mode is set to 'LineBuffering' automatically.
66−mkHandleScribe :: ColorStrategy -> Handle -> Severity -> Verbosity -> IO Scribe
65+mkHandleScribe :: ColorStrategy -> Handle -> PermitFunc -> Verbosity -> IO Scribe
67mkHandleScribe = mkHandleScribeWithFormatter bracketFormat
66mkHandleScribe = mkHandleScribeWithFormatter bracketFormat
68
67
69-- | Logs to a file handle such as stdout, stderr, or a file. Takes a custom
68-- | Logs to a file handle such as stdout, stderr, or a file. Takes a custom
@@ -71,17 +70,22 @@ mkHandleScribe = mkHandleScribeWithFormatter bracketFormat
71--
70--
72-- Returns the newly-created `Scribe`. The finalizer flushes the
71-- Returns the newly-created `Scribe`. The finalizer flushes the
73-- handle. Handle mode is set to 'LineBuffering' automatically.
72-- handle. Handle mode is set to 'LineBuffering' automatically.
74−mkHandleScribeWithFormatter :: (forall a . LogItem a => ItemFormatter a) -> ColorStrategy -> Handle -> Severity -> Verbosity -> IO Scribe
73+mkHandleScribeWithFormatter :: (forall a . LogItem a => ItemFormatter a)
74+ -> ColorStrategy
75+ -> Handle
76+ -> PermitFunc
77+ -> Verbosity
78+ -> IO Scribe
75−mkHandleScribeWithFormatter itemFormatter cs h sev verb = do
79+mkHandleScribeWithFormatter itemFormatter cs h permitF verb = do
76 hSetBuffering h LineBuffering
80 hSetBuffering h LineBuffering
77 colorize <- case cs of
81 colorize <- case cs of
78 ColorIfTerminal -> hIsTerminalDevice h
82 ColorIfTerminal -> hIsTerminalDevice h
79 ColorLog b -> return b
83 ColorLog b -> return b
80 lock <- newMVar ()
84 lock <- newMVar ()
81 let logger i@Item{..} = do
85 let logger i@Item{..} = do
82− when (permitItem sev i) $ bracket_ (takeMVar lock) (putMVar lock ()) $
86+ bracket_ (takeMVar lock) (putMVar lock ()) $
83 T.hPutStrLn h $ toLazyText $ itemFormatter colorize verb i
87 T.hPutStrLn h $ toLazyText $ itemFormatter colorize verb i
84− return $ Scribe logger (hFlush h)
88+ return $ Scribe logger (hFlush h) permitF
85
89
86
90
87-------------------------------------------------------------------------------
91-------------------------------------------------------------------------------
@@ -90,11 +94,11 @@ mkHandleScribeWithFormatter itemFormatter cs h sev verb = do
90-- 'AppendMode' and will close the file handle on
94-- 'AppendMode' and will close the file handle on
91-- 'closeScribe'/'closeScribes'. Does not do log coloring. Sets handle
95-- 'closeScribe'/'closeScribes'. Does not do log coloring. Sets handle
92-- to 'LineBuffering' mode.
96-- to 'LineBuffering' mode.
93−mkFileScribe :: FilePath -> Severity -> Verbosity -> IO Scribe
97+mkFileScribe :: FilePath -> PermitFunc -> Verbosity -> IO Scribe
94−mkFileScribe f sev verb = do
98+mkFileScribe f permitF verb = do
95 h <- openFile f AppendMode
99 h <- openFile f AppendMode
96− Scribe logger finalizer <- mkHandleScribe (ColorLog False) h sev verb
100+ Scribe logger finalizer permit <- mkHandleScribe (ColorLog False) h permitF verb
97− return (Scribe logger (finalizer `finally` hClose h))
101+ return (Scribe logger (finalizer `finally` hClose h) permit)
98
102
99
103
100-------------------------------------------------------------------------------
104-------------------------------------------------------------------------------
@@ -169,8 +173,8 @@ colorBySeverity withColor severity msg = case severity of
169-- stdout. This is a decent example of how to build a LogEnv and is
173-- stdout. This is a decent example of how to build a LogEnv and is
170-- best for scripts that just need a quick, reasonable set up to log
174-- best for scripts that just need a quick, reasonable set up to log
171-- to stdout.
175-- to stdout.
172−ioLogEnv :: Severity -> Verbosity -> IO LogEnv
176+ioLogEnv :: PermitFunc -> Verbosity -> IO LogEnv
173−ioLogEnv sev verb = do
177+ioLogEnv permit verb = do
174 le <- initLogEnv "io" "io"
178 le <- initLogEnv "io" "io"
175− lh <- mkHandleScribe ColorIfTerminal stdout sev verb
179+ lh <- mkHandleScribe ColorIfTerminal stdout permit verb
176 registerScribe "stdout" lh defaultScribeSettings le
180 registerScribe "stdout" lh defaultScribeSettings le
katip/test/Katip/Tests.hs+3−2
@@ -141,6 +141,7 @@ loggingTests = testGroup "logging"
141 let scribe = Scribe
141 let scribe = Scribe
142 { liPush = \i -> atomically (modifyTVar' items (<> [toObject <$> i]))
142 { liPush = \i -> atomically (modifyTVar' items (<> [toObject <$> i]))
143 , scribeFinalizer = return ()
143 , scribeFinalizer = return ()
144+ , scribePermitItem = permitItem DebugS
144 }
145 }
145 le1 <- initLogEnv "tests" "test"
146 le1 <- initLogEnv "tests" "test"
146 le2 <- registerScribe "recorder" scribe defaultScribeSettings le1
147 le2 <- registerScribe "recorder" scribe defaultScribeSettings le1
@@ -154,7 +155,7 @@ trivialScribe :: IO (Scribe, TVar Bool)
154trivialScribe = do
155trivialScribe = do
155 finalizerCalled <- newTVarIO False
156 finalizerCalled <- newTVarIO False
156 let finalizer = atomically (writeTVar finalizerCalled True)
157 let finalizer = atomically (writeTVar finalizerCalled True)
157− return (Scribe (const (return ())) finalizer, finalizerCalled)
158+ return (Scribe (const (return ())) finalizer (permitItem DebugS), finalizerCalled)
158
159
159
160
160-------------------------------------------------------------------------------
161-------------------------------------------------------------------------------
@@ -164,7 +165,7 @@ brokenScribe scribeNum = do
164 let finalizer = do
165 let finalizer = do
165 atomically (writeTVar finalizerCalled True)
166 atomically (writeTVar finalizerCalled True)
166 throw (ScribeBroken scribeNum)
167 throw (ScribeBroken scribeNum)
167− return (Scribe (const (return ())) finalizer, finalizerCalled)
168+ return (Scribe (const (return ())) finalizer (permitItem DebugS), finalizerCalled)
168
169
169
170
170-------------------------------------------------------------------------------
171-------------------------------------------------------------------------------
katip/test/Katip/Tests/Scribes/Handle.hs+2−2
@@ -92,7 +92,7 @@ setup :: IO (FilePath, Handle, IO (), LogEnv)
92setup = do
92setup = do
93 tempDir <- getTemporaryDirectory
93 tempDir <- getTemporaryDirectory
94 (fp, h) <- openTempFile tempDir "katip.log"
94 (fp, h) <- openTempFile tempDir "katip.log"
95− s <- mkHandleScribe (ColorLog False) h DebugS V3
95+ s <- mkHandleScribe (ColorLog False) h (permitItem DebugS) V3
96 le <- initLogEnv "katip-test" "test"
96 le <- initLogEnv "katip-test" "test"
97 le' <- registerScribe "handle" s defaultScribeSettings le
97 le' <- registerScribe "handle" s defaultScribeSettings le
98 return (fp, h, void (closeScribes le'), le')
98 return (fp, h, void (closeScribes le'), le')
@@ -111,7 +111,7 @@ setupFile = do
111 tempDir <- getTemporaryDirectory
111 tempDir <- getTemporaryDirectory
112 (fp, h) <- openTempFile tempDir "katip.log"
112 (fp, h) <- openTempFile tempDir "katip.log"
113 hClose h
113 hClose h
114− s <- mkFileScribe fp DebugS V3
114+ s <- mkFileScribe fp (permitItem DebugS) V3
115 le <- initLogEnv "katip-test" "test"
115 le <- initLogEnv "katip-test" "test"
116 le' <- registerScribe "handle" s defaultScribeSettings le
116 le' <- registerScribe "handle" s defaultScribeSettings le
117 return (fp, void (closeScribes le'), le')
117 return (fp, void (closeScribes le'), le')
stack.yaml+1−1
@@ -1,4 +1,4 @@
1−resolver: lts-11.14
1+resolver: lts-12.22
2packages:
2packages:
3 - katip
3 - katip
4 - katip-elasticsearch
4 - katip-elasticsearch