web_server: Log requests to WSGI app

- This is quite useful for debugging even on production machines.

- CherryPy can't be used for logging as grafting a WSGI application bypasses the
usual mechanisms of logging.

- Keep requests for static files turned off in CherryPy as these are not very
useful.

Tests:

- Making a request print an INFO message on the log with method and path after
the /freedombox part. Logs can be seen in systemd journal.

- Requests for static files are not logged.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2026-02-17 12:38:52 -08:00 committed by James Valleroy
parent 854916c54c
commit 0d579012d7
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808

View File

@ -72,8 +72,19 @@ def init():
'engine.autoreload.match': AUTORELOAD_REGEX,
})
def _logging_middleware(application):
"""A WSGI middleware to log messages before executing them."""
def _wrapper(environ, start_response):
"""Log request, then hand control to original app."""
logger.info("%s %s", environ['REQUEST_METHOD'],
environ['PATH_INFO'])
return application(environ, start_response)
return _wrapper
application = web_framework.get_wsgi_application()
cherrypy.tree.graft(application, cfg.server_dir)
cherrypy.tree.graft(_logging_middleware(application), cfg.server_dir)
static_dir = os.path.join(cfg.file_root, 'static')
_mount_static_directory(static_dir, web_framework.get_static_url())