在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 logging 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')
输出就不会出现debug,因为debug的等级比info的小,除此之外,default为warning
format:用来设定日志输出的方式
e.g.
import logging logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG) # 里面格式化参数地址为http://docs.python.org/2/library/logging.html#logre # cord-attributes,其中也可以设置时间csctime 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
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 logger logger = logging.getLogger('simple_example') logger.setLevel(logging.DEBUG) # create console handler and set level to debug ch = logging.StreamHandler() # 如果写入文件可改成FileHandler 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.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=DEBUG handlers=consoleHandler [logger_simpleExample] level=DEBUG handlers=consoleHandler qualname=simpleExample propagate=0 [handler_consoleHandler] class=StreamHandler level=DEBUG formatter=simpleFormatter args=(sys.stdout,) [formatter_simpleFormatter] format=%(asctime)s - %(name)s - %(levelname)s - %(message)s datefmt=
程序为:
import logging import logging.config logging.config.fileConfig('logging.conf') # create logger logger = logging.getLogger('simpleExample') # 'application' code logger.debug('debug message') logger.info('info message') logger.warn('warn message') logger.error('error message') logger.critical('critical message')
当然还可以,另一种格式叫做字典的格式
version: 1 formatters: simple: format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s' handlers: console: class: logging.StreamHandler level: DEBUG formatter: simple stream: ext://sys.stdout loggers: simpleExample: level: DEBUG handlers: [console] propagate: no root: level: DEBUG handlers: [console]