Takkun Takkun - 2 months ago 10
Python Question

Using the logging python class to write to a file?

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

Answer

An example 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: set the output file, set it to append rather than overwrite, determine the format of the output message, determine the format of the output time, and determine the minimum message level it will accept.

Comments