Network.Wai.Logger

wai-logger-2.5.0: A logging system for WAI

Safe Haskell | None
Language | Haskell2010

Network.Wai.Logger

Contents

- High level functions
- Creating a logger
- Types
- Utilities
- Backward compability

Description

Apache style logger for WAI applications.

An example:

    {-# LANGUAGE OverloadedStrings #-}
    module Main where

    import Data.ByteString.Builder (byteString)
    import Control.Monad.IO.Class (liftIO)
    import qualified Data.ByteString.Char8 as BS
    import Network.HTTP.Types (status200)
    import Network.Wai (Application, responseBuilder)
    import Network.Wai.Handler.Warp (run)
    import Network.Wai.Logger (withStdoutLogger, ApacheLogger)

    main :: IO ()
    main = withStdoutLogger $ \aplogger ->
        run 3000 $ logApp aplogger

    logApp :: ApacheLogger -> Application
    logApp aplogger req response = do
        liftIO $ aplogger req status (Just len)
        response $ responseBuilder status hdr msg
      where
        status = status200
        hdr = [("Content-Type", "text/plain")]
        pong = "PONG"
        msg = byteString pong
        len = fromIntegral $ BS.length pong

Synopsis

- type ApacheLogger = Request -> Status -> Maybe Integer -> IO ()
- withStdoutLogger :: (ApacheLogger -> IO a) -> IO a
- type ServerPushLogger = Request -> ByteString -> Integer -> IO ()
- data ApacheLoggerActions
- apacheLogger :: ApacheLoggerActions -> ApacheLogger
- serverpushLogger :: ApacheLoggerActions -> ServerPushLogger
- logRotator :: ApacheLoggerActions -> IO ()
- logRemover :: ApacheLoggerActions -> IO ()
- initLoggerUser :: ToLogStr user => Maybe (Request -> Maybe user) -> IPAddrSource -> LogType -> IO FormattedTime -> IO ApacheLoggerActions
- initLogger :: IPAddrSource -> LogType -> IO FormattedTime -> IO ApacheLoggerActions
- data IPAddrSource
  - = FromSocket
  - | FromHeader
  - | FromHeaderCustom [HeaderName]
  - | FromFallback
  - | FromRequest (Request -> ByteString)
- data LogType' a where
  - LogNone :: LogType' LogStr
  - LogStdout :: BufSize -> LogType' LogStr
  - LogStderr :: BufSize -> LogType' LogStr
  - LogFileNoRotate :: FilePath -> BufSize -> LogType' LogStr
  - LogFile :: FileLogSpec -> BufSize -> LogType' LogStr
  - LogFileTimedRotate :: TimedFileLogSpec -> BufSize -> LogType' LogStr
  - LogCallback :: forall a. (a -> IO ()) -> IO () -> LogType' a
- type LogType = LogType' LogStr
- data FileLogSpec = FileLogSpec {
  - log_file :: FilePath
  - log_file_size :: Integer
  - log_backup_number :: Int
  }
- showSockAddr :: SockAddr -> NumericAddress
- logCheck :: LogType -> IO ()
- clockDateCacher :: IO (DateCacheGetter, DateCacheUpdater)
- type ZonedDate = FormattedTime
- type DateCacheGetter = IO ZonedDate
- type DateCacheUpdater = IO ()

# High level functions

type ApacheLogger = Request -> Status -> Maybe Integer -> IO () #

Apache style logger.

withStdoutLogger :: (ApacheLogger -> IO a) -> IO a #

Executing a function which takes `ApacheLogger`. This `ApacheLogger` writes log message to stdout. Each buffer (4K bytes) is flushed every second.

type ServerPushLogger = Request -> ByteString -> Integer -> IO () #

HTTP/2 server push logger in Apache style.

# Creating a logger

data ApacheLoggerActions #

Function set of Apache style logger.

apacheLogger :: ApacheLoggerActions -> ApacheLogger #

The Apache logger.

serverpushLogger :: ApacheLoggerActions -> ServerPushLogger #

The HTTP/2 server push logger.

logRotator :: ApacheLoggerActions -> IO () #

This is obsoleted. Rotation is done on-demand. So, this is now an empty action.

logRemover :: ApacheLoggerActions -> IO () #

Removing resources relating to Apache logger. E.g. flushing and deallocating internal buffers.

initLoggerUser :: ToLogStr user => Maybe (Request -> Maybe user) -> IPAddrSource -> LogType -> IO FormattedTime -> IO ApacheLoggerActions #

Creating `ApacheLogger` according to `LogType`.

initLogger :: IPAddrSource -> LogType -> IO FormattedTime -> IO ApacheLoggerActions #

# Types

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.

data LogType' a where #

Logger Type.

Constructors

LogNone |
Fields :: LogType' LogStr No logging.
LogStdout |
Fields :: BufSize -> LogType' LogStr Logging to stdout. `BufSize` is a buffer size for each capability.
LogStderr |
Fields :: BufSize -> LogType' LogStr Logging to stderr. `BufSize` is a buffer size for each capability.
LogFileNoRotate |
Fields :: FilePath -> BufSize -> LogType' LogStr Logging to a file. `BufSize` is a buffer size for each capability.
LogFile |
Fields :: FileLogSpec -> BufSize -> LogType' LogStr Logging to a file. `BufSize` is a buffer size for each capability. File rotation is done on-demand.
LogFileTimedRotate |
Fields :: TimedFileLogSpec -> BufSize -> LogType' LogStr Logging to a file. `BufSize` is a buffer size for each capability. Rotation happens based on check specified in `TimedFileLogSpec`.
LogCallback |
Fields :: forall a. (a -> IO ()) -> IO () -> LogType' a Logging with a log and flush action. run flush after log each message.

type LogType = LogType' LogStr #

data FileLogSpec #

The spec for logging files

Constructors

FileLogSpec |
Fields log_file :: FilePath log_file_size :: Integer Max log file size (in bytes) before requiring rotation. log_backup_number :: Int Max number of rotated log files to keep around before overwriting the oldest one.

# Utilities

showSockAddr :: SockAddr -> NumericAddress #

Convert `SockAddr` to `NumericAddress`. If the address is IPv4-embedded IPv6 address, the IPv4 is extracted.

logCheck :: LogType -> IO () #

# Backward compability

clockDateCacher :: IO (DateCacheGetter, DateCacheUpdater) #

Returning `DateCacheGetter` and `DateCacheUpdater`.

Note: Since version 2.1.2, this function uses the auto-update package internally, and therefore the `DateCacheUpdater` value returned need not be called. To wit, the return value is in fact an empty action.

type ZonedDate = FormattedTime #

A type for zoned date.

type DateCacheGetter = IO ZonedDate #

Getting cached `ZonedDate`.

type DateCacheUpdater = IO () #

Updateing cached `ZonedDate`. This should be called every second. See the source code of `withStdoutLogger`.