@@ -1,4 +1,4 @@
1−{-# LANGUAGE CPP #-}
1+{-# LANGUAGE ImplicitParams #-}
22
33module Control.Logger.Katip.Utils
44 ( katipIndexNameString
@@ -6,6 +6,8 @@ module Control.Logger.Katip.Utils
66 , LogFormat(..)
77 , registerFileScribe
88 , setLoggingCtx
9+ , ourFormatter
10+ , ourMapping
911 , getKatipLogger
1012 , logSeverityToKSeverity
1113 ) where
@@ -19,36 +21,18 @@ import Control.Logger.Internal as L
1921import Control.Logger.Katip as L
2022import Data.Aeson hiding (Error)
2123import 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
24+import qualified Data.ByteString.Builder as B.Builder
2625import qualified Data.HashMap.Strict as HM
27−#endif
2826import Data.Text (Text)
29−import qualified Data.Text.Lazy as T
3027import qualified Data.Text.Lazy.Builder as T.Builder
28+import qualified Data.Text.Lazy.Encoding as T.Lazy
3129import Katip as K hiding (logMsg)
30+import Katip.Core (ItemFunc(..))
3231import Katip.Monadic (KatipContextTState(..))
3332import qualified Katip.Scribes.Handle as K
3433import System.IO
3534
3635
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−
5236-- | How to format logs
5337data LogFormat
5438 = LogBracket -- ^ use `bracketFormat`
@@ -72,42 +56,38 @@ registerFileScribe gitRev verbosity format filePath permitF additions logenv =
7256 formatter = case format of
7357 LogBracket -> customBracketFormat additions'
7458 LogJson -> customJsonFormat gitRev additions'
75− additions'
76− = HM.fromList
77− . map (\(k, v) -> (textToKey k, String v))
78− $ additions
59+ additions' = HM.fromList . map (fmap String) $ additions
7960
8061-- | JSON formatter that adds the specified fields to the output
8162customJsonFormat
82− :: forall a. LogItem a => Text -> KeyMap Value -> ItemFormatter a
63+ :: forall a. LogItem a => Text -> HM.HashMap Text Value -> ItemFormatter a
8364customJsonFormat gitRev additions withColor verb i = (mappend "\n") $
8465 -- Here we reimplement the standard formater because we want the
8566 -- additional key/value pairs to be added at the top level.
8667 -- So we can't use the LogWithAdditions like we do for bracket format
87− T.Builder.fromText $
68+ B.Builder.lazyByteString . T.Lazy.encodeUtf8 . T.Builder.toLazyText $
8869 K.colorBySeverity withColor (_itemSeverity i) $
89− (T.toStrict . encodeToLazyText) (extend $ ourItemJson verb i)
70+ encodeToTextBuilder (extend $ applyItemFunc (ourFormatter gitRev verb) i)
9071 where
91− extend (Object o) = Object $ HM.union additions' o
72+ extend (Object o) = Object $ HM.union additions o
9273 extend v = v
93− additions' = HM.insert "rev-hash" (toJSON gitRev) additions
9474
9575-- | Bracket formatter that adds the specified fields to the output
9676customBracketFormat
97− :: forall a. LogItem a => KeyMap Value -> ItemFormatter a
77+ :: forall a. LogItem a => HM.HashMap Text Value -> ItemFormatter a
9878customBracketFormat additions withColor verb i =
9979 bracketFormat withColor verb (LogWithAdditions additions <$> i)
10080
10181-- | Log item payload that adds the specified additional key/value pairs
10282-- to the output on at verbosity levels
103−data LogWithAdditions a = LogWithAdditions (KeyMap Value) a
83+data LogWithAdditions a = LogWithAdditions (HM.HashMap Text Value) a
10484 deriving (Show)
10585
10686instance LogItem a => LogItem (LogWithAdditions a) where
10787 payloadKeys v (LogWithAdditions additions a) =
10888 case payloadKeys v a of
10989 AllKeys -> AllKeys
110− SomeKeys keys -> SomeKeys (map keyToText (HM.keys additions) ++ keys)
90+ SomeKeys keys -> SomeKeys (HM.keys additions ++ keys)
11191
11292instance ToObject a => ToObject (LogWithAdditions a) where
11393 toObject (LogWithAdditions additions a) =
@@ -120,5 +100,5 @@ setLoggingCtx
120100 -> ReaderT r m a
121101setLoggingCtx ctx' = local (part %~ go)
122102 where
123− go (Logger ctx scrbber f) =
124− Logger (ctx <> ctx') scrbber f
103+ go (Logger ctx f) =
104+ Logger (ctx <> ctx') f