更新文件中 ogs 的示例:
2016-10-06 07:04:21 错误 file.py 行号:120 索引错误
文档是你的朋友
记录到文件:
https://docs.python.org/2/howto/logging.html#logging-to-a-file
改变消息的格式:
https://docs.python.org/2/howto/logging.html#changed-the-format-of-displayed-messages
有关日期和日志记录的更多信息:
https://docs.python.org/2/howto/logging.html#displaying-the-date-time-in-messages
import logging
logging.basicConfig(format='%(asctime)s %(filename)s:%(levelname)s line no: %(lineno)d %(message)s', filename='example.log',level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')