Python logging multiple files using the same logger

前端 未结 3 999
难免孤独
难免孤独 2020-12-15 08:26

This is my scenario: I want to log my_module\'s activity. This needs to be done, depending on the method executed (let\'s say, INPUT and OUTPUT), to two different files.

3条回答
  •  情歌与酒
    2020-12-15 08:48

    You should instantiate an Handler for each destination you want to send your log to, then add the 2 handlers to your logger. The following should work (didn't test it though):

    logger = logging.getLogger()
    handler1 = logging.TimedRotatingFileHandler()
    handler2 = logging.TimedRotatingFileHandler()
    logger.addHandler(handler1)
    logger.addHandler(handler2)
    

    Of course add all your configuration and formatting options you may need. Basically it is just to show you that when you instantiate the logging handler you can add it to the logger. From that moment on, your log records will be emitted to every handler added to the logger.

提交回复
热议问题