wai-extra-3.1.17: Provides some basic WAI handlers and middleware.

Safe Haskell | None
Language | Haskell2010

Network.Wai.Middleware.RequestLogger

Contents

- Basic stdout logging
- Create more versions
  - Settings type
    - Settings fields
  - More settings

Synopsis

- logStdout :: Middleware
- logStdoutDev :: Middleware
- mkRequestLogger :: RequestLoggerSettings -> IO Middleware
- data RequestLoggerSettings
- defaultRequestLoggerSettings :: RequestLoggerSettings
- outputFormat :: RequestLoggerSettings -> OutputFormat
- autoFlush :: RequestLoggerSettings -> Bool
- destination :: RequestLoggerSettings -> Destination
- data OutputFormat
  - = Apache IPAddrSource
  - | ApacheWithSettings ApacheSettings
  - | Detailed Bool
  - | DetailedWithSettings DetailedSettings
  - | CustomOutputFormat OutputFormatter
  - | CustomOutputFormatWithDetails OutputFormatterWithDetails
  - | CustomOutputFormatWithDetailsAndHeaders OutputFormatterWithDetailsAndHeaders
- data ApacheSettings
- defaultApacheSettings :: ApacheSettings
- setApacheIPAddrSource :: IPAddrSource -> ApacheSettings -> ApacheSettings
- setApacheRequestFilter :: (Request -> Response -> Bool) -> ApacheSettings -> ApacheSettings
- setApacheUserGetter :: (Request -> Maybe ByteString) -> ApacheSettings -> ApacheSettings
- data DetailedSettings = DetailedSettings {
  - useColors :: Bool
  - mModifyParams :: Maybe (Param -> Maybe Param)
  - mFilterRequests :: Maybe (Request -> Response -> Bool)
  - mPrelogRequests :: Bool
  }
- defaultDetailedSettings :: DetailedSettings
- type OutputFormatter = ZonedDate -> Request -> Status -> Maybe Integer -> LogStr
- type OutputFormatterWithDetails = ZonedDate -> Request -> Status -> Maybe Integer -> NominalDiffTime -> [ByteString] -> Builder -> LogStr
- type OutputFormatterWithDetailsAndHeaders = ZonedDate -> Request -> Status -> Maybe Integer -> NominalDiffTime -> [ByteString] -> Builder -> [Header] -> LogStr
- data Destination
  - = Handle Handle
  - | Logger LoggerSet
  - | Callback Callback
- type Callback = LogStr -> IO ()
- data IPAddrSource
  - = FromSocket
  - | FromHeader
  - | FromHeaderCustom [HeaderName]
  - | FromFallback
  - | FromRequest (Request -> ByteString)

# Basic stdout logging

logStdout :: Middleware #

Production request logger middleware.

This uses the `Apache` logging format, and takes IP addresses for clients from the socket (see `IPAddrSource` for more information). It logs to `stdout`.

logStdoutDev :: Middleware #

Development request logger middleware.

This uses the `Detailed` `True` logging format and logs to `stdout`.

# Create more versions

mkRequestLogger :: RequestLoggerSettings -> IO Middleware #

Create the `Middleware` using the given `RequestLoggerSettings`

Since: 1.3.0

## Settings type

data RequestLoggerSettings #

Settings for the request logger.

Sets what which format,

`outputFormat`, `autoFlush`, and `destination` are record fields for the record type `RequestLoggerSettings`, so they can be used to modify settings values using record syntax.

Since: 1.3.0

#### Instances

Instances details

Default RequestLoggerSettings # | DO NOT USE THIS INSTANCE! Please use `defaultRequestLoggerSettings` instead. This instance will be removed in a future major release.
Instance details Defined in Network.Wai.Middleware.RequestLogger Methods def :: RequestLoggerSettings #

defaultRequestLoggerSettings :: RequestLoggerSettings #

Default `RequestLoggerSettings`.

Use this to create `RequestLoggerSettings`, and use the accompanying fields to edit these settings.

Since: 3.1.8

### Settings fields

outputFormat :: RequestLoggerSettings -> OutputFormat #

Default value: `Detailed True`.

Since: 1.3.0

autoFlush :: RequestLoggerSettings -> Bool #

Only applies when using the `Handle` constructor for `destination`.

Default value: `True`.

Since: 1.3.0

destination :: RequestLoggerSettings -> Destination #

Default: `Handle stdout`.

Since: 1.3.0

## More settings

data OutputFormat #

The logging format.

Since: 1.3.0

Constructors

