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,2942

Module documentation for 0.2.0.0

katip-wai ci

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 Request type that contains all the info about a request including its trace id.
  • Added a Response type that contains info about a response, including how long it took the server to respond.
  • traceRequest to convert from a Network.Wai.Request to a Request.
  • traceResponse to convert from a Network.Wai.Response to a Response.
  • Added the Options type, which provides more flexibility to customize how the request and response are handled.
  • Added more formatting options for both the request and response.
  • Added middlewareCustom for full customization.

Changed

  • The default formats used by middleware have been changed. See defaultRequestFormat and defaultResponseFormat for the new formats.

Removed

  • middlewareWithFormatter has been removed. Please use middlewareCustom instead.

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

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 middlewareWithFormatter and defaultFormat so that you may customize the logging format.

0.1.0.0 - 2021-12-29

Added

  • Initial release