katip-wai
WAI middleware for logging request and response info through katip.
https://github.com/Disco-Dave/katip-wai#readme
| LTS Haskell 24.18: | 0.2.0.0 | 
| Stackage Nightly 2025-11-03: | 0.2.0.0 | 
| Latest on Hackage: | 0.2.0.0 | 
BSD-3-Clause licensed and maintained by David Burkett
This version can be pinned in stack with:
katip-wai-0.2.0.0@sha256:aa70436cc019c4c70c5226c25de726e6a4d88d02d3594bcd82f7b4cf0f59f13a,2942Module documentation for 0.2.0.0
Depends on 13 packages(full list with versions):
aeson, base, bytestring, case-insensitive, clock, containers, http-types, katip, network, text, time, uuid, wai
katip-wai 
Middleware for logging http request and response information through Katip.
You can find the full documentation on Hackage.
Example using Servant (./example)
import Control.Exception (bracket)
import Data.Proxy (Proxy (Proxy))
import Katip qualified
import Katip.Wai (ApplicationT, runApplication)
import Katip.Wai qualified
import Network.Wai.Handler.Warp qualified as Warp
import Servant qualified
import System.IO (stdout)
import UnliftIO (MonadUnliftIO (withRunInIO))
type Api = Servant.GetNoContent
server :: Servant.ServerT Api (Katip.KatipContextT Servant.Handler)
server = do
  Katip.logLocM Katip.InfoS "This message should also have the request context"
  pure Servant.NoContent
mkApplication :: ApplicationT (Katip.KatipContextT IO)
mkApplication = Katip.Wai.middleware Katip.InfoS $ request send -> do
  logEnv <- Katip.getLogEnv
  context <- Katip.getKatipContext
  namespace <- Katip.getKatipNamespace
  let hoistedApp =
        let proxy = Proxy @Api
            hoistedServer = Servant.hoistServer proxy (Katip.runKatipContextT logEnv context namespace) server
         in Servant.serve proxy hoistedServer
  withRunInIO $ toIO -> hoistedApp request (toIO . send)
withLogEnv :: (Katip.LogEnv -> IO a) -> IO a
withLogEnv useLogEnv = do
  handleScribe <-
    Katip.mkHandleScribeWithFormatter
      Katip.jsonFormat
      (Katip.ColorLog False)
      stdout
      (Katip.permitItem minBound)
      Katip.V3
  let makeLogEnv =
        Katip.initLogEnv "example-app" "local-dev"
          >>= Katip.registerScribe "stdout" handleScribe Katip.defaultScribeSettings
  bracket makeLogEnv Katip.closeScribes useLogEnv
main :: IO ()
main = withLogEnv $ logEnv ->
  let
    app = runApplication (Katip.runKatipContextT logEnv () "main") mkApplication
   in
    Warp.run 5555 app
Example output
{"app":["example-app"],"at":"2024-09-07T18:44:10.411097829Z","data":{"request":{"headers":{Host:"localhost:5555","User-Agent":"curl8.9.1"},"httpVersion":"HTTP1.1","id":"7ec0fbc4-722c-4c70-a168-c2abe5c7b4fa","isSecure":false,"method":GET,"path":"/","queryString":[],"receivedAt":"2024-09-07T18:44:10.411057334Z","remoteHost":"127.0.0.1:51230"}},"env":"local-dev","host":"x1g11","loc":null,"msg":"Request received.","ns":["example-app","main"],"pid":"106249","sev":Info,"thread":"27"}
{"app":["example-app"],"at":"2024-09-07T18:44:10.411097829Z","data":{"request":{"headers":{Host:"localhost:5555","User-Agent":"curl8.9.1"},"httpVersion":"HTTP1.1","id":"7ec0fbc4-722c-4c70-a168-c2abe5c7b4fa","isSecure":false,"method":GET,"path":"","queryString":[],"receivedAt":"2024-09-07T18:44:10.411057334Z","remoteHost":"127.0.0.1:51230"}},"env":"local-dev","host":"x1g11","loc":{"loc_col":3,"loc_fn":"srcKatipWaiExample/Short.hs","loc_ln":19,"loc_mod":Katip.Wai.Example.Short,"loc_pkg":"my-katip-wai-example-0.1.0.0-inplace"},"msg":"This message should also have the request context","ns":["example-app","main"],"pid":"106249","sev":Info,"thread":"27"}
{"app":["example-app"],"at":"2024-09-07T18:44:10.411097829Z","data":{"request":{"headers":{Host:"localhost:5555","User-Agent":"curl8.9.1"},"httpVersion":"HTTP1.1","id":"7ec0fbc4-722c-4c70-a168-c2abe5c7b4fa","isSecure":false,"method":GET,"path":"/","queryString":[],"receivedAt":"2024-09-07T18:44:10.411057334Z","remoteHost":"127.0.0.1:51230"},"response":{"headers":{},"respondedAt":"2024-09-07T18:44:10.411199014Z","responseTime":{"time":0.137369,"unit":"ms"},"status":{"code":204,"message":"No Content"}}},"env":"local-dev","host":"x1g11","loc":null,"msg":"Response sent.","ns":["example-app","main"],"pid":"106249","sev":Info,"thread":"27"}
Changes
Changelog
All notable changes to this project will be documented in this file.
The format is based on Keep a Changelog, and this project adheres to Haskell Package Versioning Policy.
0.2.0.0 - 2024-09-11
Added
- Added a 
Requesttype that contains all the info about a request including its trace id. - Added a 
Responsetype that contains info about a response, including how long it took the server to respond. traceRequestto convert from aNetwork.Wai.Requestto aRequest.traceResponseto convert from aNetwork.Wai.Responseto aResponse.- Added the 
Optionstype, which provides more flexibility to customize how the request and response are handled. - Added more formatting options for both the request and response.
 - Added 
middlewareCustomfor full customization. 
Changed
- The default formats used by 
middlewarehave been changed. SeedefaultRequestFormatanddefaultResponseFormatfor the new formats. 
Removed
middlewareWithFormatterhas been removed. Please usemiddlewareCustominstead.
0.1.2.4 - 2024-05-16
Fixed
- Increased upper bound for 
network 
0.1.2.3 - 2024-01-01
Fixed
- Increased upper bound for 
text - Increased upper bound for 
bytestring 
0.1.2.2 - 2023-06-29
Fixed
- Increased upper bound for 
aeson 
0.1.2.1 - 2022-11-12
Added
- Added CHANGELOG.md
 - Added CONTRIBUTING.md
 
Fixed
- Adjusted version bounds
 - Fixed haddock error
 
0.1.2.0 - 2022-06-04
Fixed
- Adjusted version bound for text.
 
0.1.1.0 - 2022-02-02
Added
- Added 
middlewareWithFormatteranddefaultFormatso that you may customize the logging format. 
0.1.0.0 - 2021-12-29
Added
- Initial release