How to configure asgi-correlation with loguru ?
See original GitHub issueHello there !
Loguru seems to get more popularity as time goes by, I’ve tried to setup a very minimal example to get your library working with it, but it seems it can’t find the correlation id.
I’ve tried several things, and right now i’m doing this :
import logging
import sys
import uvicorn
from asgi_correlation_id import CorrelationIdMiddleware
from fastapi import FastAPI
from loguru import logger
app = FastAPI()
app.add_middleware(CorrelationIdMiddleware)
# remove default handler
logger.remove()
fmt = "[{time}] [application_name] [{extra[correlation_id]}] [{level}] - {name}:{function}:{line} - {message}"
logger.add(sys.stderr, format=fmt, level=logging.DEBUG)
@app.get('/')
def index():
logger.info(f"Request with id ")
return 'OK'
if __name__ == '__main__':
uvicorn.run(app)
And I get a :
Record was: {'elapsed': datetime.timedelta(seconds=1, microseconds=1880), 'exception': None, 'extra': {}, 'file': (name='another_main.py', path='C:\\Users\\User\\PycharmProjects\\x\\y\\another_main.py'), 'function': 'index', 'level': (name='INFO', no=20, icon='ℹ️'), 'line': 21, 'message': 'Request with id ', 'module': 'another_main', 'name': '__main__', 'process': (id=3412, name='MainProcess'), 'thread': (id=9164, name='asyncio_0'), 'time': datetime(2021, 11, 17, 15, 7, 54, 443182, tzinfo=datetime.timezone(datetime.timedelta(seconds=3600), 'Paris, Madrid'))}
Traceback (most recent call last):
File "C:\Users\User\PycharmProjects\x\venv\lib\site-packages\loguru\_handler.py", line 155, in emit
formatted = precomputed_format.format_map(formatter_record)
KeyError: 'correlation_id'
--- End of logging error ---
Got any idea ? Thanks !
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
snok/asgi-correlation-id: Request ID propagation for ASGI apps
To set up the package, you need to add the middleware and configure logging. Adding the middleware. The middleware can be added like...
Read more >A Complete Guide to Logging in Python with Loguru
Analyze, correlate and filter logs with SQL. Create actionable dashboards. Share and comment with built ...
Read more >How To Override Uvicorn Logger in FastAPI using Loguru
Uvicorn is a lightning-fast ASGI server implementation, using uvloop and httptools. Until recently Python has ... Easy to use and customize with loguru....
Read more >asgi-correlation-id 0.1.1 - PyPI
Middleware that propagates HTTP header correlation IDs to project logs. ... pip install asgi-correlation-id==0.1.1. Copy PIP instructions.
Read more >Switching from standard logging to loguru
The main difference is that standard logging requires the user to explicitly instantiate named Logger and configure them with Handler , Formatter and...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Wow, this works great! I wouldn’t have thought about this.
I just post this full example so people will know.
Thank you for your precious help and your time, I’ll close this now 😃
Here’s my example
输出如下: