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