看这就好, 很容易懂:
https://docs.python.org/3/howto/logging.html#logging-basic-tutorial
1 2 3 4 5 6 7 |
import logging logging.basicConfig(filename='06_example_log.log', level=logging.INFO, format='[%(asctime)s] %(name)-12s %(levelname)s: %(message)s') logging.info("start") logging.info("one thing done.") logging.info("another thing done.") logging.info("all finished.") |
结果在06_example_log.log文件里有:
1 2 3 4 |
[2016-10-20 15:43:50,930] root INFO: start [2016-10-20 15:43:50,931] root INFO: one thing done. [2016-10-20 15:43:50,931] root INFO: another thing done. [2016-10-20 15:43:50,931] root INFO: all finished. |