Configure/disable request logging in yesod

56 Views Asked by At

Using yesod 1.6.2.1, when creating a new application (regardless of whether using the scaffolding or using the builtin warp port foundation), you get request logs like this:

127.0.0.1 - - [18/Aug/2023:11:19:54 +0200] "GET / HTTP/1.1" 200 ...

I don't see where/how they're configured. The Yesod typeclass comes with a shouldLogIO member, but even if that's set to return False you still get these messages on each request. While these are mostly harmless, I'd at least like to disable them in tests because the mess up the test runner output.

What actually creates these log entries, and how do I configure/disable them?

1

There are 1 best solutions below

0
Cubic On BEST ANSWER

These logs come from a WAI middleware that the warp helper in yesod as well as the scaffolding create. There doesn't seem to be a central place to configure this by default, however it can be disabled by simply not including the middleware in question.

In the scaffolded site, this is in the withApp function in TestImport.hs:

withApp :: SpecWith (TestApp App) -> Spec
withApp = before $ do
    settings <- loadYamlSettings
        ["config/test-settings.yml", "config/settings.yml"]
        []
        useEnv
    foundation <- makeFoundation settings
    wipeDB foundation
    logWare <- liftIO $ makeLogWare foundation
    return (foundation, logWare)

simply replace (foundation, logWare) with (foundation, id) here (this works since WAI middlewares are simply functions of type Application -> Application).

Similarly, in the scaffolded site, you can just remove logWare from makeApplication in Application.hs:

makeApplication :: App -> IO Application
makeApplication foundation = do
    logWare <- makeLogWare foundation
    -- Create the WAI application and apply middlewares
    appPlain <- toWaiAppPlain foundation
    return $ logWare $ defaultMiddlewaresNoLogging appPlain

When not using the scaffolded site, to run the application without the default logging middleware just do something similar to the scaffolded site and convert your foundation to a WAI app via toWaiAppPlain, add the default middlwares without logging via defaultMiddlewaresNoLogging, and use Network.Wai.Handler.Warp.run to run the resulting WAI application with the warp server`.