Skip to content

json-logging 1.7.0

Latest

Choose a tag to compare

@cvas22 cvas22 released this 23 Nov 05:16
08e3126

This release adds Connexion 3 AsyncApp support and an opt-in way to read the request body in FastAPI, and fixes a few long-standing logging bugs. There's one behaviour change for Flask and Connexion apps that's worth reading before you upgrade, see "Upgrading" below.

pip install json-logging==1.7.0

Added

  • Connexion 3 AsyncApp support. init_connexion() plus init_request_instrument(app) now work with an AsyncApp, without Flask. Call init_request_instrument(app) before the app starts. (#148, fixes #96)
  • FastAPI: init_request_instrument(..., read_request_body=True) reads the body before the handler runs and puts it on request.state.json_logging_request_body, so a custom request formatter can use it. It's off by default, the body is never logged automatically, and it needs Starlette 0.28 or newer. (#146, fixes #84)

Changed

  • With JSON logging enabled, unhandled exceptions from Flask and Connexion apps are now one JSON record with level and exc_info instead of a plain multi-line traceback. init_*() now converts handlers already attached to the root logger, and init_request_instrument(app) converts flask.logging.default_handler. (#147)

Fixed

  • Records logged with stack_info=True now keep the stack in a stack_info field. (#153, fixes #150)
  • An empty correlation ID header is treated as missing, so the next header is tried or an ID is generated when that's enabled. (#152, fixes #149)
  • get_request_logger() raises the documented RuntimeError instead of AttributeError when called before init_request_instrument(). (#139)
  • Request lookup no longer fails with RuntimeError: dictionary changed size during iteration under tracers and debuggers. (#120, fixes #89)

Upgrading

If you attach a root logger handler with your own formatter before calling init_*(), it will emit JSON after this upgrade. To keep a plain-text handler, add it after initialisation with an explicit logging.Formatter. If your log parsers or alerts match on the old multi-line Flask tracebacks, check them before rolling this out.

Default output is otherwise unchanged, apart from the new stack_info field (only for records that ask for it) and the empty-header change above.

Known limitations with Connexion 3 AsyncApp

  • The request log is written after the app call returns, so if a background task raises after the response is sent, the request log can be missing.
  • response_time_ms includes background task time.
  • Instrumenting an AsyncApp turns off the uvicorn.access logger for the whole process.
  • remote_user is - in AsyncApp request logs.

Tested on Python 3.9 to 3.14. Full details are in the changelog.