Network.Wai.Logger

wai-logger-2.2.4.1: A logging system for WAI

Safe Haskell | None
Language | Haskell2010

Network.Wai.Logger

Contents

- High level functions
- Creating a logger
- Types
- Date cacher
- Utilities

Description

Apache style logger for WAI applications.

An example:

    {-# LANGUAGE OverloadedStrings #-}
    module Main where

    import Blaze.ByteString.Builder (fromByteString)
    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 = fromByteString pong
        len = fromIntegral $ BS.length pong

Synopsis

- type ApacheLogger = Request -> Status -> Maybe Integer -> IO ()
- withStdoutLogger :: (ApacheLogger -> IO a) -> IO a
- data ApacheLoggerActions = ApacheLoggerActions {
  - apacheLogger :: ApacheLogger
  - logRotator :: IO ()
  - logRemover :: IO ()
  }
- initLogger :: IPAddrSource -> LogType -> DateCacheGetter -> IO ApacheLoggerActions
- data IPAddrSource
  - = FromSocket
  - | FromHeader
  - | FromFallback
- data LogType
  - = LogNone
  - | LogStdout BufSize
  - | LogFile FileLogSpec BufSize
  - | LogCallback (LogStr -> IO ()) (IO ())
- data FileLogSpec :: * = FileLogSpec {
  - log_file :: FilePath
  - log_file_size :: Integer
  - log_backup_number :: Int
  }
- clockDateCacher :: IO (DateCacheGetter, DateCacheUpdater)
- type ZonedDate = ByteString
- type DateCacheGetter = IO ZonedDate
- type DateCacheUpdater = IO ()
- logCheck :: LogType -> IO ()
- showSockAddr :: SockAddr -> NumericAddress

# 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.

# Creating a logger

data ApacheLoggerActions

Constructors

ApacheLoggerActions |
Fields apacheLogger :: ApacheLogger logRotator :: IO () This is obsoleted. Rotation is done on-demand. So, this is now an empty action. logRemover :: IO () Removing resources relating Apache logger. E.g. flushing and deallocating internal buffers.

initLogger :: IPAddrSource -> LogType -> DateCacheGetter -> IO ApacheLoggerActions

Creating `ApacheLogger` according to `LogType`.

# 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.
FromFallback | From the peer address if header is not found.

data LogType

Logger Type.

Constructors

LogNone | No logging.
LogStdout BufSize | Logging to stdout. `BufSize` is a buffer size for each capability.
LogFile FileLogSpec BufSize | Logging to a file. `BufSize` is a buffer size for each capability. File rotation is done on-demand.
LogCallback (LogStr -> IO ()) (IO ()) |

data FileLogSpec :: *

The spec for logging files

Constructors

FileLogSpec |
Fields log_file :: FilePath log_file_size :: Integer log_backup_number :: Int

# Date cacher

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 = ByteString

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`.

# Utilities

logCheck :: LogType -> IO ()

Checking if a log file can be written if `LogType` is `LogFile`.

showSockAddr :: SockAddr -> NumericAddress

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