@@ -1,4 +1,4 @@
1−{-# LANGUAGE ImplicitParams #-}
1+{-# LANGUAGE CPP #-}
22
33module Control.Logger.Katip.Utils
44 ( katipIndexNameString
@@ -6,8 +6,6 @@ module Control.Logger.Katip.Utils
66 , LogFormat(..)
77 , registerFileScribe
88 , setLoggingCtx
9− , ourFormatter
10− , ourMapping
119 , getKatipLogger
1210 , logSeverityToKSeverity
1311 ) where
@@ -21,18 +19,36 @@ import Control.Logger.Internal as L
2119import Control.Logger.Katip as L
2220import Data.Aeson hiding (Error)
2321import Data.Aeson.Text
24−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
2526import qualified Data.HashMap.Strict as HM
27+#endif
2628import Data.Text (Text)
29+import qualified Data.Text.Lazy as T
2730import qualified Data.Text.Lazy.Builder as T.Builder
28−import qualified Data.Text.Lazy.Encoding as T.Lazy
2931import Katip as K hiding (logMsg)
30−import Katip.Core (ItemFunc(..))
3132import Katip.Monadic (KatipContextTState(..))
3233import qualified Katip.Scribes.Handle as K
3334import System.IO
3435
3536
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+
3652-- | How to format logs
3753data LogFormat
3854 = LogBracket -- ^ use `bracketFormat`
@@ -56,38 +72,42 @@ registerFileScribe gitRev verbosity format filePath permitF additions logenv =
5672 formatter = case format of
5773 LogBracket -> customBracketFormat additions'
5874 LogJson -> customJsonFormat gitRev additions'
59− additions' = HM.fromList . map (fmap String) $ additions
75+ additions'
76+ = HM.fromList
77+ . map (\(k, v) -> (textToKey k, String v))
78+ $ additions
6079
6180-- | JSON formatter that adds the specified fields to the output
6281customJsonFormat
63− :: forall a. LogItem a => Text -> HM.HashMap Text Value -> ItemFormatter a
82+ :: forall a. LogItem a => Text -> KeyMap Value -> ItemFormatter a
6483customJsonFormat gitRev additions withColor verb i = (mappend "\n") $
6584 -- Here we reimplement the standard formater because we want the
6685 -- additional key/value pairs to be added at the top level.
6786 -- So we can't use the LogWithAdditions like we do for bracket format
68− B.Builder.lazyByteString . T.Lazy.encodeUtf8 . T.Builder.toLazyText $
87+ T.Builder.fromText $
6988 K.colorBySeverity withColor (_itemSeverity i) $
70− encodeToTextBuilder (extend $ applyItemFunc (ourFormatter gitRev verb) i)
89+ (T.toStrict . encodeToLazyText) (extend $ ourItemJson verb i)
7190 where
72− extend (Object o) = Object $ HM.union additions o
91+ extend (Object o) = Object $ HM.union additions' o
7392 extend v = v
93+ additions' = HM.insert "rev-hash" (toJSON gitRev) additions
7494
7595-- | Bracket formatter that adds the specified fields to the output
7696customBracketFormat
77− :: forall a. LogItem a => HM.HashMap Text Value -> ItemFormatter a
97+ :: forall a. LogItem a => KeyMap Value -> ItemFormatter a
7898customBracketFormat additions withColor verb i =
7999 bracketFormat withColor verb (LogWithAdditions additions <$> i)
80100
81101-- | Log item payload that adds the specified additional key/value pairs
82102-- to the output on at verbosity levels
83−data LogWithAdditions a = LogWithAdditions (HM.HashMap Text Value) a
103+data LogWithAdditions a = LogWithAdditions (KeyMap Value) a
84104 deriving (Show)
85105
86106instance LogItem a => LogItem (LogWithAdditions a) where
87107 payloadKeys v (LogWithAdditions additions a) =
88108 case payloadKeys v a of
89109 AllKeys -> AllKeys
90− SomeKeys keys -> SomeKeys (HM.keys additions ++ keys)
110+ SomeKeys keys -> SomeKeys (map keyToText (HM.keys additions) ++ keys)
91111
92112instance ToObject a => ToObject (LogWithAdditions a) where
93113 toObject (LogWithAdditions additions a) =
@@ -100,5 +120,5 @@ setLoggingCtx
100120 -> ReaderT r m a
101121setLoggingCtx ctx' = local (part %~ go)
102122 where
103− go (Logger ctx f) =
104− Logger (ctx <> ctx') f
123+ go (Logger ctx scrbber f) =
124+ Logger (ctx <> ctx') scrbber f