on master help sign in
CustomOutputFormat
335140dMichael Snoyman
wai-extra/Network/Wai/Middleware/RequestLogger.hs+23−12
@@ -12,6 +12,7 @@ module Network.Wai.Middleware.RequestLogger
12 , autoFlush
12 , autoFlush
13 , destination
13 , destination
14 , OutputFormat (..)
14 , OutputFormat (..)
15+ , OutputFormatter
15 , Destination (..)
16 , Destination (..)
16 , Callback
17 , Callback
17 , IPAddrSource (..)
18 , IPAddrSource (..)
@@ -46,6 +47,9 @@ import System.Log.FastLogger.Date (getDate, dateInit, ZonedDate)
46
47
47data OutputFormat = Apache IPAddrSource
48data OutputFormat = Apache IPAddrSource
48 | Detailed Bool -- ^ use colors?
49 | Detailed Bool -- ^ use colors?
50+ | CustomOutputFormat OutputFormatter
51+
52+type OutputFormatter = ZonedDate -> Request -> Status -> Maybe Integer -> [LogStr]
49
53
50data Destination = Handle Handle
54data Destination = Handle Handle
51 | Logger Logger
55 | Logger Logger
@@ -81,27 +85,34 @@ mkRequestLogger RequestLoggerSettings{..} = do
81 Callback c -> return (c, Nothing)
85 Callback c -> return (c, Nothing)
82 case outputFormat of
86 case outputFormat of
83 Apache ipsrc -> do
87 Apache ipsrc -> do
84− getdate <-
88+ getdate <- dateHelper mgetdate
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
94 return $ apacheMiddleware callback ipsrc getdate
89 return $ apacheMiddleware callback ipsrc getdate
95 Detailed useColors -> detailedMiddleware callback useColors
90 Detailed useColors -> detailedMiddleware callback useColors
91+ CustomOutputFormat formatter -> do
92+ getdate <- dateHelper mgetdate
93+ return $ customMiddleware callback getdate formatter
96 where
94 where
97 fromLogger l = (loggerPutStr l, Just $ loggerDate l)
95 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
98
106
99apacheMiddleware :: Callback -> IPAddrSource -> IO ZonedDate -> Middleware
107apacheMiddleware :: 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
101 res <- app req
112 res <- app req
102 date <- liftIO getdate
113 date <- liftIO getdate
103 -- We use Nothing for the response size since we generally don't know it
114 -- 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
105 return res
116 return res
106
117
107-- | Production request logger middleware.
118-- | Production request logger middleware.
wai-extra/wai-extra.cabal+1−1
@@ -1,5 +1,5 @@
1Name: wai-extra
1Name: wai-extra
2−Version: 1.3.0.5
2+Version: 1.3.1
3Synopsis: Provides some basic WAI handlers and middleware.
3Synopsis: Provides some basic WAI handlers and middleware.
4Description: The goal here is to provide common features without many dependencies.
4Description: The goal here is to provide common features without many dependencies.
5License: MIT
5License: MIT