Changing logging's 'basicConfig' which is already set

后端 未结 1 614
终归单人心
终归单人心 2020-12-17 07:34

I am using the logging module in python as:

import logging, sys
logger= logging.getLogger(__file__)
logging.basicConfig(stream = sys.stderr, level=logging.D         


        
相关标签:
1条回答
  • 2020-12-17 08:18

    If you look in the Python sources for logging/__init__.py, you'll see that basicConfig() sets the handlers on the root logger object by calling addHandler(). If you want to start from scratch, you could remove all existing handlers and then call basicConfig() again.

    # Example to remove all root logger handlers and reconfigure. (UNTESTED)
    import logging
    
    # Remove all handlers associated with the root logger object.
    for handler in logging.root.handlers[:]:
        logging.root.removeHandler(handler)
    
    # Reconfigure logging again, this time with a file.
    logging.basicConfig(filename = 'myfile.log', level=logging.DEBUG, format='%(filename)s:%(lineno)s %(levelname)s:%(message)s')
    
    0 讨论(0)
提交回复
热议问题