python库的学习系列之 15. Generic Operating System Services

15.7. logging — Logging facility for Python

使用这个模块,可以打印出非常漂亮的log信息,如一个简单的例子:

  
    
import logging
import os
import logging.config

logging.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 = consoleHandler
qualname
= abc
propagate
= 0

[handler_consoleHandler]
class = StreamHandler
level
= INFO
formatter
= simpleFormatter
args
= (sys.stdout,)

在使用的地方:

  
    
logging.getLogger().info( " sleep 30 seconds and do the next things " )
一般来说,上面的例子就够我们工作中使用了,如果要输出更漂亮,复杂的log信息, 可以参考 http://docs.python.org/library/logging.html

你可能感兴趣的:(service)