Skip to content
This repository has been archived by the owner on Aug 18, 2020. It is now read-only.

Commit

Permalink
[CBR-345] apply minSeverity as soon as possible
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Diemand <codieplusplus@apax.net>
  • Loading branch information
CodiePP committed Sep 17, 2018
1 parent 76bea25 commit d249ee9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
23 changes: 15 additions & 8 deletions util/src/Pos/Util/Wlog/Compatibility.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import qualified Pos.Util.Log.Internal as Internal
import Pos.Util.Log.LoggerConfig (LogHandler (..),
LogSecurityLevel (..), LoggerConfig (..),
defaultInteractiveConfiguration, lcLoggerTree, lhName,
ltHandlers)
ltHandlers, ltMinSeverity)
import System.IO.Unsafe (unsafePerformIO)

import Universum
Expand Down Expand Up @@ -89,12 +89,17 @@ instance CanLog IO where
mayEnv <- Internal.getLogEnv lh
case mayEnv of
Nothing -> error "logging not yet initialized. Abort."
Just env -> Log.logItem' ()
(K.Namespace (T.split (=='.') name))
env
Nothing
(Internal.sev2klog severity)
(K.logStr msg)
Just env -> do
mayConfig <- Internal.getConfig lh
case mayConfig of
Nothing -> error "no logging configuration. Abort."
Just lc -> when (severity >= lc ^. lcLoggerTree ^. ltMinSeverity)
$ Log.logItem' ()
(K.Namespace (T.split (=='.') name))
env
Nothing
(Internal.sev2klog severity)
(K.logStr msg)

type WithLogger m = (CanLog m, HasLoggerName m)

Expand Down Expand Up @@ -257,7 +262,9 @@ logItemS lhandler a ns loc sev cond msg = do
let cfg = case maycfg of
Nothing -> error "No Configuration for logging found. Abort."
Just c -> c
liftIO $ do
let sevmin = Internal.sev2klog $ cfg ^. lcLoggerTree ^. ltMinSeverity
when (sev >= sevmin)
$ liftIO $ do
item <- K.Item
<$> pure (K._logEnvApp le)
<*> pure (K._logEnvEnv le)
Expand Down
29 changes: 15 additions & 14 deletions util/test/Test/Pos/Util/WlogSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,13 @@ run_logging _ n n0 n1= do
logWarning msg
logError msg
endTime <- getPOSIXTime
threadDelay $ fromIntegral (5000 * n0)
threadDelay $ fromIntegral (8000 * n0)
let diffTime = nominalDiffTimeToMicroseconds (endTime - startTime)
putStrLn $ " time for " ++ (show (n0*n1)) ++ " iterations: " ++ (show diffTime)
lineslogged1 <- getLinesLogged
let lineslogged = lineslogged1 - lineslogged0
putStrLn $ " lines logged :" ++ (show lineslogged)
threadDelay 0500000 -- wait for empty queue
return (diffTime, lineslogged)
where msg :: Text
msg = replicate n "abcdefghijklmnopqrstuvwxyz"
Expand Down Expand Up @@ -102,6 +103,19 @@ spec = describe "Logging" $ do
lc = lc0 & lcLoggerTree .~ newlt
setupLogging lc

modifyMaxSuccess (const 2) $ modifyMaxSize (const 2) $
it "change minimum severity filter for a specific context" $
monadicIO $ do
lineslogged0 <- lift $ getLinesLogged
lift $ usingLoggerName "silent" $ do { logWarning "you won't see this!" }
lift $ threadDelay 0300000
lift $ usingLoggerName "verbose" $ do { logWarning "now you read this!" }
lift $ threadDelay 0300000
lineslogged1 <- lift $ getLinesLogged
let lineslogged = lineslogged1 - lineslogged0
putStrLn $ "lines logged: " ++ (show lineslogged)
assert (lineslogged == 1)

modifyMaxSuccess (const 1) $ modifyMaxSize (const 1) $
it "demonstrate logging" $
monadicIO $ lift $ someLogging
Expand All @@ -118,16 +132,3 @@ spec = describe "Logging" $ do
it "lines counted as logged must be equal to how many was intended to be written" $
property prop_lines

modifyMaxSuccess (const 2) $ modifyMaxSize (const 2) $
it "change minimum severity filter for a specific context" $
monadicIO $ do
lineslogged0 <- lift $ getLinesLogged
lift $ usingLoggerName "silent" $ do { logWarning "you won't see this!" }
lift $ threadDelay 0300000
lift $ usingLoggerName "verbose" $ do { logWarning "now you read this!" }
lift $ threadDelay 0300000
lineslogged1 <- lift $ getLinesLogged
let lineslogged = lineslogged1 - lineslogged0
putStrLn $ "lines logged: " ++ (show lineslogged)
assert (lineslogged == 1)

0 comments on commit d249ee9

Please sign in to comment.