uvicorn icon indicating copy to clipboard operation
uvicorn copied to clipboard

feat(logging): add customizable access log format for Gunicorn style …

Open ZaneL1u opened this issue 3 months ago • 1 comments

Summary

This PR adds support for Gunicorn-style customizable access log formatting to Uvicorn, addressing issue #527 .

Users can now specify custom access log formats using the --access-log-format CLI option or the access_log_format parameter in the Python API, supporting 20+ format atoms compatible with Gunicorn's specification.

Changes

  • New GunicornAccessFormatter class in uvicorn/logging.py

    • Supports 20+ Gunicorn format atoms (h, l, u, t, r, m, U, q, H, s, B, b, f, a, T, D, M, L, p)
    • Basic Authentication username extraction
    • Request header interpolation via %({header}i)s syntax
    • Response time tracking in multiple units
  • CLI option --access-log-format

    • Example: uvicorn app:app --access-log-format '%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"'
  • Python API parameter access_log_format

    • Added to Config class and run() function
  • Protocol integration

    • Modified h11_impl.py and httptools_impl.py to track response time and size
    • Automatic formatter detection (GunicornAccessFormatter vs AccessFormatter)

Checklist

  • [x] I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • [x] I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • [x] I've updated the documentation accordingly.

ZaneL1u avatar Nov 08 '25 21:11 ZaneL1u

@ZaneL1u

Can't wait to see this merged 💪 .

This would be nice for people running Gunicorn with Uvicorn workers.

Will it be in that way ? Or "just" applying Gunicorn logging style to Uvicorn ?

laurent-brisbois avatar Dec 04 '25 00:12 laurent-brisbois