Registry / web-framework / asgi-logger

asgi-logger

JSON →
library0.1.0pypypi✓ verified 85d ago

asgi-logger is a middleware-based access logger designed for ASGI servers, offering an alternative to the default Uvicorn logger. It is compatible with any ASGI application and provides customizable log formats. The current version is 0.1.0, released in November 2021, with development continuing on GitHub though without frequent PyPI releases.

pip install asgi-logger
INSTALL
IMPORT
SIG · ASGI-LOGGER
A
asgi-logger
web-frameworkpythonv0.1.0
Install
1.6s avg
Import
51ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.1.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.055s · 18.3MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 1.6s · import 0.048s · 19MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

AccessLoggerMiddleware
from asgi_logger import AccessLoggerMiddleware

This example demonstrates integrating `asgi-logger` with a FastAPI application. It shows how to add the `AccessLoggerMiddleware` with a custom format and, crucially, how to disable Uvicorn's built-in access logger to prevent redundant log entries.

import logging from fastapi import FastAPI from fastapi.middleware import Middleware from asgi_logger import AccessLoggerMiddleware import uvicorn # Instantiate FastAPI app app = FastAPI( middleware=[ Middleware(AccessLoggerMiddleware, format='%(client_addr)s - "%(request_line)s" %(status_code)s') ] ) @app.get("/hello") async def home(): return {"message": "Hello world!"} # Important: Disable Uvicorn's default access logger to avoid duplicate logs logging.getLogger("uvicorn.access").handlers = [] if __name__ == "__main__": # To run, save this as main.py and execute: uvicorn main:app --port 8000 print("Navigate to http://127.0.0.1:8000/hello") uvicorn.run(app, host="127.0.0.1", port=8000)
Debug
Known issues
gotchaWhen using `asgi-logger` with Uvicorn, you must explicitly disable Uvicorn's default access logger to avoid duplicate log entries for each request.
fix
Add `logging.getLogger("uvicorn.access").handlers = []` to your application's startup code.
affects: All versions (0.1.0)
breakingThe project was originally named `uvicorn-logger` and was later renamed to `asgi-logger`. If you were using an extremely old version under the previous name, imports and package names will need updating.
fix
Ensure you are installing `asgi-logger` and importing `AccessLoggerMiddleware` from `asgi_logger`.
affects: Prior to 0.1.0
gotchaThere are known issues with `asgi-logger` sometimes duplicating the root path in logs, especially with Starlette 0.33+, and occasional `KeyError: 'status'` in the middleware. These are open issues on GitHub.
fix
Check the GitHub issues for potential workarounds or newer versions addressing these specific bugs. You might need to adjust your log parsing or consider alternative logging approaches if these are critical.
affects: 0.1.0
Errors
Common errors & fixes
TypeError: a bytes-like object is required, not 'str' (often seen with Flask's app.logger or other non-ASGI native loggers)
Attempting to use a WSGI-centric logger (like Flask's `app.logger` directly) in an ASGI context without proper adaptation, or conflicts with how different loggers handle byte vs. string output.
fix
Ensure all logging is configured to work within the asynchronous ASGI context, ideally using standard Python `logging` or an ASGI-aware logger. If using Flask with Uvicorn, consider using a wrapper or ensuring Flask's logger doesn't interfere.
ERROR: Exception in ASGI application Traceback (most recent call last): ... (KeyError: 'status')
An open issue indicates that the middleware can sometimes produce a `KeyError: 'status'`. This suggests an edge case where the 'status' key expected in the scope or event message is missing.
fix
This is an unresolved issue in the current version. Monitor the `asgi-logger` GitHub repository for updates or workarounds. For now, careful error handling around the middleware or a custom middleware to catch such `KeyError` might be needed.
Logs appear twice in the console.
This is a common issue when both `asgi-logger` and Uvicorn's default access logger are active simultaneously.
fix
Disable Uvicorn's default access logger by adding `logging.getLogger("uvicorn.access").handlers = []` to your application's entry point, before Uvicorn starts.
Upgrade
Version history
0.1.0latest on PyPI · released Nov 7, 2021
Audit
Dependencies
fastapioptionalCommonly used ASGI framework for quickstart examples.
uvicornoptionalCommonly used ASGI server, often paired with asgi-logger. Its default access logger needs to be disabled when using asgi-logger.
Agent activity
38 hits · last 30 days
node
34
OpenAI (training)
1
Resources
asgi-logger — pip install asgi-logger · libregistry