packages feed

filter-logger 0.5.0.0 → 0.6.0.0

raw patch · 3 files changed

+59/−16 lines, 3 filesdep +ansi-terminalPVP ok

version bump matches the API change (PVP)

Dependencies added: ansi-terminal

API changes (from Hackage documentation)

Files

filter-logger.cabal view
@@ -1,5 +1,5 @@ name:                filter-logger-version:             0.5.0.0+version:             0.6.0.0 synopsis:            Filterable request logging wai middleware. Change how data is logged and when. description:         Composable filters to transform objects and control when they are written to server logs. homepage:            https://github.com/caneroj1/filter-logger#readme@@ -17,9 +17,11 @@   hs-source-dirs:      src   exposed-modules:     Network.Wai.Middleware.FilterLogger                      , Network.Wai.Middleware.FilterLogger.Internal+  other-modules:       Network.Wai.Middleware.FilterLogger.Colorizer   build-depends:       base >= 4.6 && < 5                      , aeson                      , aeson-pretty+                     , ansi-terminal                      , bytestring                      , data-default                      , fast-logger
+ src/Network/Wai/Middleware/FilterLogger/Colorizer.hs view
@@ -0,0 +1,33 @@+module Network.Wai.Middleware.FilterLogger.Colorizer where++import           Data.ByteString           (ByteString, pack)+import           Data.Char+import           Data.Monoid+import           System.Console.ANSI+import           System.Console.ANSI.Codes+import           System.Console.ANSI.Types++toBS :: String -> ByteString+toBS = pack . map (fromIntegral . ord)++colored :: Color -> ByteString -> ByteString+colored c bs = colorCode <> bs <> reset+  where colorCode = toBS $ setSGRCode [SetColor Foreground Vivid c]++reset :: ByteString+reset = toBS $ setSGRCode []++red :: ByteString -> ByteString+red = colored Red++blue :: ByteString -> ByteString+blue = colored Blue++yellow :: ByteString -> ByteString+yellow = colored Yellow++green :: ByteString -> ByteString+green = colored Green++cyan :: ByteString -> ByteString+cyan = colored Cyan
src/Network/Wai/Middleware/FilterLogger/Internal.hs view
@@ -19,24 +19,25 @@ import           Control.Monad import           Data.Aeson import           Data.Aeson.Encode.Pretty-import           Data.ByteString                      (ByteString)-import qualified Data.ByteString                      as BS hiding (ByteString)-import           Data.ByteString.Builder              (Builder)-import qualified Data.ByteString.Lazy                 as BL (ByteString,-                                                             fromStrict,-                                                             toStrict)+import           Data.ByteString                               (ByteString)+import qualified Data.ByteString                               as BS hiding (ByteString)+import           Data.ByteString.Builder                       (Builder)+import qualified Data.ByteString.Lazy                          as BL (ByteString,+                                                                      fromStrict,+                                                                      toStrict) import           Data.Char import           Data.Default import           Data.Semigroup-import           Data.Time.Clock                      (NominalDiffTime)+import           Data.Time.Clock                               (NominalDiffTime) import           Data.Word import           Network.HTTP.Types.Status import           Network.Wai import           Network.Wai.Logger+import           Network.Wai.Middleware.FilterLogger.Colorizer import           Network.Wai.Middleware.RequestLogger import           System.IO.Unsafe import           System.Log.FastLogger-import           Text.Printf                          (printf)+import           Text.Printf                                   (printf)  -- | Options for controlling log filtering. data FilterOptions = FilterOptions {@@ -123,17 +124,14 @@ buildLog :: Bool -> MyOutputFormatter buildLog detail date req status responseSize time builder body = logString detail   where-    toBS   = BS.pack . map (fromIntegral . ord)-     dfromRational :: Rational -> Double     dfromRational = fromRational      inMS   = printf "%.2f" . dfromRational $ toRational time * 1000 -    header = rawPathInfo req    <>-             rawQueryString req <> "\n" <>-             date               <> "\n" <>-             toBS (show . fromIntegral $ statusCode status) <> " - " <> statusMessage status <> "\n"+    header = colorizedUrl        <> "\n" <>+             date                <> "\n" <>+             colorizedStatusCode <> "\n"      buildRespSize (Just s) = "Response Size: " <> toBS (show s) <> "\n"     buildRespSize Nothing  = ""@@ -145,9 +143,19 @@      formattedBody       | BS.null body = body-      | otherwise    = body <> "\n"+      | otherwise    = yellow $ body <> "\n"      logString detailed = toLogStr (       header              <>       buildDetails detail <>       formattedBody)++    colorizedUrl = cyan $ rawPathInfo req <> rawQueryString req++    colorizedStatusCode+      | code < 300 = green str+      | code < 400 = yellow str+      | otherwise  = red str+      where str = toBS (show code) <> " " <> codeMsg+            code = statusCode status+            codeMsg = statusMessage status