logging.basicConfig(filename='example.log',level=logging.INFO) logging.debug('This message should go to the log file') logging.info('So should this') logging.warning('And this, too')
# example.log内容如下 INFO:root:So should this WARNING:root:And this, too
自定义日志格式
日志起码要有时间吧!
1 2 3 4 5 6
import logging logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') logging.warning('is when this event was logged.')
#输出 12/12/2010 11:46:36 AM is when this event was logged.