@@ -3,6 +3,8 @@ module Control.Logger
33 ( Logger
44 , loggerContext
55 , scrubber
6+ , censor
7+ , censorWith
68 , logMsg
79 , logMsgWith
810 , logInfo
@@ -34,6 +36,7 @@ module Control.Logger
3436 , st
3537 , ToObject(..)
3638 , withScrubber
39+ , censoringWith
3740 , censoring
3841 ) where
3942
@@ -51,6 +54,7 @@ import GHC.Stack
5154import Katip (ToObject (..))
5255import System.Log.FastLogger
5356import Text.Shakespeare.Text (st)
57+-- import Control.Monad.RWS (censor)
5458#if MIN_VERSION_mtl(2,3,0)
5559import Control.Monad
5660import Control.Monad.IO.Class
@@ -108,7 +112,21 @@ logErrorWith
108112 -> m ()
109113logErrorWith logger msg = withFrozenCallStack (logMsgWith logger Error msg)
110114
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
112130withScrubber
113131 :: forall r m a. (Has Logger r, MonadReader r m)
114132 => (Endo Text -> Endo Text)
@@ -120,14 +138,23 @@ withScrubber scrubF = local expandScrubber
120138 expandScrubber = over part (over scrubber scrubF)
121139
122140-- | Run an action while censoring additional text from logs
123−censoring
141+censoringWith
124142 :: (Has Logger r, MonadReader r m)
125143 => Text
144+ -> Text
126145 -> m a
127146 -> m a
128−censoring txt = withScrubber (<> censored)
147+censoringWith replacement txt = withScrubber (<> censored)
129148 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 "***"
131158
132159-- | Log exceptions occured in computation and just ignore them.
133160tryLogError