@@ -3,19 +3,17 @@ module Katip.Scribes.Handle where
33-------------------------------------------------------------------------------
44import Control.Applicative as A
55import Control.Concurrent
6−import Control.Exception (finally)
6+import Control.Exception (bracket_, finally)
77import Data.Aeson
8−import Data.Aeson.Text
9−import Data.ByteString.Builder as B
10−import Data.ByteString.Lazy as BL (hPutStr)
8+import Data.Text.Lazy (toStrict)
9+import Data.Text.Lazy.Encoding (decodeUtf8)
10+import qualified Data.HashMap.Strict as HM
1111import Data.Monoid as M
1212import Data.Scientific as S
1313import Data.Text (Text)
14−import Data.Text.Lazy.Builder as TL
15−import Data.Text.Lazy.Encoding (encodeUtf8)
14+import Data.Text.Lazy.Builder
15+import Data.Text.Lazy.IO as T
1616import System.IO
17−import qualified Data.HashMap.Strict as HM
18−
1917-------------------------------------------------------------------------------
2018import Katip.Core
2119import Katip.Format.Time (formatAsLogTime)
@@ -23,20 +21,15 @@ import Katip.Format.Time (formatAsLogTime)
2321
2422
2523-------------------------------------------------------------------------------
26−brackets :: TL.Builder -> TL.Builder
24+brackets :: Builder -> Builder
2725brackets m = fromText "[" M.<> m <> fromText "]"
2826
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'
3427
3528-------------------------------------------------------------------------------
36−getKeys :: LogItem s => Verbosity -> s -> [TL.Builder]
29+getKeys :: LogItem s => Verbosity -> s -> [Builder]
3730getKeys verb a = concat (renderPair A.<$> HM.toList (payloadObject verb a))
3831 where
39− renderPair :: (Text, Value) -> [TL.Builder]
32+ renderPair :: (Text, Value) -> [Builder]
4033 renderPair (k,v) =
4134 case v of
4235 Object o -> concat [renderPair (k <> "." <> k', v') | (k', v') <- HM.toList o]
@@ -84,12 +77,14 @@ mkHandleScribeWithFormatter :: (forall a . LogItem a => ItemFormatter a)
8477 -> Verbosity
8578 -> IO Scribe
8679mkHandleScribeWithFormatter itemFormatter cs h permitF verb = do
80+ hSetBuffering h LineBuffering
8781 colorize <- case cs of
8882 ColorIfTerminal -> hIsTerminalDevice h
8983 ColorLog b -> return b
9084 lock <- newMVar ()
91− let logger i@Item{..} = withMVar lock $ \() -> do
92− BL.hPutStr h $ toLazyByteString $ itemFormatter colorize verb i
85+ let logger i@Item{..} = do
86+ bracket_ (takeMVar lock) (putMVar lock ()) $
87+ T.hPutStrLn h $ toLazyText $ itemFormatter colorize verb i
9388 return $ Scribe logger (hFlush h) permitF
9489
9590
@@ -109,11 +104,10 @@ mkFileScribe f permitF verb = do
109104-------------------------------------------------------------------------------
110105-- | A custom ItemFormatter for logging `Item`s. Takes a `Bool` indicating
111106-- whether to colorize the output, `Verbosity` of output, and an `Item` to
112−-- format. Note that formatter is responsible for appending the newline at the
113−-- end of the string if required
107+-- format.
114108--
115109-- See `bracketFormat` and `jsonFormat` for examples.
116−type ItemFormatter a = Bool -> Verbosity -> Item a -> B.Builder
110+type ItemFormatter a = Bool -> Verbosity -> Item a -> Builder
117111
118112
119113formatItem :: LogItem a => ItemFormatter a
@@ -127,10 +121,10 @@ formatItem = bracketFormat
127121-- > [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
128122-- > [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
129123bracketFormat :: LogItem a => ItemFormatter a
130−bracketFormat withColor verb Item{..} = textBuilderToBS $ appendNL $
124+bracketFormat withColor verb Item{..} =
131125 brackets nowStr <>
132126 brackets (mconcat $ map fromText $ intercalateNs _itemNamespace) <>
133− brackets (renderSeverity' _itemSeverity) <>
127+ brackets (fromText (renderSeverity' _itemSeverity)) <>
134128 brackets (fromString _itemHost) <>
135129 brackets ("PID " <> fromString (show _itemProcess)) <>
136130 brackets ("ThreadId " <> fromText (getThreadIdText _itemThread)) <>
@@ -141,7 +135,7 @@ bracketFormat withColor verb Item{..} = textBuilderToBS $ appendNL $
141135 nowStr = fromText (formatAsLogTime _itemTime)
142136 ks = map brackets $ getKeys verb _itemPayload
143137 renderSeverity' severity =
144− colorBySeverity withColor severity (fromText $ renderSeverity severity)
138+ colorBySeverity withColor severity (renderSeverity severity)
145139
146140-- | Logs items as JSON. This can be useful in circumstances where you already
147141-- have infrastructure that is expecting JSON to be logged to a standard stream
@@ -151,15 +145,15 @@ bracketFormat withColor verb Item{..} = textBuilderToBS $ appendNL $
151145-- > {"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"}
152146-- > {"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"}
153147jsonFormat :: LogItem a => ItemFormatter a
154−jsonFormat withColor verb i = textBuilderToBS $
155− appendNL $
148+jsonFormat withColor verb i =
149+ fromText $
156150 colorBySeverity withColor (_itemSeverity i) $
157− encodeToTextBuilder $ itemJson verb i
151+ toStrict $ decodeUtf8 $ encode $ itemJson verb i
158152
159153-- | Color a text message based on `Severity`. `ErrorS` and more severe errors
160154-- are colored red, `WarningS` is colored yellow, and all other messages are
161155-- rendered in the default color.
162−colorBySeverity :: Bool -> Severity -> TL.Builder -> TL.Builder
156+colorBySeverity :: Bool -> Severity -> Text -> Text
163157colorBySeverity withColor severity msg = case severity of
164158 EmergencyS -> red msg
165159 AlertS -> red msg