@@ -1,4 +1,5 @@
1{-# LANGUAGE ImplicitParams #-}
1{-# LANGUAGE ImplicitParams #-}
3module Control.Logger.Katip.Utils
4module Control.Logger.Katip.Utils
4 ( katipIndexNameString
5 ( katipIndexNameString
@@ -6,7 +7,6 @@ module Control.Logger.Katip.Utils
6 , LogFormat(..)
7 , LogFormat(..)
7 , registerFileScribe
8 , registerFileScribe
8 , setLoggingCtx
9 , setLoggingCtx
10 , getKatipLogger
10 , getKatipLogger
11 , logSeverityToKSeverity
11 , logSeverityToKSeverity
@@ -20,18 +20,36 @@ import Control.Logger.Internal as L
20import Control.Logger.Katip as L
20import Control.Logger.Katip as L
21import Data.Aeson hiding (Error)
21import Data.Aeson hiding (Error)
22import Data.Aeson.Text
22import Data.Aeson.Text
23+#if MIN_VERSION_aeson(2,0,0)
23−import qualified Data.ByteString.Builder as B.Builder
24+import qualified Data.Aeson.KeyMap as HM
25+import qualified Data.Aeson.Key as HM
24import qualified Data.HashMap.Strict as HM
27import qualified Data.HashMap.Strict as HM
25import Data.Text (Text)
29import Data.Text (Text)
30+import qualified Data.Text.Lazy as T
26import qualified Data.Text.Lazy.Builder as T.Builder
31import qualified Data.Text.Lazy.Builder as T.Builder
27−import qualified Data.Text.Lazy.Encoding as T.Lazy
28import Katip as K hiding (logMsg)
32import Katip as K hiding (logMsg)
29−import Katip.Core (ItemFunc(..))
30import Katip.Monadic (KatipContextTState(..))
33import Katip.Monadic (KatipContextTState(..))
31import qualified Katip.Scribes.Handle as K
34import qualified Katip.Scribes.Handle as K
32import System.IO
35import System.IO
38+#if MIN_VERSION_aeson(2,0,0)
39+type KeyMap v = HM.KeyMap v
40+textToKey :: Text -> Key
41+textToKey = HM.fromText
42+keyToText :: Key -> Text
45+type KeyMap v = HM.HashMap Text v
47+textToKey :: Text -> Key
49+keyToText :: Key -> Text
35-- | How to format logs
53-- | How to format logs
36data LogFormat
54data LogFormat
37 = LogBracket -- ^ use `bracketFormat`
55 = LogBracket -- ^ use `bracketFormat`
@@ -55,38 +73,42 @@ registerFileScribe gitRev verbosity format filePath permitF additions logenv =
55 formatter = case format of
73 formatter = case format of
56 LogBracket -> customBracketFormat additions'
74 LogBracket -> customBracketFormat additions'
57 LogJson -> customJsonFormat gitRev additions'
75 LogJson -> customJsonFormat gitRev additions'
58− additions' = HM.fromList . map (fmap String) $ additions
76+ additions'
78+ . map (\(k, v) -> (textToKey k, String v))
60-- | JSON formatter that adds the specified fields to the output
81-- | JSON formatter that adds the specified fields to the output
61customJsonFormat
82customJsonFormat
62− :: forall a. LogItem a => Text -> HM.HashMap Text Value -> ItemFormatter a
83+ :: forall a. LogItem a => Text -> KeyMap Value -> ItemFormatter a
63customJsonFormat gitRev additions withColor verb i = (mappend "\n") $
84customJsonFormat gitRev additions withColor verb i = (mappend "\n") $
64 -- Here we reimplement the standard formater because we want the
85 -- Here we reimplement the standard formater because we want the
65 -- additional key/value pairs to be added at the top level.
86 -- additional key/value pairs to be added at the top level.
66 -- So we can't use the LogWithAdditions like we do for bracket format
87 -- So we can't use the LogWithAdditions like we do for bracket format
67− B.Builder.lazyByteString . T.Lazy.encodeUtf8 . T.Builder.toLazyText $
88+ T.Builder.fromText $
68 K.colorBySeverity withColor (_itemSeverity i) $
89 K.colorBySeverity withColor (_itemSeverity i) $
69− encodeToTextBuilder (extend $ applyItemFunc (ourFormatter gitRev verb) i)
90+ (T.toStrict . encodeToLazyText) (extend $ ourItemJson verb i)
71− extend (Object o) = Object $ HM.union additions o
92+ extend (Object o) = Object $ HM.union additions' o
72 extend v = v
93 extend v = v
94+ additions' = HM.insert "rev-hash" (toJSON gitRev) additions
74-- | Bracket formatter that adds the specified fields to the output
96-- | Bracket formatter that adds the specified fields to the output
75customBracketFormat
97customBracketFormat
76− :: forall a. LogItem a => HM.HashMap Text Value -> ItemFormatter a
98+ :: forall a. LogItem a => KeyMap Value -> ItemFormatter a
77customBracketFormat additions withColor verb i =
99customBracketFormat additions withColor verb i =
78 bracketFormat withColor verb (LogWithAdditions additions <$> i)
100 bracketFormat withColor verb (LogWithAdditions additions <$> i)
80-- | Log item payload that adds the specified additional key/value pairs
102-- | Log item payload that adds the specified additional key/value pairs
81-- to the output on at verbosity levels
103-- to the output on at verbosity levels
82−data LogWithAdditions a = LogWithAdditions (HM.HashMap Text Value) a
104+data LogWithAdditions a = LogWithAdditions (KeyMap Value) a
83 deriving (Show)
105 deriving (Show)
85instance LogItem a => LogItem (LogWithAdditions a) where
107instance LogItem a => LogItem (LogWithAdditions a) where
86 payloadKeys v (LogWithAdditions additions a) =
108 payloadKeys v (LogWithAdditions additions a) =
87 case payloadKeys v a of
109 case payloadKeys v a of
88 AllKeys -> AllKeys
110 AllKeys -> AllKeys
89− SomeKeys keys -> SomeKeys (HM.keys additions ++ keys)
111+ SomeKeys keys -> SomeKeys (map keyToText (HM.keys additions) ++ keys)
91instance ToObject a => ToObject (LogWithAdditions a) where
113instance ToObject a => ToObject (LogWithAdditions a) where
92 toObject (LogWithAdditions additions a) =
114 toObject (LogWithAdditions additions a) =