News for hackers. Stuff that matters.
Re:Logging in Python like a pro by paulfurtado on Friday July 01, 2022 @12:43PM (#31949111)
It's definitely sad that there isn't built-in context support, but if you're looking for a solution to this, it is actually very easy to attach context to logs using threadlocal and a logging filter.

When running a webserver though, an even simpler trick is to add middleware that sets the current request's info as the current thread's name, and then including threadName in your log format.

Re:Logging in Python like a pro by andrewingram on Friday July 01, 2022 @01:53PM (#31950121)
ContextVars are seemingly more robust and work with async. We use them quite extensively for adding context to logs.
Re:Logging in Python like a pro by nerdwaller on Friday July 01, 2022 @02:14PM (#31950460)
I was going to say the same, I combine ContextVars with LoggerAdapters[1].

[1] https://docs.python.org/3/library/logging.html#logging.Logge...

Re:Logging in Python like a pro by MapleWalnut on Friday July 01, 2022 @04:49PM (#31952663)
TIL that ContextVars can work in place of thread locals too. Thanks for pointing that out!

I had mistakenly assumed ContextVars only worked for async code.

Re:Logging in Python like a pro by memco on Friday July 01, 2022 @02:20PM (#31950554)
The contextual support is lacking but it can be done. See this comment from a previous discussion where I briefly described how I’ve used the record factory to help: https://news.ycombinator.com/item?id=31484527
Re:Logging in Python like a pro by 1337shadow on Friday July 01, 2022 @03:10PM (#31951249)
Take a look at pylogctx maybe it'll do what you need.