@@ -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
@@ -41,7 +44,9 @@ import Control.Has
4144import Control.Lens hiding (censoring)
4245import Control.Logger.Internal
4346import Control.Logger.Orphans ()
47+import Control.Monad
4448import Control.Monad.Catch
49+import Control.Monad.IO.Class
4550import qualified Data.List as List
4651import Data.Monoid
4752import Data.Text (Text)
@@ -51,10 +56,6 @@ import GHC.Stack
5156import Katip (ToObject (..))
5257import System.Log.FastLogger
5358import Text.Shakespeare.Text (st)
54−#if MIN_VERSION_mtl(2,3,0)
55−import Control.Monad
56−import Control.Monad.IO.Class
57−#endif
5859
5960logError
6061 :: (LoggingMonad r m)
@@ -108,7 +109,21 @@ logErrorWith
108109 -> m ()
109110logErrorWith logger msg = withFrozenCallStack (logMsgWith logger Error msg)
110111
111−-- | Adjust the scrubbing function.
112+safeReplace :: Text -> Text -> Text -> Text
113+safeReplace what with where_
114+ | Text.null what = where_
115+ | otherwise = Text.replace what with where_
116+
117+-- | Add a some text to scrubber
118+censorWith :: Text -> Text -> Logger -> Logger
119+censorWith replacement needle l =
120+ l & scrubber <>~ Endo (safeReplace needle replacement)
121+
122+-- | Censor with "***"
123+censor :: Text -> Logger -> Logger
124+censor = censorWith "***"
125+
126+-- | Run an action with adjusted scrubber
112127withScrubber
113128 :: forall r m a. (Has Logger r, MonadReader r m)
114129 => (Endo Text -> Endo Text)
@@ -120,14 +135,23 @@ withScrubber scrubF = local expandScrubber
120135 expandScrubber = over part (over scrubber scrubF)
121136
122137-- | Run an action while censoring additional text from logs
123−censoring
138+censoringWith
124139 :: (Has Logger r, MonadReader r m)
125140 => Text
141+ -> Text
126142 -> m a
127143 -> m a
128−censoring txt = withScrubber (<> censored)
144+censoringWith replacement txt = withScrubber (<> censored)
129145 where
130− censored = Endo (Text.replace txt "XXXX")
146+ censored = Endo (safeReplace txt replacement)
147+
148+-- | Run an action while censoring additional text from logs
149+censoring
150+ :: (Has Logger r, MonadReader r m)
151+ => Text
152+ -> m a
153+ -> m a
154+censoring = censoringWith "***"
131155
132156-- | Log exceptions occured in computation and just ignore them.
133157tryLogError