Think before you speak, read before you think.

python debug使用logging

by

in

使用 logging 有很多好处:

可以方便添加 timestamp
可以为日志定级
不会和业务的 print 混淆
如果你忘了去掉 logging ,最多也只是有一个 log 文件

如果要打印出来

import logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
logging.debug('This is a log message.')

如果要放到文件里

import logging
logging.basicConfig(filename='log_filename.txt', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
logging.debug('This is a log message.')

报警级别

logger.critical('This is a critical message.')
logger.error('This is an error message.')
logger.warning('This is a warning message.')
logger.info('This is an informative message.')
logger.debug('This is a low-level debug message.')

http://inventwithpython.com/blog/2012/04/06/stop-using-print-for-debugging-a-5-minute-quickstart-guide-to-pythons-logging-module/


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *