Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Can I add request-id into uvicorn.access log? #40

Closed
tienhm0202 opened this issue Apr 25, 2021 · 2 comments
Closed

Question: Can I add request-id into uvicorn.access log? #40

tienhm0202 opened this issue Apr 25, 2021 · 2 comments

Comments

@tienhm0202
Copy link

My log now is:

[2021-04-25 08:48:58 +0000] [13] [INFO] RID: ad303ddacea942b89c6f08e33b5f09be | CID: d68deb856ea84d4e8e7bc7c08bc511bd | s.m.users.business.controllers | An user is trying to log in our system

[2021-04-25 08:48:58 +0000] [13] [INFO] uvicorn.access | 172.21.0.1:64268 - "POST /v1/users/login HTTP/1.1" 200

I tried to add filter to log request-id and correlation-id to uvicorn.access but it raised starlette_context.errors.ContextDoesNotExistError.

Is there a way to add context data to uvicorn.access log?

@tienhm0202
Copy link
Author

Look like context is closed right in RawContextMiddleware - I will try another

@tomwojcik
Copy link
Owner

tomwojcik commented Apr 25, 2021

Yes, it's not possible to use context in uvicorn.

encode/uvicorn#859

You can see it's still changing. A few months ago the behavior you desire was there by default and now it's not even possible to add headers to logs if I read it correctly. If I were you I'd not rely on uvicorn logs AT ALL but rather add a proxy server where you will have total control over it and much more. Then, send logs to a centralized server.

So if you're willing to overengineer it:

  • add traeffic / caddy
  • add ELK or something else where you can browse logs

Or just use 0.13.0 where I believe the response scope was logged.


Note I'm not an expert on this topic. If you come up with a solution to your problem, please let me know. I might be wrong about app/server layers and maybe it's possible to glue them together 🤷🏻‍♂️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants