**
一、基础教程
**
DEBUG 详细信息,通常仅在诊断问题时才需要。
INFO 确认一切正常。
WARNING 表示发生了意外情况,或者表示在不久的将来出现了一些问题(例如“磁盘空间不足”)。该软件仍按预期运行。
ERROR 由于存在更严重的问题,该软件无法执行某些功能。
CRITICAL 严重错误,表明程序本身可能无法继续运行。
1、一个简单的例子:
import logging
logging.warning('Watch out!') # will print a message to the console
logging.info('I told you so') # will not print anything
执行结果:
.warning:root:Watch out!
默认级别为warning,所以info未输出
2、记录到文件
import logging
logging.basicConfig(filename='example.log',level=logging.DEBUG)#追加日志信息
#logging.basicConfig(filename='example.log', filemode='w', level=logging.DEBUG)#覆盖日志信息
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')
example.log文件内容:
DEBUG:root:This message should go to the log file
INFO:root:So should this
WARNING:root:And this, too
3、多个模块记录
#文件mylib.py
import logging
def do_something():
logging.info('Doing something')
#文件myapp.py
import logging
import mylib
def main():
logging.basicConfig(filename='myapp.log', level=logging.INFO)
logging.info('Started')
mylib.do_something()
logging.info('Finished')
if __name__ == '__main__':
main()
运行结果
INFO:root:Started
INFO:root:Doing something
INFO:root:Finished
4、记录可变数据
import logging
logging.warning('%s before you %s', 'Look', 'leap!')
输出:
WARNING:root:Look before you leap!
5、更改显示消息中的格式
import logging
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
logging.debug('This message should appear on the console')
logging.info('So should this')
logging.warning('And this, too')
输出:
DEBUG:This message should appear on the console
INFO:So should this
WARNING:And this, too
7、显示在消息中的日期/时间·
###----------------要显示事件的日期和时间,您可以将'%(asctime)s'放入格式字符串中:
import logging
logging.basicConfig(format='%(asctime)s %(message)s')
logging.warning('is when this event was logged.')
输出:
2010-12-12 11:41:42,612 is when this event was logged.
####---日期/时间显示的默认格式(如上所示)类似于ISO8601或 RFC 3339。如果您需要对日期/时间格式的更多控制,请向提供一个 datefmt参数basicConfig,如以下示例所示:
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.
datefmt参数的格式与所支持的格式相同 time.strftime()。
**
二、高级日志教程
**
配置日志
import logging
# create logger
logger = logging.getLogger('simple_example')
logger.setLevel(logging.DEBUG)
# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# create formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# add formatter to ch
ch.setFormatter(formatter)
# add ch to logger
logger.addHandler(ch)
# 'application' code
logger.debug('debug message')
logger.info('info message')
logger.warning('warn message')
logger.error('error message')
logger.critical('critical message')
输出:
$ python simple_logging_module.py
2005-03-19 15:10:26,618 - simple_example - DEBUG - debug message
2005-03-19 15:10:26,620 - simple_example - INFO - info message
2005-03-19 15:10:26,695 - simple_example - WARNING - warn message
2005-03-19 15:10:26,697 - simple_example - ERROR - error message
2005-03-19 15:10:26,773 - simple_example - CRITICAL - critical message