git.iko.soy/typeable/control-logger
on master help sign in
expand the api
16c79c4Ville Tirronen
src/Control/Logger.hs+31−4
@@ -3,6 +3,8 @@ module Control.Logger
3 ( Logger
3 ( Logger
4 , loggerContext
4 , loggerContext
5 , scrubber
5 , scrubber
6+ , censor
7+ , censorWith
6 , logMsg
8 , logMsg
7 , logMsgWith
9 , logMsgWith
8 , logInfo
10 , logInfo
@@ -34,6 +36,7 @@ module Control.Logger
34 , st
36 , st
35 , ToObject(..)
37 , ToObject(..)
36 , withScrubber
38 , withScrubber
39+ , censoringWith
37 , censoring
40 , censoring
38 ) where
41 ) where
39
42
@@ -51,6 +54,7 @@ import GHC.Stack
51import Katip (ToObject (..))
54import Katip (ToObject (..))
52import System.Log.FastLogger
55import System.Log.FastLogger
53import Text.Shakespeare.Text (st)
56import Text.Shakespeare.Text (st)
57+-- import Control.Monad.RWS (censor)
54#if MIN_VERSION_mtl(2,3,0)
58#if MIN_VERSION_mtl(2,3,0)
55import Control.Monad
59import Control.Monad
56import Control.Monad.IO.Class
60import Control.Monad.IO.Class
@@ -108,7 +112,21 @@ logErrorWith
108 -> m ()
112 -> m ()
109logErrorWith logger msg = withFrozenCallStack (logMsgWith logger Error msg)
113logErrorWith logger msg = withFrozenCallStack (logMsgWith logger Error msg)
110
114
111−-- | Adjust the scrubbing function.
115+safeReplace :: Text -> Text -> Text -> Text
116+safeReplace what with where_
117+ | Text.null what = where_
118+ | otherwise = Text.replace what with where_
119+
120+-- | Add a some text to scrubber
121+censorWith :: Text -> Text -> Logger -> Logger
122+censorWith replacement needle l =
123+ l & scrubber <>~ Endo (safeReplace needle replacement)
124+
125+-- | Censor with "***"
126+censor :: Text -> Logger -> Logger
127+censor = censorWith "***"
128+
129+-- | Run an action with adjusted scrubber
112withScrubber
130withScrubber
113 :: forall r m a. (Has Logger r, MonadReader r m)
131 :: forall r m a. (Has Logger r, MonadReader r m)
114 => (Endo Text -> Endo Text)
132 => (Endo Text -> Endo Text)
@@ -120,14 +138,23 @@ withScrubber scrubF = local expandScrubber
120 expandScrubber = over part (over scrubber scrubF)
138 expandScrubber = over part (over scrubber scrubF)
121
139
122-- | Run an action while censoring additional text from logs
140-- | Run an action while censoring additional text from logs
123−censoring
141+censoringWith
124 :: (Has Logger r, MonadReader r m)
142 :: (Has Logger r, MonadReader r m)
125 => Text
143 => Text
144+ -> Text
126 -> m a
145 -> m a
127 -> m a
146 -> m a
128−censoring txt = withScrubber (<> censored)
147+censoringWith replacement txt = withScrubber (<> censored)
129 where
148 where
130− censored = Endo (Text.replace txt "XXXX")
149+ censored = Endo (safeReplace txt replacement)
150+
151+-- | Run an action while censoring additional text from logs
152+censoring
153+ :: (Has Logger r, MonadReader r m)
154+ => Text
155+ -> m a
156+ -> m a
157+censoring = censoringWith "***"
131
158
132-- | Log exceptions occured in computation and just ignore them.
159-- | Log exceptions occured in computation and just ignore them.
133tryLogError
160tryLogError