在Python HOWTO里面介绍了什么时候去用这个模块。我觉得就是你想用的时候就行(废话嘛。。。。。)(例子来源于Python HOWTO)
1、basicConfig
logging.basicConfig(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')
basicConfig里面的参数有filename,filemode,level,format等
filename:你的日志存入的文件名称
filenmode:你的日志文件的打开方式,同文件打开的方式一样
level:你的日志等级
其中等级分为如下几个:从上到下依次升高
NOSET:不设置任何,即任何等级都可以记录
DEBUG:
INFO:
WARNING:
ERROR:
CRITICAL:
例子:
import logginglogging.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')
输出就不会出现debug,因为debug的等级比info的小,除此之外,default为warning
format:用来设定日志输出的方式
e.g.
import logginglogging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG) # 里面格式化参数地址为http://docs.python.org/2/library/logging.html#logre # cord-attributes,其中也可以设置时间csctimelogging.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 consoleINFO:So should thisWARNING:And this, too
2、上面只是一些基本用法,还有一些高级一点儿的用法logger,handler,formatter
logger:日志对象logger.setLevel() logger.addHandler() logger.removeHandler() logger.debug() logger.info logger.warning logger. error() logger.addFilter()
logger.removeFilter()
handler:关于日志的处理的一些参数handler.setLevel() handler.setFormatter() handler.addFilter() handler.removeFilter()
formatter:设定日志输出的形式,常见形式:'%(asctime)s - %(levelname)s - %(message)s'
import logging# create loggerlogger = logging.getLogger('simple_example')logger.setLevel(logging.DEBUG)# create console handler and set level to debugch = logging.StreamHandler() # 如果写入文件可改成FileHandlerch.setLevel(logging.DEBUG)# create formatterformatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')# add formatter to chch.setFormatter(formatter)# add ch to loggerlogger.addHandler(ch)# 'application' codelogger.debug('debug message')logger.info('info message')logger.warn('warn message')logger.error('error message')logger.critical('critical message')
3、还可以从配置文件中读取log的参数
下面是配置文件loggin.conf
[loggers]keys=root,simpleExample[handlers]keys=consoleHandler[formatters]keys=simpleFormatter[logger_root]level=DEBUGhandlers=consoleHandler[logger_simpleExample]level=DEBUGhandlers=consoleHandlerqualname=simpleExamplepropagate=0[handler_consoleHandler]class=StreamHandlerlevel=DEBUGformatter=simpleFormatterargs=(sys.stdout,)[formatter_simpleFormatter]format=%(asctime)s - %(name)s - %(levelname)s - %(message)sdatefmt=
程序为:
import loggingimport logging.configlogging.config.fileConfig('logging.conf')# create loggerlogger = logging.getLogger('simpleExample')# 'application' codelogger.debug('debug message')logger.info('info message')logger.warn('warn message')logger.error('error message')logger.critical('critical message')
当然还可以,另一种格式叫做字典的格式
version: 1formatters: simple: format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'handlers: console: class: logging.StreamHandler level: DEBUG formatter: simple stream: ext://sys.stdoutloggers: simpleExample: level: DEBUG handlers: [console] propagate: noroot: level: DEBUG handlers: [console]