wai-log-0.4.0.1: A logging middleware for WAI applications
Safe HaskellSafe-Inferred
LanguageHaskell2010

Network.Wai.Log

Description

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"
}
Synopsis

Create a Middleware

mkLogMiddlewareWith :: (MonadLog m, ToJSON id) => Options id -> m (LogMiddleware id) Source #

Create a LogMiddleware using the supplied Options

Type

type LogMiddleware id = (id -> Application) -> Application Source #

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.

Options

data Options id Source #

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.

Constructors

Options 

Fields

data ResponseTime Source #

Timing data

Constructors

ResponseTime 

Fields

defaultOptions :: Options UUID Source #

Default Options

{ logLevel = LogInfo
, logRequest = defaultLogRequest
, logResponse = defaultLogResponse
, logGetRequestId = 'const nextRandom'
}

defaultLogRequest :: ToJSON id => id -> Request -> [Pair] Source #

Logs the following request values:

  • request_uuid
  • method
  • url path
  • remote host
  • user agent
  • body-length

defaultLogResponse :: ToJSON id => id -> Request -> Response -> Value -> ResponseTime -> [Pair] Source #

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

mkOpaqueDefaultOptions :: ToJSON id => (Request -> IO id) -> Options id Source #

Build a default Options record for an opaque id given a function for retrieving an id.

Helpers

logRequestId :: (MonadLog m, ToJSON id) => id -> m a -> m a Source #

Helper to consistently log the request id in your application by adding request_id field to log's localData