Per thread logging in python

前端 未结 5 1712
逝去的感伤
逝去的感伤 2021-01-13 05:03

Consider a multi threaded python application using python logger module. I want to do per thread logging, so I have appended the a unique-ID (not thread id) at the bottom of

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-13 05:34

    As far as I can tell, this is actually an intractable problem in python's logging system. You can kind of hack a solution if you have access to every place a new thread is created and can fiddle with logging controls via logging handlers and filters. But as soon as your code calls some lib/code somewhere that wants to do threading itself, this solution fails, at least in terms of where that lib's threads will log messages to.

    I asked a related question with more detail at

    How do I log to different files from different threads in python?

    and I also posted a comment exposing this issue to the blog by @Vinay Sajip mentioned above. I.e., as far as I can tell (and I could be wrong (!)), his solution does not solve the underlying issue here. Nothing really does unless some high level wizardry involving call stack tracing or somesuch is done to hack away at this basic shortcoming in threads + logging. The multiprocessing package does not solve this issue, either. Each of the Processes is still bound to the same root logger (which in my naivete, seems a bit odd/worrisome, btw.)

    There would be a real fix if a thread could be queried for its parent thread, but it cannot.

提交回复
热议问题