This repository has been archived by the owner on Aug 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 629
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CBR-423] Demonstrate structured logging.
- Loading branch information
Andreas Triantafyllos
committed
Sep 17, 2018
1 parent
23bb03d
commit 455bd47
Showing
5 changed files
with
109 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
{-# LANGUAGE RecordWildCards #-} | ||
|
||
module Test.Pos.Util.LogStructuredSpec | ||
( spec) | ||
where | ||
|
||
import Universum | ||
|
||
import qualified Data.HashMap.Strict as HM | ||
import System.Directory (getCurrentDirectory, removeFile) | ||
import Test.Hspec (Spec, describe, it) | ||
import Test.Hspec.QuickCheck (modifyMaxSize, modifyMaxSuccess) | ||
import Test.QuickCheck.Monadic (monadicIO) | ||
|
||
import Pos.Util.Log (closeLogScribes) | ||
import Pos.Util.Log.Internal (FileDescription (..)) | ||
import Pos.Util.Log.LoggerConfig (BackendKind (..), LogHandler (..), | ||
LogSecurityLevel (..), LoggerConfig (..), LoggerTree (..), | ||
jsonInteractiveConfiguration) | ||
import Pos.Util.Log.Rotator (latestLogFile) | ||
import Pos.Util.Log.Structured (logDebugX, logErrorX, logInfoX, | ||
logNoticeX, logWarningX) | ||
import Pos.Util.Wlog | ||
import Pos.Util.Wlog.Compatibility (setupLogging') | ||
|
||
someLogging :: IO () | ||
someLogging = do | ||
lh <- setupLogging' loggerConfig | ||
usingLoggerName "testing" $ do | ||
testLog | ||
closeLogScribes lh | ||
where | ||
loggerConfig :: LoggerConfig | ||
loggerConfig = let | ||
_lcRotation = Nothing | ||
_lcBasePath = Nothing | ||
_lcLoggerTree = LoggerTree { | ||
_ltMinSeverity = Debug, | ||
_ltNamedSeverity = HM.empty, | ||
_ltHandlers = [ LogHandler { | ||
_lhBackend = StderrBE, | ||
_lhName = "stderr", | ||
_lhFpath = Nothing, | ||
_lhSecurityLevel = Just SecretLogLevel, | ||
_lhMinSeverity = Just Debug } | ||
, LogHandler { | ||
_lhBackend = FileJsonBE, | ||
_lhName = "json", | ||
_lhFpath = Just "node.json", | ||
_lhSecurityLevel = Just SecretLogLevel, | ||
_lhMinSeverity = Just Debug} | ||
] | ||
} | ||
in | ||
LoggerConfig{..} | ||
|
||
testLog :: (MonadIO m, WithLogger m) => m () | ||
testLog = do | ||
logDebug "debug" | ||
-- the following will be shown only in JSON file | ||
logDebugX item | ||
|
||
logInfo "info" | ||
logInfoX item | ||
|
||
logNotice "notice" | ||
logNoticeX item | ||
|
||
logWarning "warning" | ||
logWarningX item | ||
|
||
logError "error" | ||
logErrorX item | ||
where | ||
item = jsonInteractiveConfiguration Debug | ||
|
||
spec :: Spec | ||
spec = describe "Strucutured logging" $ do | ||
modifyMaxSuccess (const 1) $ modifyMaxSize (const 1) $ | ||
it "demonstrate structured logging (see node.json-{timestamp})" $ | ||
monadicIO $ do | ||
lift $ someLogging | ||
dir <- liftIO $ getCurrentDirectory | ||
mayLogFile <- liftIO $ latestLogFile $ | ||
FileDescription { prefixpath = dir, filename = "node.json"} | ||
case mayLogFile of | ||
Just logFile -> do | ||
putStrLn ("\nContents of JSON file: " <> (show logFile) :: Text) | ||
contents <- readFile logFile | ||
putStrLn contents | ||
liftIO $ removeFile logFile | ||
Nothing -> putStrLn ("JSON file NOT found:" :: Text) |