Logging System
Relevant source files
The following files were used as context for generating this wiki page:
The Uvicorn logging system provides a structured way to capture and display server events, access requests, and ASGI application logs. It utilizes Python's standard logging module but extends it with custom formatters to provide colourized output, specialized access logs, and a unique TRACE log level for deep debugging.
The system is highly configurable, allowing users to provide their own logging dictionaries, file-based configurations (JSON, YAML, INI), or environment variables to control verbosity and output destinations.
Architecture and Core Components
The logging system is built around specialized loggers and formatters that differentiate between general server errors, HTTP/WebSocket access requests, and internal ASGI protocol events.
Logger Categories
| Logger Name | Purpose | Default Level |
|---|---|---|
uvicorn | Root logger for general server process events. | INFO |
uvicorn.error | Captures server-side errors and protocol-level warnings. | INFO |
uvicorn.access | Logs individual HTTP and WebSocket requests/connections. | INFO |
uvicorn.asgi | Used for detailed tracing of ASGI scope and message events. | TRACE |
Sources: [uvicorn/config.py:73-82](https://github.com/Kludex/uvicorn/blob/57b3991d5f6f8e2f750378be5eada7c4fedd90f9/uvicorn/config.py#L73-L82), [uvicorn/logging.py:100-103](https://github.com/Kludex/uvicorn/blob/57b3991d5f6f8e2f750378be5eada7c4fedd90f9/uvicorn/logging.py#L100-L103)
Custom Formatters
Uvicorn implements three primary formatters to handle different logging needs:
- ColourizedFormatter: The base class for Uvicorn's formatting logic. It adds ANSI color codes to log levels using the
clicklibrary and supports acolor_messageextra attribute for custom colored content. - DefaultFormatter: Inherits from
ColourizedFormatter, typically used for theuvicorn.errorlogger. It usessys.stderrfor color detection. - AccessFormatter: Specifically designed for the
uvicorn.accesslogger. It parses request arguments (client address, method, path, version, status) and applies color coding to HTTP status codes (e.g., green for 2xx, red for 4xx/5xx).
Sources: [uvicorn/logging.py:15-97](https://github.com/Kludex/uvicorn/blob/57b3991d5f6f8e2f750378be5eada7c4fedd90f9/uvicorn/logging.py#L15-L97)
The diagram shows the inheritance hierarchy of Uvicorn's custom log formatters derived from the Python standard library.
Logging Data Flow
Logging occurs at various stages of the request-response cycle. Protocol implementations (HTTP and WebSocket) interact with loggers to record lifecycle events.
Access Logging Flow
When an HTTP request is completed, the RequestResponseCycle uses the uvicorn.access logger to record the transaction.
Sources: [uvicorn/protocols/http/httptools_impl.py:284-292](https://github.com/Kludex/uvicorn/blob/57b3991d5f6f8e2f750378be5eada7c4fedd90f9/uvicorn/protocols/http/httptools_impl.py#L284-L292), [uvicorn/protocols/http/h11_impl.py:339-347](https://github.com/Kludex/uvicorn/blob/57b3991d5f6f8e2f750378be5eada7c4fedd90f9/uvicorn/protocols/http/h11_impl.py#L339-L347)
Trace Logging
Uvicorn introduces a TRACE level (value 5) which is lower than DEBUG (value 10). This level is used to log fine-grained events such as:
- Protocol connection made/lost.
- WebSocket upgrades.
- Internal ASGI message flow.
Sources: [uvicorn/logging.py:13](https://github.com/Kludex/uvicorn/blob/57b3991d5f6f8e2f750378be5eada7c4fedd90f9/uvicorn/logging.py#L13), [uvicorn/protocols/http/httptools_impl.py:114-123](https://github.com/Kludex/uvicorn/blob/57b3991d5f6f8e2f750378be5eada7c4fedd90f9/uvicorn/protocols/http/httptools_impl.py#L114-L123), [uvicorn/protocols/websockets/websockets_sansio_impl.py:117-120](https://github.com/Kludex/uvicorn/blob/57b3991d5f6f8e2f750378be5eada7c4fedd90f9/uvicorn/protocols/websockets/websockets_sansio_impl.py#L117-L120)
Configuration and Customization
The logging system is initialized via the Config class. Users can customize logging through several parameters.
Logging Parameters in Config
| Parameter | Type | Description |
|---|---|---|
log_config | dict, str, Path, ConfigParser | The logging configuration to use. Supports .json, .yaml, and .ini files. |
log_level | str, int | The minimum log level to capture (case-insensitive string or integer). |
access_log | bool | Whether to enable the access logger. Defaults to True. |
use_colors | bool | Force enable/disable color output. Defaults to None (auto-detect TTY). |
Sources: [uvicorn/config.py:165-171](https://github.com/Kludex/uvicorn/blob/57b3991d5f6f8e2f750378be5eada7c4fedd90f9/uvicorn/config.py#L165-L171), [uvicorn/config.py:326-368](https://github.com/Kludex/uvicorn/blob/57b3991d5f6f8e2f750378be5eada7c4fedd90f9/uvicorn/config.py#L326-L368)
Loading Logic
Uvicorn supports dynamic loading of logging configurations:
- Dictionaries: Applied using
logging.config.dictConfig. - JSON Files: Loaded and applied as a dictionary.
- YAML Files: Requires
PyYAML. Loaded and applied as a dictionary. - INI Files: Applied using
logging.config.fileConfig.
Sources: [uvicorn/config.py:328-356](https://github.com/Kludex/uvicorn/blob/57b3991d5f6f8e2f750378be5eada7c4fedd90f9/uvicorn/config.py#L328-L356)
Sources: [uvicorn/config.py:326-368](https://github.com/Kludex/uvicorn/blob/57b3991d5f6f8e2f750378be5eada7c4fedd90f9/uvicorn/config.py#L326-L368)
Protocol-Specific Logging Implementation
HTTP Protocols (H11 and HttpTools)
Both implementations log connection states if the log level is set to TRACE. They also handle access logging within their respective cycle classes.
- Connection Made: Logs
"%s:%d - HTTP connection made". - WebSocket Upgrade: Logs
"%s:%d - Upgrading to WebSocket". - Error 400: Logs
"Invalid HTTP request received."as a warning.
Sources: [uvicorn/protocols/http/httptools_impl.py:118-144](https://github.com/Kludex/uvicorn/blob/57b3991d5f6f8e2f750378be5eada7c4fedd90f9/uvicorn/protocols/http/httptools_impl.py#L118-L144), [uvicorn/protocols/http/h11_impl.py:94-100](https://github.com/Kludex/uvicorn/blob/57b3991d5f6f8e2f750378be5eada7c4fedd90f9/uvicorn/protocols/http/h11_impl.py#L94-L100)
WebSocket Protocols
WebSocket implementations (like websockets-sansio) log similar lifecycle events:
- Connection Made/Lost: Logged at
TRACElevel. - Handshake Acceptance: The
uvicorn.accesslogger logs the result of the handshake (e.g.,"WebSocket /ws" [accepted]).
Sources: [uvicorn/protocols/websockets/websockets_sansio_impl.py:117-133](https://github.com/Kludex/uvicorn/blob/57b3991d5f6f8e2f750378be5eada7c4fedd90f9/uvicorn/protocols/websockets/websockets_sansio_impl.py#L117-L133), [uvicorn/protocols/websockets/websockets_sansio_impl.py:317-321](https://github.com/Kludex/uvicorn/blob/57b3991d5f6f8e2f750378be5eada7c4fedd90f9/uvicorn/protocols/websockets/websockets_sansio_impl.py#L317-L321)
Summary
Uvicorn's logging system is a robust wrapper around Python's standard logging, optimized for high-performance async serving. By separating error logging from access logging and providing detailed trace levels, it allows developers to maintain clear visibility into server health and application behavior while remaining flexible enough for production deployment customization.