{"id":8843,"library":"asgi-logger","title":"ASGI Logger Middleware","description":"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.","status":"active","version":"0.1.0","language":"en","source_language":"en","source_url":"https://github.com/Kludex/asgi-logger","tags":["asgi","logging","middleware","uvicorn","fastapi"],"install":[{"cmd":"pip install asgi-logger","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Commonly used ASGI framework for quickstart examples.","package":"fastapi","optional":true},{"reason":"Commonly used ASGI server, often paired with asgi-logger. Its default access logger needs to be disabled when using asgi-logger.","package":"uvicorn","optional":true}],"imports":[{"symbol":"AccessLoggerMiddleware","correct":"from asgi_logger import AccessLoggerMiddleware"}],"quickstart":{"code":"import logging\nfrom fastapi import FastAPI\nfrom fastapi.middleware import Middleware\nfrom asgi_logger import AccessLoggerMiddleware\nimport uvicorn\n\n# Instantiate FastAPI app\napp = FastAPI(\n    middleware=[\n        Middleware(AccessLoggerMiddleware, format='%(client_addr)s - \"%(request_line)s\" %(status_code)s')\n    ]\n)\n\n@app.get(\"/hello\")\nasync def home():\n    return {\"message\": \"Hello world!\"}\n\n# Important: Disable Uvicorn's default access logger to avoid duplicate logs\nlogging.getLogger(\"uvicorn.access\").handlers = []\n\nif __name__ == \"__main__\":\n    # To run, save this as main.py and execute: uvicorn main:app --port 8000\n    print(\"Navigate to http://127.0.0.1:8000/hello\")\n    uvicorn.run(app, host=\"127.0.0.1\", port=8000)\n","lang":"python","description":"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."},"warnings":[{"fix":"Add `logging.getLogger(\"uvicorn.access\").handlers = []` to your application's startup code.","message":"When using `asgi-logger` with Uvicorn, you must explicitly disable Uvicorn's default access logger to avoid duplicate log entries for each request.","severity":"gotcha","affected_versions":"All versions (0.1.0)"},{"fix":"Ensure you are installing `asgi-logger` and importing `AccessLoggerMiddleware` from `asgi_logger`.","message":"The 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.","severity":"breaking","affected_versions":"Prior to 0.1.0"},{"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.","message":"There 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.","severity":"gotcha","affected_versions":"0.1.0"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"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.","cause":"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.","error":"TypeError: a bytes-like object is required, not 'str' (often seen with Flask's app.logger or other non-ASGI native loggers)"},{"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.","cause":"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.","error":"ERROR:    Exception in ASGI application\nTraceback (most recent call last):\n... (KeyError: 'status')"},{"fix":"Disable Uvicorn's default access logger by adding `logging.getLogger(\"uvicorn.access\").handlers = []` to your application's entry point, before Uvicorn starts.","cause":"This is a common issue when both `asgi-logger` and Uvicorn's default access logger are active simultaneously.","error":"Logs appear twice in the console."}]}