@@ -1,11 +1,12 @@
11{-# LANGUAGE OverloadedStrings #-}
22module Network.Wai.Middleware.RequestLogger
33 ( logStdout
4− , logHandle
4+ , logCallback
55 , logStdoutDev
6+ , logCallbackDev
7+ -- * Deprecated
8+ , logHandle
69 , logHandleDev
7− , logStdoutDevLT
8− , logHandleDevLT
910 ) where
1011
1112import System.IO (stdout, hFlush)
@@ -15,37 +16,41 @@ import Control.Monad.IO.Class (liftIO)
1516import Network.Wai (Request(..), Middleware)
1617import System.Log.FastLogger
1718import Network.HTTP.Types as H
18−import qualified Data.Text.Lazy as LT
19−import qualified Data.Text.Encoding as TE
2019
2120import Network.Wai.Parse (parseRequestBody, lbsSink, fileName, Param, File)
2221import qualified Data.ByteString.Lazy as LBS
23−import System.IO (hPutStrLn, stderr)
2422
2523import qualified Data.Conduit as C
2624import qualified Data.Conduit.List as CL
2725
28−-- | like @logHandle@, but prints to 'stdout'
26+logHandle :: (BS.ByteString -> IO ()) -> Middleware
27+logHandle = logCallback
28+{-# DEPRECATED logHandle "Please use logCallback instead." #-}
29+logHandleDev :: (BS.ByteString -> IO ()) -> Middleware
30+logHandleDev = logCallbackDev
31+{-# DEPRECATED logHandleDev "Please use logCallbackDev instead." #-}
32+
33+-- | Production request logger middleware.
34+-- Implemented on top of "logCallback", but prints to 'stdout'
2935logStdout :: Middleware
3036logStdout = logHandle $ \bs -> hPutLogStr stdout [LB bs]
3137
32−-- | like @logHandleDev@, but prints to 'stdout'
38+-- | Development request logger middleware.
39+-- Implemented on top of "logCallbackDev", but prints to 'stdout'
3340--
34−-- Note that this flushes 'stdout' on each call to make it useful for
35−-- development. This is very inefficient for production use.
41+-- Flushes 'stdout' on each request, which would be inefficient in production use.
42+-- Use "logStdout" in production.
3643logStdoutDev :: Middleware
3744logStdoutDev = logHandleDev $ \bs -> hPutLogStr stdout [LB bs] >> hFlush stdout
3845
39−-- FIXME This is not appropriately named at all. It's not working on a Handle,
40−-- it's working on a function. I find the functions in this module to be very
41−-- confusing.
42−-- - Michael
43−
4446-- | Prints a message using the given callback function for each request.
4547-- Designed for fast production use at the expense of convenience.
4648-- In particular, no POST parameter information is currently given
47−logHandle :: (BS.ByteString -> IO ()) -> Middleware
48−logHandle cb app req = do
49+--
50+-- This is lower-level - use "logStdout" unless you need this greater control
51+logCallback :: (BS.ByteString -> IO ()) -- ^ A function that logs the ByteString log message.
52+ -> Middleware
53+logCallback cb app req = do
4954 liftIO $ cb $ BS.concat
5055 [ requestMethod req
5156 , " "
@@ -61,28 +66,14 @@ logHandle cb app req = do
6166toBS :: H.Ascii -> BS.ByteString
6267toBS = id
6368
64−-- FIXME: What's the purpose of this function? Why would it ever be preferable
65−-- to logStdoutDev? And why is it implemented via unpack instead of using a
66−-- more efficient Data.Text.IO function?
67−-- - Michael
68−
69−-- | Inefficient, but convenient Development load logger middleware
70−-- Prints a message to 'stderr' for each request using logHandleDevLT
71−logStdoutDevLT :: Middleware
72−logStdoutDevLT = logHandleDevLT $ hPutStrLn stderr . LT.unpack
73−
74−-- | logHandleDev, but expects Lazy Text instead of a ByteString
75−logHandleDevLT :: (LT.Text -> IO ()) -> Middleware
76−logHandleDevLT cb app req =
77− logHandleDev (\msg -> cb $ LT.fromStrict $ TE.decodeUtf8 msg) app req
78−
7969-- | Prints a message using the given callback function for each request.
8070-- This is not for serious production use- it is inefficient.
8171-- It immediately consumes a POST body and fills it back in and is otherwise inefficient
8272--
83−-- Note that this function will not flush output.
84−logHandleDev :: (BS.ByteString -> IO ()) -> Middleware
85−logHandleDev cb app req = do
73+-- This is lower-level - use "logStdoutDev" unless you need this greater control
74+logCallbackDev :: (BS.ByteString -> IO ()) -- ^ A function that logs the ByteString log message.
75+ -> Middleware
76+logCallbackDev cb app req = do
8677 body <- requestBody req C.$$ CL.consume
8778 postParams <- if any (requestMethod req ==) ["GET", "HEAD"]
8879 then return []
@@ -101,7 +92,7 @@ logHandleDev cb app req = do
10192 , paramsToBS "POST " postParams
10293 , "\n"
10394 ]
104− -- we just consumed the body- fill the enumerator back up so it is available again
95+ -- The body was consumed. Fill it back up so it is available again
10596 app req { requestBody = CL.sourceList body }
10697 where
10798 paramsToBS prefix params =