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

Commit 5740948

Browse files
committed
[CBR-345] printing log items compatible with current format
Signed-off-by: Alexander Diemand <[email protected]>
1 parent 35fd1b8 commit 5740948

File tree

1 file changed

+32
-30
lines changed

1 file changed

+32
-30
lines changed

util/src/Pos/Util/Log/Scribes.hs

+32-30
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,20 @@ module Pos.Util.Log.Scribes
1111
, mkJsonFileScribe
1212
) where
1313

14-
import Universum hiding (fromString)
14+
import Universum
1515

1616
import Control.AutoUpdate (UpdateSettings (..), defaultUpdateSettings,
1717
mkAutoUpdate)
1818
import Control.Concurrent.MVar (modifyMVar_)
1919

2020
import Data.Aeson.Text (encodeToLazyText)
21+
import qualified Data.Text as T
2122
import Data.Text.Lazy.Builder
2223
import qualified Data.Text.Lazy.IO as TIO
2324
import Data.Time (diffUTCTime)
25+
import Data.Time.Format (defaultTimeLocale, formatTime)
2426
import Katip.Core
25-
import Katip.Format.Time (formatAsIso8601)
26-
import Katip.Scribes.Handle (brackets, getKeys)
27+
import Katip.Scribes.Handle (brackets)
2728

2829
import qualified Pos.Util.Log.Internal as Internal
2930
import Pos.Util.Log.LoggerConfig (RotationParameters (..))
@@ -49,7 +50,7 @@ mkTextFileScribe :: RotationParameters -> Internal.FileDescription -> Bool -> Se
4950
mkTextFileScribe rot fdesc colorize s v = do
5051
mkFileScribe rot fdesc formatter colorize s v
5152
where
52-
formatter :: LogItem a => Handle -> Bool -> Verbosity -> Item a -> IO Int
53+
formatter :: {-LogItem a =>-} Handle -> Bool -> Verbosity -> Item a -> IO Int
5354
formatter hdl colorize' v' item = do
5455
let tmsg = toLazyText $ formatItem colorize' v' item
5556
TIO.hPutStrLn hdl tmsg
@@ -100,7 +101,7 @@ mkFileScribeH :: Handle -> Bool -> Severity -> Verbosity -> IO Scribe
100101
mkFileScribeH h colorize s v = do
101102
hSetBuffering h LineBuffering
102103
locklocal <- newMVar ()
103-
let logger :: forall a. LogItem a => Item a -> IO ()
104+
let logger :: forall a. {-LogItem a =>-} Item a -> IO ()
104105
logger item = when (permitItem s item) $
105106
bracket_ (takeMVar locklocal) (putMVar locklocal ()) $
106107
TIO.hPutStrLn h $! toLazyText $ formatItem colorize v item
@@ -120,42 +121,43 @@ mkDevNullScribe lh s v = do
120121
h <- openFile "/dev/null" WriteMode
121122
let colorize = False
122123
hSetBuffering h LineBuffering
123-
let logger :: forall a. LogItem a => Item a -> IO ()
124+
let logger :: forall a. {-LogItem a =>-} Item a -> IO ()
124125
logger item = when (permitItem s item) $
125126
Internal.incrementLinesLogged lh
126127
>> (TIO.hPutStrLn h $! toLazyText $ formatItem colorize v item)
127128
pure $ Scribe logger (hClose h)
128129

129130

130131
-- | format a @LogItem@ with subsecond precision (ISO 8601)
131-
formatItem :: LogItem a => Bool -> Verbosity -> Item a -> Builder
132-
formatItem withColor verb Item{..} =
133-
brackets nowStr <>
134-
brackets (mconcat $ map fromText $ intercalateNs _itemNamespace) <>
135-
brackets (fromText (renderSeverity' _itemSeverity)) <>
136-
brackets (fromString _itemHost) <>
137-
brackets (fromString (show _itemProcess)) <>
138-
brackets (fromText (getThreadIdText _itemThread)) <>
139-
mconcat ks <>
140-
maybe mempty (brackets . fromString . locationToString) _itemLoc <>
132+
formatItem :: Bool -> Verbosity -> Item a -> Builder
133+
formatItem withColor _verb Item{..} =
134+
fromText header <>
135+
fromText " " <>
136+
brackets (fromText timestamp) <>
141137
fromText " " <>
142138
unLogStr _itemMessage
143139
where
144-
nowStr = fromText (formatAsIso8601 _itemTime)
145-
ks = map brackets $ getKeys verb _itemPayload
146-
renderSeverity' s = case s of
147-
EmergencyS -> red $ renderSeverity s
148-
AlertS -> red $ renderSeverity s
149-
CriticalS -> red $ renderSeverity s
150-
ErrorS -> red $ renderSeverity s
151-
NoticeS -> magenta $ renderSeverity s
152-
WarningS -> yellow $ renderSeverity s
153-
InfoS -> blue $ renderSeverity s
154-
_ -> renderSeverity s
140+
header = colorBySeverity _itemSeverity $
141+
"[" <> mconcat namedcontext <> ":" <> severity <> ":" <> threadid <> "]"
142+
namedcontext = intercalateNs _itemNamespace
143+
severity = renderSeverity _itemSeverity
144+
threadid = getThreadIdText _itemThread
145+
timestamp = T.pack $ formatTime defaultTimeLocale tsformat _itemTime
146+
tsformat :: String
147+
tsformat = "%F %T%2Q %Z"
148+
colorBySeverity s m = case s of
149+
EmergencyS -> red m
150+
AlertS -> red m
151+
CriticalS -> red m
152+
ErrorS -> red m
153+
NoticeS -> magenta m
154+
WarningS -> yellow m
155+
InfoS -> blue m
156+
_ -> m
155157
red = colorize "31"
156158
yellow = colorize "33"
157159
magenta = colorize "35"
158160
blue = colorize "34"
159-
colorize c s
160-
| withColor = "\ESC["<> c <> "m" <> s <> "\ESC[0m"
161-
| otherwise = s
161+
colorize c m
162+
| withColor = "\ESC["<> c <> "m" <> m <> "\ESC[0m"
163+
| otherwise = m

0 commit comments

Comments
 (0)