on ca029dd help sign in
3 files changed+51−34
since splitsnapshots
unifiedsplit
katip/src/Katip/Scribes/Handle.hs+42−26 katip/test/Katip/Tests/Scribes/Handle.hs+6−8 stack.yaml+3
katip/src/Katip/Scribes/Handle.hs+42−26
@@ -3,17 +3,19 @@ module Katip.Scribes.Handle where
33-------------------------------------------------------------------------------
44import Control.Applicative as A
55import Control.Concurrent
6−import Control.Exception (bracket_, finally)
6+import Control.Exception (finally)
77import Data.Aeson
8−import Data.Text.Lazy (toStrict)
9−import Data.Text.Lazy.Encoding (decodeUtf8)
10−import qualified Data.HashMap.Strict as HM
8+import Data.Aeson.Text
9+import Data.ByteString.Builder as B
10+import Data.ByteString.Lazy as BL (hPutStr)
1111import Data.Monoid as M
1212import Data.Scientific as S
1313import Data.Text (Text)
14−import Data.Text.Lazy.Builder
15−import Data.Text.Lazy.IO as T
14+import Data.Text.Lazy.Builder as TL
15+import Data.Text.Lazy.Encoding (encodeUtf8)
1616import System.IO
17+import qualified Data.HashMap.Strict as HM
18+
1719-------------------------------------------------------------------------------
1820import Katip.Core
1921import Katip.Format.Time (formatAsLogTime)
@@ -21,15 +23,20 @@ import Katip.Format.Time (formatAsLogTime)
2123
2224
2325-------------------------------------------------------------------------------
24−brackets :: Builder -> Builder
26+brackets :: TL.Builder -> TL.Builder
2527brackets m = fromText "[" M.<> m <> fromText "]"
2628
29+textBuilderToBS :: TL.Builder -> B.Builder
30+textBuilderToBS = B.lazyByteString . encodeUtf8 . toLazyText
31+
32+appendNL :: TL.Builder -> TL.Builder
33+appendNL b = b <> TL.singleton '\n'
2734
2835-------------------------------------------------------------------------------
29−getKeys :: LogItem s => Verbosity -> s -> [Builder]
36+getKeys :: LogItem s => Verbosity -> s -> [TL.Builder]
3037getKeys verb a = concat (renderPair A.<$> HM.toList (payloadObject verb a))
3138 where
32− renderPair :: (Text, Value) -> [Builder]
39+ renderPair :: (Text, Value) -> [TL.Builder]
3340 renderPair (k,v) =
3441 case v of
3542 Object o -> concat [renderPair (k <> "." <> k', v') | (k', v') <- HM.toList o]
@@ -65,6 +72,19 @@ data ColorStrategy
6572mkHandleScribe :: ColorStrategy -> Handle -> PermitFunc -> Verbosity -> IO Scribe
6673mkHandleScribe = mkHandleScribeWithFormatter bracketFormat
6774
75+
76+mkFileScribeWithFormatter :: (forall a . LogItem a => ItemFormatter a)
77+ -> ColorStrategy
78+ -> FilePath
79+ -> PermitFunc
80+ -> Verbosity
81+ -> IO Scribe
82+mkFileScribeWithFormatter formatter color f permitF verb = do
83+ h <- openBinaryFile f AppendMode
84+ Scribe logger finalizer permit <- mkHandleScribeWithFormatter formatter color h permitF verb
85+ return (Scribe logger (finalizer `finally` hClose h) permit)
86+
87+
6888-- | Logs to a file handle such as stdout, stderr, or a file. Takes a custom
6989-- `ItemFormatter` that can be used to format `Item` as needed.
7090--
@@ -77,14 +97,12 @@ mkHandleScribeWithFormatter :: (forall a . LogItem a => ItemFormatter a)
7797 -> Verbosity
7898 -> IO Scribe
7999mkHandleScribeWithFormatter itemFormatter cs h permitF verb = do
80− hSetBuffering h LineBuffering
81100 colorize <- case cs of
82101 ColorIfTerminal -> hIsTerminalDevice h
83102 ColorLog b -> return b
84103 lock <- newMVar ()
85− let logger i@Item{..} = do
86− bracket_ (takeMVar lock) (putMVar lock ()) $
87− T.hPutStrLn h $ toLazyText $ itemFormatter colorize verb i
104+ let logger i@Item{..} = withMVar lock $ \() -> do
105+ BL.hPutStr h $ toLazyByteString $ itemFormatter colorize verb i
88106 return $ Scribe logger (hFlush h) permitF
89107
90108
@@ -95,19 +113,17 @@ mkHandleScribeWithFormatter itemFormatter cs h permitF verb = do
95113-- 'closeScribe'/'closeScribes'. Does not do log coloring. Sets handle
96114-- to 'LineBuffering' mode.
97115mkFileScribe :: FilePath -> PermitFunc -> Verbosity -> IO Scribe
98−mkFileScribe f permitF verb = do
99− h <- openFile f AppendMode
100− Scribe logger finalizer permit <- mkHandleScribe (ColorLog False) h permitF verb
101− return (Scribe logger (finalizer `finally` hClose h) permit)
116+mkFileScribe = mkFileScribeWithFormatter bracketFormat (ColorLog False)
102117
103118
104119-------------------------------------------------------------------------------
105120-- | A custom ItemFormatter for logging `Item`s. Takes a `Bool` indicating
106121-- whether to colorize the output, `Verbosity` of output, and an `Item` to
107−-- format.
122+-- format. Note that formatter is responsible for appending the newline at the
123+-- end of the string if required
108124--
109125-- See `bracketFormat` and `jsonFormat` for examples.
110−type ItemFormatter a = Bool -> Verbosity -> Item a -> Builder
126+type ItemFormatter a = Bool -> Verbosity -> Item a -> B.Builder
111127
112128
113129formatItem :: LogItem a => ItemFormatter a
@@ -121,10 +137,10 @@ formatItem = bracketFormat
121137-- > [2016-05-11 21:01:15][MyApp.confrabulation][Debug][myhost.example.com][PID 1724][ThreadId 1154][confrab_factor:42.0][main:Helpers.Logging Helpers/Logging.hs:41:9] Confrabulating widgets, with extra namespace and context
122138-- > [2016-05-11 21:01:15][MyApp][Info][myhost.example.com][PID 1724][ThreadId 1154][main:Helpers.Logging Helpers/Logging.hs:43:7] Namespace and context are back to normal
123139bracketFormat :: LogItem a => ItemFormatter a
124−bracketFormat withColor verb Item{..} =
140+bracketFormat withColor verb Item{..} = textBuilderToBS $ appendNL $
125141 brackets nowStr <>
126142 brackets (mconcat $ map fromText $ intercalateNs _itemNamespace) <>
127− brackets (fromText (renderSeverity' _itemSeverity)) <>
143+ brackets (renderSeverity' _itemSeverity) <>
128144 brackets (fromString _itemHost) <>
129145 brackets ("PID " <> fromString (show _itemProcess)) <>
130146 brackets ("ThreadId " <> fromText (getThreadIdText _itemThread)) <>
@@ -135,7 +151,7 @@ bracketFormat withColor verb Item{..} =
135151 nowStr = fromText (formatAsLogTime _itemTime)
136152 ks = map brackets $ getKeys verb _itemPayload
137153 renderSeverity' severity =
138− colorBySeverity withColor severity (renderSeverity severity)
154+ colorBySeverity withColor severity (fromText $ renderSeverity severity)
139155
140156-- | Logs items as JSON. This can be useful in circumstances where you already
141157-- have infrastructure that is expecting JSON to be logged to a standard stream
@@ -145,15 +161,15 @@ bracketFormat withColor verb Item{..} =
145161-- > {"at":"2018-10-02T21:50:30.4523848Z","env":"production","ns":["MyApp","confrabulation"],"data":{"confrab_factor":42},"app":["MyApp"],"msg":"Confrabulating widgets, with extra namespace and context","pid":"10456","loc":{"loc_col":11,"loc_pkg":"main","loc_mod":"Helpers.Logging","loc_fn":"Helpers\\Logging.hs","loc_ln":53},"host":"myhost.example.com","sev":"Debug","thread":"ThreadId 139"}
146162-- > {"at":"2018-10-02T21:50:30.4523848Z","env":"production","ns":["MyApp"],"data":{},"app":["MyApp"],"msg":"Namespace and context are back to normal","pid":"10456","loc":{"loc_col":9,"loc_pkg":"main","loc_mod":"Helpers.Logging","loc_fn":"Helpers\\Logging.hs","loc_ln":55},"host":"myhost.example.com","sev":"Info","thread":"ThreadId 139"}
147163jsonFormat :: LogItem a => ItemFormatter a
148−jsonFormat withColor verb i =
149− fromText $
164+jsonFormat withColor verb i = textBuilderToBS $
165+ appendNL $
150166 colorBySeverity withColor (_itemSeverity i) $
151− toStrict $ decodeUtf8 $ encode $ itemJson verb i
167+ encodeToTextBuilder $ itemJson verb i
152168
153169-- | Color a text message based on `Severity`. `ErrorS` and more severe errors
154170-- are colored red, `WarningS` is colored yellow, and all other messages are
155171-- rendered in the default color.
156−colorBySeverity :: Bool -> Severity -> Text -> Text
172+colorBySeverity :: Bool -> Severity -> TL.Builder -> TL.Builder
157173colorBySeverity withColor severity msg = case severity of
158174 EmergencyS -> red msg
159175 AlertS -> red msg
katip/test/Katip/Tests/Scribes/Handle.hs+6−8
@@ -1,5 +1,5 @@
11{-# LANGUAGE OverloadedStrings #-}
2−{-# LANGUAGE RankNTypes #-}
2+{-# LANGUAGE RankNTypes #-}
33module Katip.Tests.Scribes.Handle
44 ( tests
55 ) where
@@ -7,12 +7,11 @@ module Katip.Tests.Scribes.Handle
77-------------------------------------------------------------------------------
88import Control.Monad
99import Data.Aeson
10−import qualified Data.ByteString.Char8 as B
10+import Data.ByteString.Builder as B
1111import qualified Data.ByteString.Lazy as BL
1212import Data.Monoid as M
1313import Data.Text (Text)
1414import qualified Data.Text as T
15−import qualified Data.Text.Encoding as T
1615import qualified Data.Text.Lazy as LT
1716import qualified Data.Text.Lazy.Builder as LT
1817import Data.Time
@@ -245,14 +244,13 @@ writeJsonLog = writeFormattedLog jsonFormat
245244
246245writeFormattedLog :: (forall a . LogItem a => ItemFormatter a) -> (FilePath, Handle) -> IO (BL.ByteString)
247246writeFormattedLog format (path, h) = do
248− mapM_ (put . formatOne) genItems
249− mapM_ (put . formatOne) genTypedItems
247+ mapM_ put genItems
248+ mapM_ put genTypedItems
250249 hClose h
251250 BL.readFile path
252251 where
253− formatOne :: LogItem a => Item a -> Text
254− formatOne = LT.toStrict . LT.toLazyText . format False V3
255− put = B.hPutStrLn h . T.encodeUtf8
252+ put :: (LogItem a) => Item a -> IO ()
253+ put = BL.hPutStr h . toLazyByteString . format False V3
256254
257255setupTempFile :: IO (FilePath, Handle)
258256setupTempFile = do
stack.yaml+3
@@ -1,3 +1,6 @@
1+nix:
2+ packages:
3+ - zlib
14resolver: lts-12.26
25packages:
36 - katip