Prerequisites
Python Version
3.12.3
FastAPI Version
0.115
Environment
Development
Current Behavior
When logging with extra context (using logging.Logger.extra or LoggerAdapter), the message text appears in both console and file outputs but the extra key/value data is not included. Example: logger.info("User action", extra={"user_id": "123", "meta": {"path": "/api"}}) produces only the message and standard fields in both console and file transports — the user_id/meta fields are missing.
Expected Behavior
Extra data passed in the log call should be included in both console and file handlers (either rendered by the formatter or included in structured/JSON output). For example, the console/file output should contain user_id and meta fields or the formatter should include them.
Steps To Reproduce
- Create a minimal FastAPI app (or run any script) with basic logging config and two handlers (console + file):
- Console handler using a text formatter
- File handler writing to app.log
- Log using the extra argument or a LoggerAdapter:
- logger.info("User action", "user_id": "123", "session": "abc")
- Observe console and app.log — extra fields are not present.
Error Logs
Text format including fields: 2025-11-08 13:00:00 INFO myapp: User action user_id=123 session=abc
Or JSON structured output with extra fields present
Error Logs No exception is thrown — just missing/omitted data in formatted output.
Additional Context
- Possible factors: formatter does not reference extra keys, using different formatters for handlers, uvicorn/FastAPI/third-party logging setup overriding handlers, or the use of a structured logger (structlog/loguru) without the correct processors/adapters.
- If using the logging "extra" mapping, note that format strings must include the specific keys (e.g., "%(user_id)s") or use a JSON formatter that automatically includes extra fields.
- If using python-json-logger, ensure you initialize it to include extra attributes (or use a logging adapter to merge them).
- If uvicorn is involved, it may have its own logger configuration; confirm the app logger is configured at startup and handlers are not duplicated or replaced.
Prerequisites
Python Version
3.12.3
FastAPI Version
0.115
Environment
Development
Current Behavior
When logging with extra context (using logging.Logger.extra or LoggerAdapter), the message text appears in both console and file outputs but the extra key/value data is not included. Example: logger.info("User action", extra={"user_id": "123", "meta": {"path": "/api"}}) produces only the message and standard fields in both console and file transports — the user_id/meta fields are missing.
Expected Behavior
Extra data passed in the log call should be included in both console and file handlers (either rendered by the formatter or included in structured/JSON output). For example, the console/file output should contain user_id and meta fields or the formatter should include them.
Steps To Reproduce
Error Logs
Text format including fields: 2025-11-08 13:00:00 INFO myapp: User action user_id=123 session=abc Or JSON structured output with extra fields present Error Logs No exception is thrown — just missing/omitted data in formatted output.Additional Context