@@ -3,20 +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)
11−import Data.ByteString.Lazy.Char8 as BL (singleton)
8+import Data.Text.Lazy (toStrict)
9+import Data.Text.Lazy.Encoding (decodeUtf8)
10+import qualified Data.HashMap.Strict as HM
1211import Data.Monoid as M
1312import Data.Scientific as S
1413import Data.Text (Text)
15−import Data.Text.Lazy.Builder as TL
16−import Data.Text.Lazy.Encoding (encodeUtf8)
14+import Data.Text.Lazy.Builder
15+import Data.Text.Lazy.IO as T
1716import System.IO
18−import qualified Data.HashMap.Strict as HM
19−
2017-------------------------------------------------------------------------------
2118import Katip.Core
2219import Katip.Format.Time (formatAsLogTime)
@@ -24,20 +21,15 @@ import Katip.Format.Time (formatAsLogTime)
2421
2522
2623-------------------------------------------------------------------------------
27−brackets :: TL.Builder -> TL.Builder
24+brackets :: Builder -> Builder
2825brackets m = fromText "[" M.<> m <> fromText "]"
2926
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')
3527
3628-------------------------------------------------------------------------------
37−getKeys :: LogItem s => Verbosity -> s -> [TL.Builder]
29+getKeys :: LogItem s => Verbosity -> s -> [Builder]
3830getKeys verb a = concat (renderPair A.<$> HM.toList (payloadObject verb a))
3931 where
40− renderPair :: (Text, Value) -> [TL.Builder]
32+ renderPair :: (Text, Value) -> [Builder]
4133 renderPair (k,v) =
4234 case v of
4335 Object o -> concat [renderPair (k <> "." <> k', v') | (k', v') <- HM.toList o]
@@ -73,19 +65,6 @@ data ColorStrategy
7365mkHandleScribe :: ColorStrategy -> Handle -> PermitFunc -> Verbosity -> IO Scribe
7466mkHandleScribe = mkHandleScribeWithFormatter bracketFormat
7567
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−
8968-- | Logs to a file handle such as stdout, stderr, or a file. Takes a custom
9069-- `ItemFormatter` that can be used to format `Item` as needed.
9170--
@@ -98,12 +77,14 @@ mkHandleScribeWithFormatter :: (forall a . LogItem a => ItemFormatter a)
9877 -> Verbosity
9978 -> IO Scribe
10079mkHandleScribeWithFormatter itemFormatter cs h permitF verb = do
80+ hSetBuffering h LineBuffering
10181 colorize <- case cs of
10282 ColorIfTerminal -> hIsTerminalDevice h
10383 ColorLog b -> return b
10484 lock <- newMVar ()
105− let logger i@Item{..} = withMVar lock $ \() -> do
106− 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
10788 return $ Scribe logger (hFlush h) permitF
10889
10990
@@ -114,17 +95,19 @@ mkHandleScribeWithFormatter itemFormatter cs h permitF verb = do
11495-- 'closeScribe'/'closeScribes'. Does not do log coloring. Sets handle
11596-- to 'LineBuffering' mode.
11697mkFileScribe :: FilePath -> PermitFunc -> Verbosity -> IO Scribe
117−mkFileScribe = mkFileScribeWithFormatter bracketFormat (ColorLog False)
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)
118102
119103
120104-------------------------------------------------------------------------------
121105-- | A custom ItemFormatter for logging `Item`s. Takes a `Bool` indicating
122106-- whether to colorize the output, `Verbosity` of output, and an `Item` to
123−-- format. Note that formatter is responsible for appending the newline at the
124−-- end of the string if required
107+-- format.
125108--
126109-- See `bracketFormat` and `jsonFormat` for examples.
127−type ItemFormatter a = Bool -> Verbosity -> Item a -> B.Builder
110+type ItemFormatter a = Bool -> Verbosity -> Item a -> Builder
128111
129112
130113formatItem :: LogItem a => ItemFormatter a
@@ -138,10 +121,10 @@ formatItem = bracketFormat
138121-- > [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
139122-- > [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
140123bracketFormat :: LogItem a => ItemFormatter a
141−bracketFormat withColor verb Item{..} = appendNL $ textBuilderToBS $
124+bracketFormat withColor verb Item{..} =
142125 brackets nowStr <>
143126 brackets (mconcat $ map fromText $ intercalateNs _itemNamespace) <>
144− brackets (renderSeverity' _itemSeverity) <>
127+ brackets (fromText (renderSeverity' _itemSeverity)) <>
145128 brackets (fromString _itemHost) <>
146129 brackets ("PID " <> fromString (show _itemProcess)) <>
147130 brackets ("ThreadId " <> fromText (getThreadIdText _itemThread)) <>
@@ -152,7 +135,7 @@ bracketFormat withColor verb Item{..} = appendNL $ textBuilderToBS $
152135 nowStr = fromText (formatAsLogTime _itemTime)
153136 ks = map brackets $ getKeys verb _itemPayload
154137 renderSeverity' severity =
155− colorBySeverity withColor severity (fromText $ renderSeverity severity)
138+ colorBySeverity withColor severity (renderSeverity severity)
156139
157140-- | Logs items as JSON. This can be useful in circumstances where you already
158141-- have infrastructure that is expecting JSON to be logged to a standard stream
@@ -162,15 +145,15 @@ bracketFormat withColor verb Item{..} = appendNL $ textBuilderToBS $
162145-- > {"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"}
163146-- > {"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"}
164147jsonFormat :: LogItem a => ItemFormatter a
165−jsonFormat withColor verb i = appendNL $
166− textBuilderToBS $
148+jsonFormat withColor verb i =
149+ fromText $
167150 colorBySeverity withColor (_itemSeverity i) $
168− encodeToTextBuilder $ itemJson verb i
151+ toStrict $ decodeUtf8 $ encode $ itemJson verb i
169152
170153-- | Color a text message based on `Severity`. `ErrorS` and more severe errors
171154-- are colored red, `WarningS` is colored yellow, and all other messages are
172155-- rendered in the default color.
173−colorBySeverity :: Bool -> Severity -> TL.Builder -> TL.Builder
156+colorBySeverity :: Bool -> Severity -> Text -> Text
174157colorBySeverity withColor severity msg = case severity of
175158 EmergencyS -> red msg
176159 AlertS -> red msg