storing logger messages in a string

后端 未结 4 926
佛祖请我去吃肉
佛祖请我去吃肉 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:43

    Maybe this example code is enough.
    In general, you should post your code so we can see what is going on.
    You should also be looking at the actual Python documentation for the logging module while you are following any given tutorial.
    https://docs.python.org/2/library/logging.html

    The standard Python logging module can log to a file. When you are done logging, you can print the contents of that file to your shell output.

    # Do some logging to a file
    fname = 'mylog.log'
    logging.basicConfig(filename=fname, level=logging.INFO)
    logging.info('Started')
    logging.info('Finished')
    
    # Print the output
    with open(fname, 'r') as f:
        print f.read() # You could also store f.read() to a string
    

提交回复
热议问题