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.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
http://docs.python.org/library/logging.html#logging.basicConfig
I prefer to use a configuration file. It allows me to switch logging levels, locations, etc without changing code when I go from development to release. I simply package a different config file with the same name, and with the same defined loggers.
Here is my code for the log config file
http://docs.python.org/library/logging.handlers.html#filehandler
Taken from the "logging cookbook":
And you're good to go.
P.S. Make sure to read the logging HOWTO as well.
An example of using
logging.basicConfig
rather thanlogging.fileHandler()
In order, the five parts do the following:
filename=logname
)filemode='a'
)format=...
)datefmt='%H:%M:%S'
)level=logging.DEBUG
).