Skip to content

Change access log atom %({...}e)s to read ASGI scope #359

@TomiBelan

Description

@TomiBelan

This is a minor enhancement/feature request.

Hypercorn's access log configuration (https://hypercorn.readthedocs.io/en/latest/how_to_guides/logging.html) is inspired by Gunicorn (https://gunicorn.org/reference/settings/#access_log_format or https://github.com/benoitc/gunicorn/blob/9bc5891b4b06f25a8ce0e707053dcb2fb9bf638c/gunicorn/config.py#L1417-L1442).

But {...}e, the "environment variable" atom, is very different:
In Hypercorn, it reads Operating System environment variables (os.environ).
In Gunicorn, it reads WSGI environment variables (the WSGI environ dict).
I suspect you misunderstood the Gunicorn docs when you implemented it...

The cool thing about Gunicorn's version is that the WSGI app itself, while it's running, can add more info to the WSGI environ. This info can then become part of the access log line. For example, you could log some app-specific Trace ID, user/auth info, or some details about the response beyond just HTTP status code.

My feature request is that {...}e in Hypercorn should read from ASGI scope. If scope["myapp.foo"] has type str or bytes, then %({myapp.foo}e)s should log it.

This would match Gunicorn more closely, and is more useful than reading OS environment variables (they're effectively constant for all requests). Alternatively, if you don't want to make a breaking change, and you don't care about consistency with Gunicorn, then adding a new atom for ASGI scope variables would work too.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions