on master help sign in
CustomOutputFormat
commit335140d4de36b0b3897780f705b7c22d08c177f6 authorMichael Snoyman <michael@snoyman.com> dateMon 31 Dec 2012, 17:27 (14 years ago) parent1071869
2 files changed+24−13
wai-extra/Network/Wai/Middleware/RequestLogger.hs+23−12 wai-extra/wai-extra.cabal+1−1
wai-extra/Network/Wai/Middleware/RequestLogger.hs+23−12
@@ -12,6 +12,7 @@ module Network.Wai.Middleware.RequestLogger
1212 , autoFlush
1313 , destination
1414 , OutputFormat (..)
15+ , OutputFormatter
1516 , Destination (..)
1617 , Callback
1718 , IPAddrSource (..)
@@ -46,6 +47,9 @@ import System.Log.FastLogger.Date (getDate, dateInit, ZonedDate)
4647
4748data OutputFormat = Apache IPAddrSource
4849 | Detailed Bool -- ^ use colors?
50+ | CustomOutputFormat OutputFormatter
51+
52+type OutputFormatter = ZonedDate -> Request -> Status -> Maybe Integer -> [LogStr]
4953
5054data Destination = Handle Handle
5155 | Logger Logger
@@ -81,27 +85,34 @@ mkRequestLogger RequestLoggerSettings{..} = do
8185 Callback c -> return (c, Nothing)
8286 case outputFormat of
8387 Apache ipsrc -> do
84− getdate <-
85− case mgetdate of
86− Just x -> return x
87−#if MIN_VERSION_fast_logger(0, 3, 0)
88− Nothing -> do
89− (getter,_) <- ondemandDateCacher zonedDateCacheConf
90− return getter
91−#else
92− Nothing -> fmap getDate dateInit
93−#endif
88+ getdate <- dateHelper mgetdate
9489 return $ apacheMiddleware callback ipsrc getdate
9590 Detailed useColors -> detailedMiddleware callback useColors
91+ CustomOutputFormat formatter -> do
92+ getdate <- dateHelper mgetdate
93+ return $ customMiddleware callback getdate formatter
9694 where
9795 fromLogger l = (loggerPutStr l, Just $ loggerDate l)
96+ dateHelper mgetdate = do
97+ case mgetdate of
98+ Just x -> return x
99+#if MIN_VERSION_fast_logger(0, 3, 0)
100+ Nothing -> do
101+ (getter,_) <- ondemandDateCacher zonedDateCacheConf
102+ return getter
103+#else
104+ Nothing -> fmap getDate dateInit
105+#endif
98106
99107apacheMiddleware :: Callback -> IPAddrSource -> IO ZonedDate -> Middleware
100−apacheMiddleware cb ipsrc getdate app req = do
108+apacheMiddleware cb ipsrc getdate = customMiddleware cb getdate $ apacheFormat ipsrc
109+
110+customMiddleware :: Callback -> IO ZonedDate -> OutputFormatter -> Middleware
111+customMiddleware cb getdate formatter app req = do
101112 res <- app req
102113 date <- liftIO getdate
103114 -- We use Nothing for the response size since we generally don't know it
104− liftIO $ cb $ apacheFormat ipsrc date req (responseStatus res) Nothing
115+ liftIO $ cb $ formatter date req (responseStatus res) Nothing
105116 return res
106117
107118-- | Production request logger middleware.
wai-extra/wai-extra.cabal+1−1
@@ -1,5 +1,5 @@
11Name: wai-extra
2−Version: 1.3.0.5
2+Version: 1.3.1
33Synopsis: Provides some basic WAI handlers and middleware.
44Description: The goal here is to provide common features without many dependencies.
55License: MIT