on 686ae62 help sign in
4 files changed+53−34
since splitsnapshots
unifiedsplit
katip/src/Katip.hs+1 katip/src/Katip/Scribes/Handle.hs+43−26 katip/test/Katip/Tests/Scribes/Handle.hs+6−8 stack.yaml+3
katip/src/Katip.hs+1
@@ -181,6 +181,7 @@ module Katip
181181 , mkHandleScribe
182182 , mkHandleScribeWithFormatter
183183 , mkFileScribe
184+ , mkFileScribeWithFormatter
184185 , ColorStrategy (..)
185186 , ItemFormatter
186187 , bracketFormat
katip/src/Katip/Scribes/Handle.hs+43−26
@@ -3,17 +3,20 @@ 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)
11+import Data.ByteString.Lazy.Char8 as BL (singleton)
1112import Data.Monoid as M
1213import Data.Scientific as S
1314import Data.Text (Text)
14−import Data.Text.Lazy.Builder
15−import Data.Text.Lazy.IO as T
15+import Data.Text.Lazy.Builder as TL
16+import Data.Text.Lazy.Encoding (encodeUtf8)
1617import System.IO
18+import qualified Data.HashMap.Strict as HM
19+
1720-------------------------------------------------------------------------------
1821import Katip.Core
1922import Katip.Format.Time (formatAsLogTime)
@@ -21,15 +24,20 @@ import Katip.Format.Time (formatAsLogTime)
2124
2225
2326-------------------------------------------------------------------------------
24−brackets :: Builder -> Builder
27+brackets :: TL.Builder -> TL.Builder
2528brackets m = fromText "[" M.<> m <> fromText "]"
2629
30+textBuilderToBS :: TL.Builder -> B.Builder
31+textBuilderToBS = B.lazyByteString . encodeUtf8 . toLazyText
32+
33+appendNL :: B.Builder -> B.Builder
34+appendNL b = b <> B.lazyByteString (BL.singleton '\n')
2735
2836-------------------------------------------------------------------------------
29−getKeys :: LogItem s => Verbosity -> s -> [Builder]
37+getKeys :: LogItem s => Verbosity -> s -> [TL.Builder]
3038getKeys verb a = concat (renderPair A.<$> HM.toList (payloadObject verb a))
3139 where
32− renderPair :: (Text, Value) -> [Builder]
40+ renderPair :: (Text, Value) -> [TL.Builder]
3341 renderPair (k,v) =
3442 case v of
3543 Object o -> concat [renderPair (k <> "." <> k', v') | (k', v') <- HM.toList o]
@@ -65,6 +73,19 @@ data ColorStrategy
6573mkHandleScribe :: ColorStrategy -> Handle -> PermitFunc -> Verbosity -> IO Scribe
6674mkHandleScribe = mkHandleScribeWithFormatter bracketFormat
6775
76+
77+mkFileScribeWithFormatter :: (forall a . LogItem a => ItemFormatter a)
78+ -> ColorStrategy
79+ -> FilePath
80+ -> PermitFunc
81+ -> Verbosity
82+ -> IO Scribe
83+mkFileScribeWithFormatter formatter color f permitF verb = do
84+ h <- openBinaryFile f AppendMode
85+ Scribe logger finalizer permit <- mkHandleScribeWithFormatter formatter color h permitF verb
86+ return (Scribe logger (finalizer `finally` hClose h) permit)
87+
88+
6889-- | Logs to a file handle such as stdout, stderr, or a file. Takes a custom
6990-- `ItemFormatter` that can be used to format `Item` as needed.
7091--
@@ -77,14 +98,12 @@ mkHandleScribeWithFormatter :: (forall a . LogItem a => ItemFormatter a)
7798 -> Verbosity
7899 -> IO Scribe
79100mkHandleScribeWithFormatter itemFormatter cs h permitF verb = do
80− hSetBuffering h LineBuffering
81101 colorize <- case cs of
82102 ColorIfTerminal -> hIsTerminalDevice h
83103 ColorLog b -> return b
84104 lock <- newMVar ()
85− let logger i@Item{..} = do
86− bracket_ (takeMVar lock) (putMVar lock ()) $
87− T.hPutStrLn h $ toLazyText $ itemFormatter colorize verb i
105+ let logger i@Item{..} = withMVar lock $ \() -> do
106+ BL.hPutStr h $ toLazyByteString $ itemFormatter colorize verb i
88107 return $ Scribe logger (hFlush h) permitF
89108
90109
@@ -95,19 +114,17 @@ mkHandleScribeWithFormatter itemFormatter cs h permitF verb = do
95114-- 'closeScribe'/'closeScribes'. Does not do log coloring. Sets handle
96115-- to 'LineBuffering' mode.
97116mkFileScribe :: 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)
117+mkFileScribe = mkFileScribeWithFormatter bracketFormat (ColorLog False)
102118
103119
104120-------------------------------------------------------------------------------
105121-- | A custom ItemFormatter for logging `Item`s. Takes a `Bool` indicating
106122-- whether to colorize the output, `Verbosity` of output, and an `Item` to
107−-- format.
123+-- format. Note that formatter is responsible for appending the newline at the
124+-- end of the string if required
108125--
109126-- See `bracketFormat` and `jsonFormat` for examples.
110−type ItemFormatter a = Bool -> Verbosity -> Item a -> Builder
127+type ItemFormatter a = Bool -> Verbosity -> Item a -> B.Builder
111128
112129
113130formatItem :: LogItem a => ItemFormatter a
@@ -121,10 +138,10 @@ formatItem = bracketFormat
121138-- > [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
122139-- > [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
123140bracketFormat :: LogItem a => ItemFormatter a
124−bracketFormat withColor verb Item{..} =
141+bracketFormat withColor verb Item{..} = appendNL $ textBuilderToBS $
125142 brackets nowStr <>
126143 brackets (mconcat $ map fromText $ intercalateNs _itemNamespace) <>
127− brackets (fromText (renderSeverity' _itemSeverity)) <>
144+ brackets (renderSeverity' _itemSeverity) <>
128145 brackets (fromString _itemHost) <>
129146 brackets ("PID " <> fromString (show _itemProcess)) <>
130147 brackets ("ThreadId " <> fromText (getThreadIdText _itemThread)) <>
@@ -135,7 +152,7 @@ bracketFormat withColor verb Item{..} =
135152 nowStr = fromText (formatAsLogTime _itemTime)
136153 ks = map brackets $ getKeys verb _itemPayload
137154 renderSeverity' severity =
138− colorBySeverity withColor severity (renderSeverity severity)
155+ colorBySeverity withColor severity (fromText $ renderSeverity severity)
139156
140157-- | Logs items as JSON. This can be useful in circumstances where you already
141158-- have infrastructure that is expecting JSON to be logged to a standard stream
@@ -145,15 +162,15 @@ bracketFormat withColor verb Item{..} =
145162-- > {"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"}
146163-- > {"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"}
147164jsonFormat :: LogItem a => ItemFormatter a
148−jsonFormat withColor verb i =
149− fromText $
165+jsonFormat withColor verb i = appendNL $
166+ textBuilderToBS $
150167 colorBySeverity withColor (_itemSeverity i) $
151− toStrict $ decodeUtf8 $ encode $ itemJson verb i
168+ encodeToTextBuilder $ itemJson verb i
152169
153170-- | Color a text message based on `Severity`. `ErrorS` and more severe errors
154171-- are colored red, `WarningS` is colored yellow, and all other messages are
155172-- rendered in the default color.
156−colorBySeverity :: Bool -> Severity -> Text -> Text
173+colorBySeverity :: Bool -> Severity -> TL.Builder -> TL.Builder
157174colorBySeverity withColor severity msg = case severity of
158175 EmergencyS -> red msg
159176 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