Python logging multiple files using the same logger

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.


what you want is to

  1. create 2 NON ROOT loggers.
  2. make handler for each one, point to different file
  3. add handler to appropriate logger

    logger1 = logging.getLogger('general_logger')
    logger2 = logging.getLogger('some_other_logger')
    
    log_handler1 = logging.handlers.RotatingFileHandler(file_1, *args)
    log_handler2 = logging.handlers.RotatingFileHandler(file_2, *args)
    
    logger1.addHandler(log_handler1)
    logger2.addHandler(log_handler2)
    

then

    logger1.info("this will be logged to file_1 ")
    logger2.info("this will be logged to file_2 ")

Please note that if you create a ROOT logger and a different logger, root logger will log everything that this different controller is trying to log.

In other words, if

    root_logger = logging.getLogger()
    logger2 = logging.getLogger('some_other_logger')

    root_log_handler = logging.handlers.RotatingFileHandler(file_1, *args)
    log_handler2 = logging.handlers.RotatingFileHandler(file_2, *args)

    root_logger.addHandler(root_log_handler)
    logger2.addHandler(log_handler2)

then

    root_logger.info("this will be logged to file_1 ")
    logger2.info("this will be logged to file_1 AND file_2 ")