@@ -1,4 +1,4 @@
1−{-# LANGUAGE CPP #-}
1+{-# LANGUAGE ImplicitParams #-}
22
33module Control.Logger.Katip.Utils
44 ( katipIndexNameString
@@ -6,6 +6,7 @@ module Control.Logger.Katip.Utils
66 , LogFormat(..)
77 , registerFileScribe
88 , setLoggingCtx
9+ , ourFormatter
910 , getKatipLogger
1011 , logSeverityToKSeverity
1112 ) where
@@ -19,36 +20,18 @@ import Control.Logger.Internal as L
1920import Control.Logger.Katip as L
2021import Data.Aeson hiding (Error)
2122import Data.Aeson.Text
22−#if MIN_VERSION_aeson(2,0,0)
23−import qualified Data.Aeson.KeyMap as HM
24−import qualified Data.Aeson.Key as HM
25−#else
23+import qualified Data.ByteString.Builder as B.Builder
2624import qualified Data.HashMap.Strict as HM
27−#endif
2825import Data.Text (Text)
29−import qualified Data.Text.Lazy as T
3026import qualified Data.Text.Lazy.Builder as T.Builder
27+import qualified Data.Text.Lazy.Encoding as T.Lazy
3128import Katip as K hiding (logMsg)
29+import Katip.Core (ItemFunc(..))
3230import Katip.Monadic (KatipContextTState(..))
3331import qualified Katip.Scribes.Handle as K
3432import System.IO
3533
3634
37−#if MIN_VERSION_aeson(2,0,0)
38−type KeyMap v = HM.KeyMap v
39−textToKey :: Text -> Key
40−textToKey = HM.fromText
41−keyToText :: Key -> Text
42−keyToText = HM.toText
43−#else
44−type KeyMap v = HM.HashMap Text v
45−type Key = Text
46−textToKey :: Text -> Key
47−textToKey = id
48−keyToText :: Key -> Text
49−keyToText = id
50−#endif
51−
5235-- | How to format logs
5336data LogFormat
5437 = LogBracket -- ^ use `bracketFormat`
@@ -72,42 +55,38 @@ registerFileScribe gitRev verbosity format filePath permitF additions logenv =
7255 formatter = case format of
7356 LogBracket -> customBracketFormat additions'
7457 LogJson -> customJsonFormat gitRev additions'
75− additions'
76− = HM.fromList
77− . map (\(k, v) -> (textToKey k, String v))
78− $ additions
58+ additions' = HM.fromList . map (fmap String) $ additions
7959
8060-- | JSON formatter that adds the specified fields to the output
8161customJsonFormat
82− :: forall a. LogItem a => Text -> KeyMap Value -> ItemFormatter a
62+ :: forall a. LogItem a => Text -> HM.HashMap Text Value -> ItemFormatter a
8363customJsonFormat gitRev additions withColor verb i = (mappend "\n") $
8464 -- Here we reimplement the standard formater because we want the
8565 -- additional key/value pairs to be added at the top level.
8666 -- So we can't use the LogWithAdditions like we do for bracket format
87− T.Builder.fromText $
67+ B.Builder.lazyByteString . T.Lazy.encodeUtf8 . T.Builder.toLazyText $
8868 K.colorBySeverity withColor (_itemSeverity i) $
89− (T.toStrict . encodeToLazyText) (extend $ ourItemJson verb i)
69+ encodeToTextBuilder (extend $ applyItemFunc (ourFormatter gitRev verb) i)
9070 where
91− extend (Object o) = Object $ HM.union additions' o
71+ extend (Object o) = Object $ HM.union additions o
9272 extend v = v
93− additions' = HM.insert "rev-hash" (toJSON gitRev) additions
9473
9574-- | Bracket formatter that adds the specified fields to the output
9675customBracketFormat
97− :: forall a. LogItem a => KeyMap Value -> ItemFormatter a
76+ :: forall a. LogItem a => HM.HashMap Text Value -> ItemFormatter a
9877customBracketFormat additions withColor verb i =
9978 bracketFormat withColor verb (LogWithAdditions additions <$> i)
10079
10180-- | Log item payload that adds the specified additional key/value pairs
10281-- to the output on at verbosity levels
103−data LogWithAdditions a = LogWithAdditions (KeyMap Value) a
82+data LogWithAdditions a = LogWithAdditions (HM.HashMap Text Value) a
10483 deriving (Show)
10584
10685instance LogItem a => LogItem (LogWithAdditions a) where
10786 payloadKeys v (LogWithAdditions additions a) =
10887 case payloadKeys v a of
10988 AllKeys -> AllKeys
110− SomeKeys keys -> SomeKeys (map keyToText (HM.keys additions) ++ keys)
89+ SomeKeys keys -> SomeKeys (HM.keys additions ++ keys)
11190
11291instance ToObject a => ToObject (LogWithAdditions a) where
11392 toObject (LogWithAdditions additions a) =
@@ -120,5 +99,5 @@ setLoggingCtx
12099 -> ReaderT r m a
121100setLoggingCtx ctx' = local (part %~ go)
122101 where
123− go (Logger ctx scrbber f) =
124− Logger (ctx <> ctx') scrbber f
102+ go (Logger ctx f) =
103+ Logger (ctx <> ctx') f