Skip to content

Log request and exceptions for handlers that use escapeHttp and terminateConnection #94

Description

@HeinrichApfelmus

When using escapeHttp or terminateConnection to serve a request, the Snap HTTP server will not make any attempt to log the request, or any exception that the handler may throw.

I would prefer if Snap logs the requested path and any exception that the handler might throw. I found this useful in my Threepenny-GUI project. See also HeinrichApfelmus/threepenny-gui#145 .


Here is a minimal complete test case for the reported behavior:

$ cat SnapTest.hs
{-# LANGUAGE OverloadedStrings #-}

import qualified Data.ByteString.Char8  as BS   (hPutStrLn)
import qualified Control.Exception      as E
import           Snap.Core              as Snap
import qualified Snap.Http.Server       as Snap
import           System.IO                      (stderr)

config = Snap.setErrorLog  (Snap.ConfigIoLog $ BS.hPutStrLn stderr)
       $ Snap.setAccessLog (Snap.ConfigIoLog $ BS.hPutStrLn stderr)
       $ Snap.defaultConfig

main = Snap.httpServe config $ route
  [ ("escape"   , Snap.escapeHttp $ \_ _ _ -> E.throw msg)
  , ("terminate", Snap.terminateConnection msg)]

msg = E.AssertionFailed "Hello?"

Request issued

$ curl http://0.0.0.0:8000
<!DOCTYPE html>
<html>
<head>
<title>Not found</title>
</head>
<body>
<code>No handler accepted "/"</code>
</body></html>
$ curl http://0.0.0.0:8000/escape

curl: (52) Empty reply from server
$ curl http://0.0.0.0:8000/terminate

curl: (52) Empty reply from server

and the corresponding log

$ runhaskell SnapTest.hs 
no port specified, defaulting to port 8000
Listening on http://0.0.0.0:8000
127.0.0.1 - - [07/Dec/2016:23:04:54 +0100] "GET / HTTP/1.1" 404 198 - "curl/7.30.0"

The second and third request have not generated any log entry.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions