How to write to a file, using the logging Python module?

后端 未结 9 2058
天涯浪人
天涯浪人 2020-11-27 10:34

How can I use the logging module in Python to write to a file? Every time I try to use it, it just prints out the message.

9条回答
  •  失恋的感觉
    2020-11-27 10:39

    Taken from the "logging cookbook":

    # create logger with 'spam_application'
    logger = logging.getLogger('spam_application')
    logger.setLevel(logging.DEBUG)
    # create file handler which logs even debug messages
    fh = logging.FileHandler('spam.log')
    fh.setLevel(logging.DEBUG)
    logger.addHandler(fh)
    

    And you're good to go.

    P.S. Make sure to read the logging HOWTO as well.

提交回复
热议问题