@@ -1,4 +1,4 @@
1−{-# LANGUAGE ImplicitParams #-}
1+{-# LANGUAGE CPP #-}
22
33module Control.Logger.Katip.Utils
44 ( katipIndexNameString
@@ -6,7 +6,6 @@ module Control.Logger.Katip.Utils
66 , LogFormat(..)
77 , registerFileScribe
88 , setLoggingCtx
9− , ourFormatter
109 , getKatipLogger
1110 , logSeverityToKSeverity
1211 ) where
@@ -20,18 +19,36 @@ import Control.Logger.Internal as L
2019import Control.Logger.Katip as L
2120import Data.Aeson hiding (Error)
2221import Data.Aeson.Text
23−import qualified Data.ByteString.Builder as B.Builder
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
2426import qualified Data.HashMap.Strict as HM
27+#endif
2528import Data.Text (Text)
29+import qualified Data.Text.Lazy as T
2630import qualified Data.Text.Lazy.Builder as T.Builder
27−import qualified Data.Text.Lazy.Encoding as T.Lazy
2831import Katip as K hiding (logMsg)
29−import Katip.Core (ItemFunc(..))
3032import Katip.Monadic (KatipContextTState(..))
3133import qualified Katip.Scribes.Handle as K
3234import System.IO
3335
3436
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+
3552-- | How to format logs
3653data LogFormat
3754 = LogBracket -- ^ use `bracketFormat`
@@ -55,38 +72,42 @@ registerFileScribe gitRev verbosity format filePath permitF additions logenv =
5572 formatter = case format of
5673 LogBracket -> customBracketFormat additions'
5774 LogJson -> customJsonFormat gitRev additions'
58− additions' = HM.fromList . map (fmap String) $ additions
75+ additions'
76+ = HM.fromList
77+ . map (\(k, v) -> (textToKey k, String v))
78+ $ additions
5979
6080-- | JSON formatter that adds the specified fields to the output
6181customJsonFormat
62− :: forall a. LogItem a => Text -> HM.HashMap Text Value -> ItemFormatter a
82+ :: forall a. LogItem a => Text -> KeyMap Value -> ItemFormatter a
6383customJsonFormat gitRev additions withColor verb i = (mappend "\n") $
6484 -- Here we reimplement the standard formater because we want the
6585 -- additional key/value pairs to be added at the top level.
6686 -- So we can't use the LogWithAdditions like we do for bracket format
67− B.Builder.lazyByteString . T.Lazy.encodeUtf8 . T.Builder.toLazyText $
87+ T.Builder.fromText $
6888 K.colorBySeverity withColor (_itemSeverity i) $
69− encodeToTextBuilder (extend $ applyItemFunc (ourFormatter gitRev verb) i)
89+ (T.toStrict . encodeToLazyText) (extend $ ourItemJson verb i)
7090 where
71− extend (Object o) = Object $ HM.union additions o
91+ extend (Object o) = Object $ HM.union additions' o
7292 extend v = v
93+ additions' = HM.insert "rev-hash" (toJSON gitRev) additions
7394
7495-- | Bracket formatter that adds the specified fields to the output
7596customBracketFormat
76− :: forall a. LogItem a => HM.HashMap Text Value -> ItemFormatter a
97+ :: forall a. LogItem a => KeyMap Value -> ItemFormatter a
7798customBracketFormat additions withColor verb i =
7899 bracketFormat withColor verb (LogWithAdditions additions <$> i)
79100
80101-- | Log item payload that adds the specified additional key/value pairs
81102-- to the output on at verbosity levels
82−data LogWithAdditions a = LogWithAdditions (HM.HashMap Text Value) a
103+data LogWithAdditions a = LogWithAdditions (KeyMap Value) a
83104 deriving (Show)
84105
85106instance LogItem a => LogItem (LogWithAdditions a) where
86107 payloadKeys v (LogWithAdditions additions a) =
87108 case payloadKeys v a of
88109 AllKeys -> AllKeys
89− SomeKeys keys -> SomeKeys (HM.keys additions ++ keys)
110+ SomeKeys keys -> SomeKeys (map keyToText (HM.keys additions) ++ keys)
90111
91112instance ToObject a => ToObject (LogWithAdditions a) where
92113 toObject (LogWithAdditions additions a) =
@@ -99,5 +120,5 @@ setLoggingCtx
99120 -> ReaderT r m a
100121setLoggingCtx ctx' = local (part %~ go)
101122 where
102− go (Logger ctx f) =
103− Logger (ctx <> ctx') f
123+ go (Logger ctx scrbber f) =
124+ Logger (ctx <> ctx') scrbber f