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

后端 未结 9 2064
天涯浪人
天涯浪人 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条回答
  •  Happy的楠姐
    2020-11-27 10:49

    An example of using logging.basicConfig rather than logging.fileHandler()

    logging.basicConfig(filename=logname,
                                filemode='a',
                                format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
                                datefmt='%H:%M:%S',
                                level=logging.DEBUG)
    
    logging.info("Running Urban Planning")
    
    self.logger = logging.getLogger('urbanGUI')
    

    In order, the five parts do the following:

    1. set the output file (filename=logname)
    2. set it to append rather than overwrite (filemode='a')
    3. determine the format of the output message (format=...)
    4. determine the format of the output time (datefmt='%H:%M:%S')
    5. and determine the minimum message level it will accept (level=logging.DEBUG).

提交回复
热议问题