15.7. logging — Logging facility for Python
使用这个模块,可以打印出非常漂亮的log信息,如一个简单的例子:
import logging import os import logging.configlogging.config.fileConfig(os.path.dirname( __file__ ) + ' /../conf/logging.conf ' )
而我们的logging.conf则是配置我们log格式的东东,以上的code对应下面的conf配置:
# ########################## # log.conf # ########################## [loggers]keys = root,abc[handlers] # keys=consoleHandler,rotateFileHandler keys = consoleHandler[formatters]keys = simpleFormatter#定义了log打印的格式[formatter_simpleFormatter]format = [ % (asctime)s] ( % (levelname)s) % (filename)s: % (lineno)3d: % (message)s[logger_root]level = DEBUG # handlers=consoleHandler, rotateFileHandler handlers = consoleHandler[logger_abc]level = DEBUG # handlers=consoleHandler, rotateFileHandler handlers = consoleHandlerqualname = abcpropagate = 0[handler_consoleHandler] class = StreamHandlerlevel = INFOformatter = simpleFormatterargs = (sys.stdout,)
在使用的地方:
logging.getLogger().info( " sleep 30 seconds and do the next things " )