Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json logging #836

Merged
merged 9 commits into from
Aug 29, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions libs/extended/src/System/Logger/Extended.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,25 @@ data LogFormat = JSON | Plain | Netstring
-- | We use this as an intermediate structure to ease the implementation of the
-- ToJSON instance but we could just inline everything. I think this has
-- negligible impact and makes the code a bit more readable. Let me know
data Element' = Element' [(Builder, Builder)] [Builder]
data Element' = Element' Series [Builder]
arianvp marked this conversation as resolved.
Show resolved Hide resolved

instance ToJSON Element' where
toJSON = error "Use toEncoding"
toEncoding :: Element' -> Encoding
toEncoding (Element' fields msgs) = pairs $ fieldsToSeries fields <> msgsToSeries msgs
elementToEncoding :: Element' -> Encoding
elementToEncoding (Element' fields msgs) = pairs $ fields <> msgsToSeries msgs
where
fieldsToSeries :: [(Builder, Builder)] -> Series
fieldsToSeries = foldMap $ \(k,v) -> pair (cs . eval $ k) (text . cs . eval $ v)
msgsToSeries :: [Builder] -> Series
msgsToSeries = pair "msgs" . list (text . cs . eval)

collect :: [Element] -> Element'
collect = foldr go (Element' [] [])
collect = foldr go (Element' mempty [])
where
go :: Element -> Element' -> Element'
go (Bytes b) (Element' f m) =
Element' f (b : m)
go (Field k v) (Element' f m) =
Element' ((k,v) : f) m
Element' (f <> pair (cs . eval $ k) (text . cs . eval $ v)) m

jsonRenderer :: Renderer
jsonRenderer _sep _dateFormat _logLevel = fromEncoding . toEncoding . collect
jsonRenderer _sep _dateFormat _logLevel = fromEncoding . elementToEncoding . collect

-- | Here for backwards-compatibility reasons
netStringsToLogFormat :: Bool -> LogFormat
Expand Down