Logging to two files with different settings
Question: I am already using a basic logging config where all messages across all
modules are stored in a single file. However, I need a more complex solution
now:

  * Two files: the first remains the same.
  * The second file should have some custom format.

I have been reading the docs for the module, bu they are very complex for me
at the moment. Loggers, handlers...

So, in short:

How to log to two files in Python 3, ie:

    
    
    import logging
    # ...
    logging.file1.info('Write this to file 1')
    logging.file2.info('Write this to file 2')
    


Answer: You can do something like this:

    
    
    import logging
    formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
    
    # first file logger
    logger_1 = logging.getLogger('simple_logger')
    hdlr_1 = logging.FileHandler('simplefile_1.log')
    hdlr_1.setFormatter(formatter)
    logger_1.addHandler(hdlr_1)
    
    # second file logger
    logger_2 = logging.getLogger('simple_logger_2')
    hdlr_2 = logging.FileHandler('simplefile_2.log')    
    hdlr_2.setFormatter(formatter)
    logger_2.addHandler(hdlr_2)
    
    logger_1.info('message 1')
    logger_2.error('error foo')
    

