Create a LogMiddleware using defaultOptions
Use mkLogMiddlewareWith for custom Options
:: a typeCtrl KGHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27
Modulewai-log-0.4.0.1Haskell2010
A simple logging middleware for WAI applications that supports the 'log-*' family of packages: https://hackage.haskell.org/package/log-base
When logging to stdout using defaultOptions, the output looks like this:
2020-10-27 12:30:23 INFO eid-server: Request received {
"url": "/api/v1/transaction/new",
"body_length": "KnownLength 136",
"method": "POST",
"user_agent": "curl/7.68.0",
"request_id": "f2c89425-9ec4-4cd2-ae56-4bab23681fce",
"remote_host": "127.0.0.1:34694"
}
2020-10-27 12:30:23 INFO eid-server: Sending response {
"request_id": "f2c89425-9ec4-4cd2-ae56-4bab23681fce"
}
2020-10-27 12:30:23 INFO eid-server: Request complete {
"response_body": null,
"url": "/api/v1/transaction/new",
"method": "POST",
"status": {
"code": 400,
"message": "Bad Request"
},
"time": {
"process": 2.97493e-3,
"full": 3.159565e-3
},
"request_id": "f2c89425-9ec4-4cd2-ae56-4bab23681fce"
}
Create a LogMiddleware using defaultOptions
Use mkLogMiddlewareWith for custom Options
Create a LogMiddleware using the supplied Options
The classic wai Middleware type is Application -> Application, but
that does not work when you want to pass logging context down to the
Application.
Instead we pass an id to the Application, containing the
request_id, so it can be logged in the application's context.
Logging options
Logging response body involves extracting it from Response via IO operations,
therefore the logBody option takes Request, Status and ResponseHeaders
as arguments to decide whether the IO operations of body extraction have
to be performed.
The resulting Maybe function is the constructor of a loggable Value
from the body bytestring builder.
OptionslogLevel :: LogLevellogRequest :: id -> Request -> [Pair]logResponse :: id -> Request -> Response -> Value -> ResponseTime -> [Pair]logBody :: Maybe (Request -> Status -> ResponseHeaders -> Maybe (Builder -> Value))An optional constructor of the response body log value.
logGetRequestId :: Request -> IO idA function for getting the request id
Timing data
ResponseTimeprocessing :: NominalDiffTimeTime between request received and application finished processing request
full :: NominalDiffTimeTime between request received and response sent
Default Options
{ logLevel = LogInfo
, logRequest = defaultLogRequest
, logResponse = defaultLogResponse
, logGetRequestId = 'const nextRandom'
}
Logs the following request values:
request_uuid
method
url path
remote host
user agent
body-length
Logs the following values:
request_id
request method
request url path
response_body details provided as Value
status code
status message
time full
time processing
Time is in seconds as that is how NominalDiffTime is treated by default
Build a default Options record for an opaque id given a function for retrieving an id.
Helper to consistently log the request id in your application by adding
request_id field to log's localData