wai-log-0.3.0.0: A logging middleware for WAI applications
Safe HaskellNone
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_uuid": "f2c89425-9ec4-4cd2-ae56-4bab23681fce",
  "remote_host": "127.0.0.1:34694"
}
2020-10-27 12:30:23 INFO eid-server: Sending response {
  "request_uuid": "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_uuid": "f2c89425-9ec4-4cd2-ae56-4bab23681fce"
}
Synopsis

Create a Middleware

Type

type LogMiddleware = (UUID -> 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 a UUID to the Application, containting the request_uuid, so it can be logged in the application's context.

Options

data Options 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 permormed. 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 Source #

Default Options

{ logLevel = LogInfo
, logRequest = defaultLogRequest
, logResponse = defaultLogResponse
}

defaultLogRequest :: UUID -> Request -> [Pair] Source #

Logs the following request values:

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

defaultLogResponse :: UUID -> Request -> Response -> Value -> ResponseTime -> [Pair] Source #

Logs the following values:

  • request_uuid
  • 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

Helpers

logRequestUUID :: MonadLog m => UUID -> m a -> m a Source #

Helper to consistently log the UUID in your application by adding request_uuid field to log's localData