@@ -3,8 +3,6 @@ module Control.Logger
33 ( Logger
44 , loggerContext
55 , scrubber
6− , censor
7− , censorWith
86 , logMsg
97 , logMsgWith
108 , logInfo
@@ -36,7 +34,6 @@ module Control.Logger
3634 , st
3735 , ToObject(..)
3836 , withScrubber
39− , censoringWith
4037 , censoring
4138 ) where
4239
@@ -44,9 +41,7 @@ import Control.Has
4441import Control.Lens hiding (censoring)
4542import Control.Logger.Internal
4643import Control.Logger.Orphans ()
47−import Control.Monad
4844import Control.Monad.Catch
49−import Control.Monad.IO.Class
5045import qualified Data.List as List
5146import Data.Monoid
5247import Data.Text (Text)
@@ -56,6 +51,10 @@ import GHC.Stack
5651import Katip (ToObject (..))
5752import System.Log.FastLogger
5853import Text.Shakespeare.Text (st)
54+#if MIN_VERSION_mtl(2,3,0)
55+import Control.Monad
56+import Control.Monad.IO.Class
57+#endif
5958
6059logError
6160 :: (LoggingMonad r m)
@@ -109,41 +108,16 @@ logErrorWith
109108 -> m ()
110109logErrorWith logger msg = withFrozenCallStack (logMsgWith logger Error msg)
111110
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
111+-- | Adjust the scrubbing function.
127112withScrubber
128113 :: forall r m a. (Has Logger r, MonadReader r m)
129− => (Endo Text -> Endo Text)
114+ => Endo (Text -> Text)
130115 -> m a
131116 -> m a
132117withScrubber scrubF = local expandScrubber
133118 where
134119 expandScrubber :: r -> r
135− expandScrubber = over part (over scrubber scrubF)
136−
137−-- | Run an action while censoring additional text from logs
138−censoringWith
139− :: (Has Logger r, MonadReader r m)
140− => Text
141− -> Text
142− -> m a
143− -> m a
144−censoringWith replacement txt = withScrubber (<> censored)
145− where
146− censored = Endo (safeReplace txt replacement)
120+ expandScrubber = over part (over scrubber (appEndo scrubF))
147121
148122-- | Run an action while censoring additional text from logs
149123censoring
@@ -151,7 +125,9 @@ censoring
151125 => Text
152126 -> m a
153127 -> m a
154−censoring = censoringWith "***"
128+censoring txt = withScrubber (Endo (. censored))
129+ where
130+ censored = Text.replace txt "XXXX"
155131
156132-- | Log exceptions occured in computation and just ignore them.
157133tryLogError
@@ -203,14 +179,14 @@ fileLogger :: MonadIO m => FilePath -> m Logger
203179fileLogger fp = fastFunc <$> liftIO (newFileLoggerSet defaultBufSize fp)
204180
205181silentLogger :: Logger
206−silentLogger = Logger mempty mempty (\_ _ _ _ -> return ())
182+silentLogger = Logger mempty id (\_ _ _ _ -> return ())
207183
208184-- | Logger which flushes buffer after each message. This is useful with
209185-- stderrLogger.
210186flushingFastFunc
211187 :: LogSeverity -> LoggerSet -> Logger
212188flushingFastFunc minLogLevel loggerSet =
213− Logger mempty mempty $ \_ stack logLevel msg ->
189+ Logger mempty id $ \_ stack logLevel msg ->
214190 when (logLevel >= minLogLevel) $ do
215191 ts <- getCurrentTime
216192 pushLogStr loggerSet
@@ -234,14 +210,14 @@ flushingStderrLogger =
234210-- | Make our outdated 'Logger' of fast-logger's 'LoggerSet'
235211fastFunc :: LoggerSet -> Logger
236212fastFunc loggerSet =
237− Logger mempty mempty $ \_ _ _ msg -> pushLogStr loggerSet $ toLogStr msg
213+ Logger mempty id $ \_ _ _ msg -> pushLogStr loggerSet $ toLogStr msg
238214
239215type LogSource = Text
240216
241217-- | Default logger tests and same things.
242218defaultFastFunc :: LogSource -> LogSeverity -> LoggerSet -> Logger
243219defaultFastFunc loc minLogLevel loggerSet =
244− Logger mempty mempty $ \_ stack s msg -> when (s >= minLogLevel) $ do
220+ Logger mempty id $ \_ stack s msg -> when (s >= minLogLevel) $ do
245221 ts <- getCurrentTime
246222 pushLogStr loggerSet $ formatLogStr ts stack loc s (toLogStr msg)
247223