Apache IPAddrSource |
ApacheWithSettings ApacheSettings | Since: 3.1.8
Detailed Bool | use colors?
DetailedWithSettings DetailedSettings | Since: 3.1.3
CustomOutputFormat OutputFormatter |
CustomOutputFormatWithDetails OutputFormatterWithDetails |
CustomOutputFormatWithDetailsAndHeaders OutputFormatterWithDetailsAndHeaders |

data ApacheSettings #

Settings for the `ApacheWithSettings` `OutputFormat`. This is purposely kept as an abstract data type so that new settings can be added without breaking backwards compatibility. In order to create an `ApacheSettings` value, use `defaultApacheSettings` and the various 'setApache' functions to modify individual fields. For example:

    setApacheIPAddrSource FromHeader defaultApacheSettings

Since: 3.1.8

defaultApacheSettings :: ApacheSettings #

setApacheIPAddrSource :: IPAddrSource -> ApacheSettings -> ApacheSettings #

Where to take IP addresses for clients from. See `IPAddrSource` for more information.

Default value: FromSocket

Since: 3.1.8

setApacheRequestFilter :: (Request -> Response -> Bool) -> ApacheSettings -> ApacheSettings #

Function that allows you to filter which requests are logged, based on the request and response

Default: log all requests

Since: 3.1.8

setApacheUserGetter :: (Request -> Maybe ByteString) -> ApacheSettings -> ApacheSettings #

Function that allows you to get the current user from the request, which will then be added in the log.

Default: return no user

Since: 3.1.8

data DetailedSettings #

Settings for the `Detailed` `OutputFormat`.

`mModifyParams` allows you to pass a function to hide confidential information (such as passwords) from the logs. If result is `Nothing`, then the parameter is hidden. For example: > myformat = Detailed True (Just hidePasswords) > where hidePasswords p@(k,v) = if k = "password" then (k, "***REDACTED***") else p

`mFilterRequests` allows you to filter which requests are logged, based on the request and response.

Since: 3.1.3

Constructors

DetailedSettings |
Fields useColors :: Bool mModifyParams :: Maybe (Param -> Maybe Param) mFilterRequests :: Maybe (Request -> Response -> Bool) mPrelogRequests :: Bool Since: 3.1.7

#### Instances

Instances details

Default DetailedSettings # | DO NOT USE THIS INSTANCE! Please use `defaultDetailedSettings` This instance will be removed in a future major version.
Instance details Defined in Network.Wai.Middleware.RequestLogger Methods def :: DetailedSettings #

defaultDetailedSettings :: DetailedSettings #

Default `DetailedSettings`

Uses colors, but doesn't modify nor filter anything. Also doesn't prelog requests.

Since: 3.1.16

type OutputFormatter = ZonedDate -> Request -> Status -> Maybe Integer -> LogStr #

type OutputFormatterWithDetails = ZonedDate -> Request -> Status -> Maybe Integer -> NominalDiffTime -> [ByteString] -> Builder -> LogStr #

type OutputFormatterWithDetailsAndHeaders #

Arguments

= ZonedDate | When the log message was generated
-> Request | The WAI request
-> Status | HTTP status code
-> Maybe Integer | Response size
-> NominalDiffTime | Duration of the request
-> [ByteString] | The request body
-> Builder | Raw response
-> [Header] | The response headers
-> LogStr |

Same as `OutputFormatterWithDetails` but with response headers included

This is useful if you wish to include arbitrary application data in your logs, e.g., an authenticated user ID, which you would set in a response header in your application and retrieve in the log formatter.

Since: 3.0.27

data Destination #

Where to send the logs to.

Since: 1.3.0

Constructors

Handle Handle |
Logger LoggerSet |
Callback Callback |

type Callback = LogStr -> IO () #

When using a callback as a destination.

Since: 1.3.0

data IPAddrSource #

Source from which the IP source address of the client is obtained.

Constructors

FromSocket | From the peer address of the HTTP connection.
FromHeader | From `X-Real-IP` or `X-Forwarded-For` in the HTTP header. This picks either `X-Real-IP` or `X-Forwarded-For` depending on which of these headers comes first in the ordered list of request headers. If the `X-Forwarded-For` header is picked, the value will be assumed to be a comma-separated list of IP addresses. The value will be parsed, and the left-most IP address will be used (which is mostly likely to be the actual client IP address).
FromHeaderCustom [HeaderName] | From a custom HTTP header, useful in proxied environment. The header value will be assumed to be a comma-separated list of IP addresses. The value will be parsed, and the left-most IP address will be used (which is mostly likely to be the actual client IP address). Note that this still works as expected for a single IP address.
FromFallback | Just like `FromHeader`, but falls back on the peer address if header is not found.
FromRequest (Request -> ByteString) | This gives you the most flexibility to figure out the IP source address from the `Request`. The returned `ByteString` is used as the IP source address.