on master help sign in
Probably better if payloads define their important keys
commit1f455801b6c8792d8354e84c044e714335e8cb8f authorOzgun Ataman <ozgun.ataman@soostone.com> dateSat 31 Jan 2015, 17:33 (12 years ago) parentef103af
2 files changed+20−8
src/Katip/Core.hs+14−2 src/Katip/Scribes/Handle.hs+6−6
src/Katip/Core.hs+14−2
@@ -93,11 +93,23 @@ instance ToJSON a => ToJSON (Item a) where
9393 ]
9494
9595
96+-------------------------------------------------------------------------------
97+-- | Payload objects need instances of this class.
98+class ToJSON a => LogContext a where
99+
100+ -- | List of keys in the JSON object that should be printed in
101+ -- summarizing human-readable output.
102+ importantKeys :: a -> [Text]
103+
104+
105+instance LogContext () where importantKeys _ = []
106+
107+
96108-------------------------------------------------------------------------------
97109-- | Scribes are handlers of incoming items. Each registered scribe
98110-- knows how to push a log item somewhere.
99111data Scribe = Scribe {
100− lhPush :: forall a. ToJSON a => Item a -> IO ()
112+ lhPush :: forall a. LogContext a => Item a -> IO ()
101113 }
102114
103115
@@ -156,7 +168,7 @@ class Katip m where
156168-------------------------------------------------------------------------------
157169-- | The generic *full* logging function.
158170logF
159− :: (Applicative m, MonadIO m, ToJSON a, Katip m)
171+ :: (Applicative m, MonadIO m, LogContext a, Katip m)
160172 => Severity
161173 -> a
162174 -- ^ Contextual payload for the log
src/Katip/Scribes/Handle.hs+6−6
@@ -29,8 +29,8 @@ brackets m = fromByteString "[" <> m <> fromByteString "]"
2929
3030
3131-------------------------------------------------------------------------------
32−getKeys :: ToJSON s => s -> [Text] -> [Builder]
33−getKeys a keys = flip mapMaybe keys $ \ k ->
32+getKeys :: LogContext s => s -> [Builder]
33+getKeys a = flip mapMaybe (importantKeys a) $ \ k ->
3434 a' ^? key k . _Primitive . to renderPrim
3535 where
3636 a' = toJSON a
@@ -44,12 +44,12 @@ renderPrim NullPrim = fromByteString "null"
4444
4545
4646-------------------------------------------------------------------------------
47−mkHandleScribe :: Handle -> [Text] -> Severity -> IO Scribe
48−mkHandleScribe h keys sev = do
47+mkHandleScribe :: Handle -> Severity -> IO Scribe
48+mkHandleScribe h sev = do
4949 hSetBuffering h LineBuffering
5050 return $ Scribe $ \ Item{..} -> do
5151 let nowStr = fromString $ formatTime defaultTimeLocale "%Y-%m-%d %H:%M:%S" itemTime
52− ks = map brackets $ getKeys itemPayload keys
52+ ks = map brackets $ getKeys itemPayload
5353 msg = brackets nowStr <>
5454 brackets (fromText (renderSeverity itemSeverity)) <>
5555 brackets (fromString itemHost) <>
@@ -67,7 +67,7 @@ mkHandleScribe h keys sev = do
6767_ioLogEnv :: LogEnv
6868_ioLogEnv = unsafePerformIO $ do
6969 le <- initLogEnv "io" "io"
70− lh <- mkHandleScribe stdout [] Debug
70+ lh <- mkHandleScribe stdout Debug
7171 return $ registerHandler "stdout" lh le
7272{-# NOINLINE _ioLogEnv #-}
7373