MyNixOS website logo
Description

Filterable request logging wai middleware. Change how data is logged and when.

Composable filters to transform objects and control when they are written to server logs.

filter-logger

HackageBuild Status

Filterable request logging as a wai middleware. Change what data is logged and when.

Usage

Here is one example from the examples/password directory where we use the filter logger to implement password filtering. The example uses scotty for our web server, and we use the logShowJSON and logFilterJSON helper functions to help us create our instances.

Password Filtering


{-# LANGUAGE DeriveGeneric     #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards   #-}

module Main where

import           Data.Aeson
import           GHC.Generics
import           Network.Wai.Middleware.FilterLogger
import           Web.Scotty

data LoginRequest = LoginRequest {
    username :: String
  , password :: String
  } deriving (Generic)

instance FromJSON LoginRequest where
instance ToJSON LoginRequest where

instance LogShowable LoginRequest where
  logShow = logShowJSON

instance LogFilterable LoginRequest where
  prep = logFilterJSON

instance Loggable LoginRequest where

main :: IO ()
main = scotty 3000 $ do
  middleware filterPasswords
  post "/" $ text "SUCCESS"

filterPasswords =
  mkDefaultFilterLogger hidePasswords
  where hidePasswords r@LoginRequest{..} = Just r {password = "*****"}

Sending a POST request to localhost:3000 with a body like

{
  "username": "test-username",
  "password": "myPassw0rd123"
}

will result in a log message like

11/Jul/2017:21:48:20 -0400
200 - OK
0.03ms
{
    "username": "test-username",
    "password": "*****"
}

Chaining Filters

Here is a rather contrived example showing that you can chain these filters together easily and do all sorts of filtering.


{-# LANGUAGE OverloadedStrings #-}

module Main where

import           Control.Monad
import qualified Data.ByteString                     as BS (elem, length)
import           Data.Char
import           Data.Word
import           Network.Wai.Middleware.FilterLogger
import           Web.Scotty

main :: IO ()
main = scotty 3000 $ do
  middleware filteringMiddleware
  post "/" $ text "SUCCESS"

filteringMiddleware =
  mkDefaultFilterLogger (keepShortBodies >=> containing 'c')
  where keepShortBodies bs
          | BS.length bs < 10 = Just bs
          | otherwise         = Nothing
        containing c bs
          | BS.elem (fromIntegral $ ord c) bs = Just bs
          | otherwise                         = Nothing

Sending a POST request to localhost:3000 with a body like

abcdefghi

will result in a log message like

11/Jul/2017:22:00:59 -0400
200 - OK
0.03ms
abcdefghi

If you send a POST request with a body like

abcdefghij

or

abdefghij

you won't see anything in the server logs.

Customization

Customizing the log filtering middleware can be done with the FilterOptions type. It supports two options currently:

  • Detailed logging: Includes the request duration in milliseconds and the response body size. True by default.
  • Log when the request body is empty: Boolean value indicating whether or not to create log messages when there is no request body. True by default.
Metadata

Version

0.6.0.0

License

Executables (2)

  • bin/filter-logger-password-exe
  • bin/filter-logger-basic-exe

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows