@@ -143,34 +143,34 @@ showLS = ls . show
143-------------------------------------------------------------------------------
143-------------------------------------------------------------------------------
144-- | This has everything each log message will contain.
144-- | This has everything each log message will contain.
145data Item a = Item {
145data Item a = Item {
146− itemApp :: Namespace
146+ _itemApp :: Namespace
147− , itemEnv :: Environment
147+ , _itemEnv :: Environment
148− , itemSeverity :: Severity
148+ , _itemSeverity :: Severity
149− , itemThread :: ThreadId
149+ , _itemThread :: ThreadId
150− , itemHost :: HostName
150+ , _itemHost :: HostName
151− , itemProcess :: ProcessID
151+ , _itemProcess :: ProcessID
152− , itemPayload :: a
152+ , _itemPayload :: a
153− , itemMessage :: LogStr
153+ , _itemMessage :: LogStr
154− , itemTime :: UTCTime
154+ , _itemTime :: UTCTime
155− , itemNamespace :: Namespace
155+ , _itemNamespace :: Namespace
156− , itemLoc :: Maybe Loc
156+ , _itemLoc :: Maybe Loc
157 } deriving (Generic)
157 } deriving (Generic)
158−
158+makeLenses ''Item
161instance ToJSON a => ToJSON (Item a) where
161instance ToJSON a => ToJSON (Item a) where
162 toJSON Item{..} = A.object
162 toJSON Item{..} = A.object
163− [ "app" A..= itemApp
163+ [ "app" A..= _itemApp
164− , "env" A..= itemEnv
164+ , "env" A..= _itemEnv
165− , "sev" A..= itemSeverity
165+ , "sev" A..= _itemSeverity
166− , "thread" A..= show itemThread
166+ , "thread" A..= show _itemThread
167− , "host" A..= itemHost
167+ , "host" A..= _itemHost
168− , "pid" A..= A.String (toS (show itemProcess))
168+ , "pid" A..= A.String (toS (show _itemProcess))
169− , "data" A..= itemPayload
169+ , "data" A..= _itemPayload
170− , "msg" A..= (B.toLazyText $ unLogStr itemMessage)
170+ , "msg" A..= (B.toLazyText $ unLogStr _itemMessage)
171− , "at" A..= itemTime
171+ , "at" A..= _itemTime
172− , "ns" A..= itemNamespace
172+ , "ns" A..= _itemNamespace
173− , "loc" A..= fmap LocJs itemLoc
173+ , "loc" A..= fmap LocJs _itemLoc
176newtype LocJs = LocJs { getLocJs :: Loc }
176newtype LocJs = LocJs { getLocJs :: Loc }
@@ -224,6 +224,14 @@ payloadJson verb a = case foldMap (flip payloadKeys a) [(V0)..verb] of
224 & _Object %~ HM.filterWithKey (\ k _ -> k `elem` ks)
224 & _Object %~ HM.filterWithKey (\ k _ -> k `elem` ks)
227+-------------------------------------------------------------------------------
228+-- | Convert log item to its JSON representation while trimming its
229+-- payload based on the desired verbosity. Backends that push JSON
230+-- messages should use this to obtain their payload.
231+itemJson :: LogContext a => Verbosity -> Item a -> A.Value
232+itemJson verb a = toJSON $ a & itemPayload %~ payloadJson verb
227-------------------------------------------------------------------------------
235-------------------------------------------------------------------------------
228-- | Scribes are handlers of incoming items. Each registered scribe
236-- | Scribes are handlers of incoming items. Each registered scribe
229-- knows how to push a log item somewhere.
237-- knows how to push a log item somewhere.