storing logger messages in a string

后端 未结 4 955
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-02 10:00

I wanted to store all the intermediate log messages (warn, info, error) to a string in python and finally at the end of program, display everything as a report to the consol

4条回答
  •  借酒劲吻你
    2021-01-02 10:28

    It can be as simple as logging to a StringIO object:

    import logging
    try:
        from cStringIO import StringIO      # Python 2
    except ImportError:
        from io import StringIO
    
    log_stream = StringIO()    
    logging.basicConfig(stream=log_stream, level=logging.INFO)
    
    logging.info('hello world')
    logging.warning('be careful!')
    logging.debug("you won't see this")
    logging.error('you will see this')
    logging.critical('critical is logged too!')
    
    print(log_stream.getvalue())
    

    Output

    INFO:root:hello world
    WARNING:root:be careful!
    ERROR:root:you will see this
    CRITICAL:root:critical is logged too!
    
    

    If you want to log only those messages at levels WARN, INFO and ERROR you can do it with a filter. LevelFilter below checks each log record's level no, allowing only those records of the desired level(s):

    import logging
    try:
        from cStringIO import StringIO      # Python 2
    except ImportError:
        from io import StringIO
    
    class LevelFilter(logging.Filter):
        def __init__(self, levels):
            self.levels = levels
    
        def filter(self, record):
            return record.levelno in self.levels
    
    log_stream = StringIO()    
    logging.basicConfig(stream=log_stream, level=logging.NOTSET)
    logging.getLogger().addFilter(LevelFilter((logging.INFO, logging.WARNING, logging.ERROR)))
    
    logging.info('hello world')
    logging.warning('be careful!')
    logging.debug("you won't see this")
    logging.error('you will see this')
    logging.critical('critical is no longer logged!')
    
    print(log_stream.getvalue())
    

    Output

    INFO:root:hello world
    WARNING:root:be careful!
    ERROR:root:you will see this
    
    

提交回复
热议问题