Per thread logging in python

前端 未结 5 1715
逝去的感伤
逝去的感伤 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条回答
  •  半阙折子戏
    2021-01-13 05:26

    A potential solution would be to replace getLogger:

    import logging
    old_getlogger= logging.getLogger
    def my_getlogger(name):
        return old_getlogger( (str(someInt) if name==__name__ else "") + name)
    logging.getLogger= my_getlogger
    

    You'll need to make sure someInt has the correct value though, which may be hard, depending on the structure of your code. @Marek 's suggestion of using thread local storage may be useful for that.

提交回复
热议问题