haskellhaskell-wai

Setup Wai Middleware request logger to drop (not log) some requests


I have an app where the Nginx reverse proxy is making a lot of requests to my health endpoint. I'd like to not log these at all so my output logs are smaller. I'm also logging everything as JSON using Network.Wai.Middleware.RequestLogger.JSON, which has a function to format log messages as JSON.

One thing I could do is log an empty bytestring, but I was thinking there may be some way to no-op the log call. I can't figure out how to do that from looking at the various RequestLogger functions in wai-extra.

Does anyone have a recommendation for how to build a custom Middleware in order to not-log certain requests?


Solution

  • I created a custom formatter in the following way:

    -- | Wai Application Middleware logger
    jsonRequestLogger :: IO Middleware
    jsonRequestLogger = mkRequestLogger
        $ def { outputFormat = CustomOutputFormatWithDetails dontLogHealthEndpoint }
    
    
    dontLogHealthEndpoint :: OutputFormatterWithDetails
    dontLogHealthEndpoint date req status responseSize duration reqBody response =
        if B.isInfixOf "health" $ rawPathInfo req
            then toLogStr B.empty
            else formatAsJSON date req status responseSize duration reqBody response
    

    This seems to work fine. However, I'd still like to know if there's a better way.