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

Commit e86c409

Browse files
committed
[CBR-345] apply minSeverity as soon as possible
Signed-off-by: Alexander Diemand <[email protected]>
1 parent 4f76908 commit e86c409

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

util/src/Pos/Util/Wlog/Compatibility.hs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ import qualified Pos.Util.Log.Internal as Internal
5858
import Pos.Util.Log.LoggerConfig (LogHandler (..),
5959
LogSecurityLevel (..), LoggerConfig (..),
6060
defaultInteractiveConfiguration, defaultTestConfiguration,
61-
lcLoggerTree, lhName, ltHandlers)
61+
lcLoggerTree, lhName, ltHandlers, ltMinSeverity)
6262
import System.IO.Unsafe (unsafePerformIO)
6363

6464
import Universum
@@ -90,12 +90,17 @@ instance CanLog IO where
9090
mayEnv <- Internal.getLogEnv lh
9191
case mayEnv of
9292
Nothing -> error "logging not yet initialized. Abort."
93-
Just env -> Log.logItem' ()
94-
(K.Namespace (T.split (=='.') name))
95-
env
96-
Nothing
97-
(Internal.sev2klog severity)
98-
(K.logStr msg)
93+
Just env -> do
94+
mayConfig <- Internal.getConfig lh
95+
case mayConfig of
96+
Nothing -> error "no logging configuration. Abort."
97+
Just lc -> when (severity >= lc ^. lcLoggerTree ^. ltMinSeverity)
98+
$ Log.logItem' ()
99+
(K.Namespace (T.split (=='.') name))
100+
env
101+
Nothing
102+
(Internal.sev2klog severity)
103+
(K.logStr msg)
99104

100105
type WithLogger m = (CanLog m, HasLoggerName m)
101106

@@ -262,7 +267,9 @@ logItemS lhandler a ns loc sev cond msg = do
262267
let cfg = case maycfg of
263268
Nothing -> error "No Configuration for logging found. Abort."
264269
Just c -> c
265-
liftIO $ do
270+
let sevmin = Internal.sev2klog $ cfg ^. lcLoggerTree ^. ltMinSeverity
271+
when (sev >= sevmin)
272+
$ liftIO $ do
266273
item <- K.Item
267274
<$> pure (K._logEnvApp le)
268275
<*> pure (K._logEnvEnv le)

util/test/Test/Pos/Util/WlogSpec.hs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,13 @@ run_logging _ n n0 n1= do
6161
logWarning msg
6262
logError msg
6363
endTime <- getPOSIXTime
64-
threadDelay $ fromIntegral (5000 * n0)
64+
threadDelay $ fromIntegral (8000 * n0)
6565
let diffTime = nominalDiffTimeToMicroseconds (endTime - startTime)
6666
putStrLn $ " time for " ++ (show (n0*n1)) ++ " iterations: " ++ (show diffTime)
6767
lineslogged1 <- getLinesLogged
6868
let lineslogged = lineslogged1 - lineslogged0
6969
putStrLn $ " lines logged :" ++ (show lineslogged)
70+
threadDelay 0500000 -- wait for empty queue
7071
return (diffTime, lineslogged)
7172
where msg :: Text
7273
msg = replicate n "abcdefghijklmnopqrstuvwxyz"
@@ -102,6 +103,19 @@ spec = describe "Logging" $ do
102103
lc = lc0 & lcLoggerTree .~ newlt
103104
setupLogging "test" lc
104105

106+
modifyMaxSuccess (const 2) $ modifyMaxSize (const 2) $
107+
it "change minimum severity filter for a specific context" $
108+
monadicIO $ do
109+
lineslogged0 <- lift $ getLinesLogged
110+
lift $ usingLoggerName "silent" $ do { logWarning "you won't see this!" }
111+
lift $ threadDelay 0300000
112+
lift $ usingLoggerName "verbose" $ do { logWarning "now you read this!" }
113+
lift $ threadDelay 0300000
114+
lineslogged1 <- lift $ getLinesLogged
115+
let lineslogged = lineslogged1 - lineslogged0
116+
putStrLn $ "lines logged: " ++ (show lineslogged)
117+
assert (lineslogged == 1)
118+
105119
modifyMaxSuccess (const 1) $ modifyMaxSize (const 1) $
106120
it "demonstrate logging" $
107121
monadicIO $ lift $ someLogging
@@ -118,16 +132,3 @@ spec = describe "Logging" $ do
118132
it "lines counted as logged must be equal to how many was intended to be written" $
119133
property prop_lines
120134

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

0 commit comments

Comments
 (0)