katip-wai 0.1.0.0 → 0.1.1.0
raw patch · 2 files changed
+47/−36 lines, 2 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ Katip.Wai: defaultFormat :: UUID -> Request -> Value
+ Katip.Wai: middlewareWithFormatter :: KatipContext m => (UUID -> Request -> Value) -> Severity -> MiddlewareT m
Files
- katip-wai.cabal +1/−1
- src/Katip/Wai.hs +46/−35
katip-wai.cabal view
@@ -1,7 +1,7 @@ cabal-version: 2.2 name: katip-wai-version: 0.1.0.0+version: 0.1.1.0 synopsis: WAI middleware for logging request and response info through katip. description: WAI middleware for logging request and response info through katip. Please see the README on GitHub at <https://github.com/Disco-Dave/katip-wai#readme>
src/Katip/Wai.hs view
@@ -8,32 +8,6 @@ Add information about 'Wai.Request', 'Wai.response', and the elapsed time to Katip's 'Katip.LogContexts'. -The following is added to the context as \"request\":-- - \"id\": Uniquely generated string that can be used to correlate logs to a single request.-- - \"httpVersion\": Version of the request.-- - \"remoteHost\": Address the request came from.-- - \"isSecure\": True if the request was made over an SSL connection, otherwise false.-- - \"method\": HTTP Method used for the request, ie. GET, POST, PUT, PATCH, DELETE, etc.-- - \"path\": URL without a hostname, port, or querystring.-- - \"queryString\": Query string of the request if one was sent.-- - \"bodyLength\": Size of the body in the request.-- - \"headers.host\": Value of the \"Host\" header.-- - \"headers.referer\": Value of the \"Referer\" header.-- - \"headers.userAgent\": Value of the \"User-Agent\" header.-- - \"headers.range\": Value of the \"Range\" header.- The following is added to the context as \"response\": - \"elapsedTimeInNanoSeconds\": Amount of time from receiving the request to sending the response in nano seconds.@@ -41,6 +15,9 @@ - \"status\": The status of the response, ie. 200, 202, 204, 400, 404, 500, etc. -} module Katip.Wai (+ -- * Middleware+ defaultFormat,+ middlewareWithFormatter, middleware, -- * Helpers for threading Katip's Context throughout the entire 'Wai.Application`@@ -109,10 +86,38 @@ toJSON = Aeson.object . requestToKeyValues toEncoding = Aeson.pairs . mconcat . requestToKeyValues -toLoggableRequest :: Wai.Request -> IO Request-toLoggableRequest request = do- requestId <- UUID.V4.nextRandom- pure+{- | Default format for the request. The 'requestId' will be unique every time we+ call this function, it's an id to correlate logs generated by the request.++ Current format:++ - \"id\": Uniquely generated string that can be used to correlate logs to a single request.++ - \"httpVersion\": Version of the request.++ - \"remoteHost\": Address the request came from.++ - \"isSecure\": True if the request was made over an SSL connection, otherwise false.++ - \"method\": HTTP Method used for the request, ie. GET, POST, PUT, PATCH, DELETE, etc.++ - \"path\": URL without a hostname, port, or querystring.++ - \"queryString\": Query string of the request if one was sent.++ - \"bodyLength\": Size of the body in the request.++ - \"headers.host\": Value of the \"Host\" header.++ - \"headers.referer\": Value of the \"Referer\" header.++ - \"headers.userAgent\": Value of the \"User-Agent\" header.++ - \"headers.range\": Value of the \"Range\" header.+-}+defaultFormat :: UUID -> Wai.Request -> Aeson.Value+defaultFormat requestId request =+ Aeson.toJSON $ Request { requestId = requestId , requestHttpVersion = Wai.httpVersion request@@ -173,11 +178,17 @@ Katip.logFM severity "Response sent" pure responseReceived --- | Logs the request, response, and elapsed time in Katip's context-middleware :: Katip.KatipContext m => Katip.Severity -> MiddlewareT m-middleware severity application request send = do+-- | Logs the request using the provided format, response, and elapsed time in Katip's context+middlewareWithFormatter :: Katip.KatipContext m => (UUID -> Wai.Request -> Aeson.Value) -> Katip.Severity -> MiddlewareT m+middlewareWithFormatter format severity application request send = do start <- liftIO $ Clock.getTime Clock.Monotonic- loggableRequest <- liftIO $ toLoggableRequest request- Katip.katipAddContext (Katip.sl "request" loggableRequest) $ do+ requestId <- liftIO UUID.V4.nextRandom+ let jsonRequest = format requestId request+ Katip.katipAddContext (Katip.sl "request" jsonRequest) $ do Katip.logFM severity "Request received" application request (withLoggedResponse severity start send)++-- | Logs the request, response, and elapsed time in Katip's context+middleware :: Katip.KatipContext m => Katip.Severity -> MiddlewareT m+middleware =+ middlewareWithFormatter defaultFormat