What is the most pythonic way of logging for multiple modules and multiple handlers with specified encoding?

后端 未结 2 1534
半阙折子戏
半阙折子戏 2021-01-13 17:57

I\'m looking for concrete advice about how the multiple module and multiple handler logging should be done. I have added my simplified code here, but I don\'t want to bias t

2条回答
  •  别那么骄傲
    2021-01-13 18:31

    To kinda wrap this up, here is what I did; I've put the following two lines in every module/file:

    import logging
    logger = logging.getLogger(__name__)
    

    This sets up the logging, but doesn't add any handlers. I then add handlers to the root logger in the main file I'm running the imported modules therefore pass their records to the root logger and everything gets saved and displayed. I do it like CaptainMurphy suggested, but with logger = logging.getLogger('') to work with the root logger

    To address encoding - I had problems saving non-ascii strings to file. So I just added FileHandler to the root logger, where encoding can be specified. I couldn't do it with logging.basicConfig.

    Thank you again @CaptainMurphy - I'm sorry I can't upvote you with my low reputation, but I've checkmarked the answer.

提交回复
热议问题