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