我想做一些我认为很简单的事情。Python中不同级别的日志记录
其实用Python记录模块,我有兴趣记录 一切从命令 行参数给出的水平在命令行上,及日志记录文件到一个固定的调试级别。
创建具有不同级别不起作用两个不同的记录器,但 设置两个不同的处理程序都添加到根 记录器也不起作用的水平,所以对我应该怎么真正做到这一点的任何想法? (阅读上的其他链接第二种方法应该工作,所以我在做别的事情愚蠢?)
这是目前建立了我的日志系统中的代码:
class LoggerSetup(object):
"""Setup the different logger objects
"""
def __init__(self):
self.root_logger = logging.getLogger()
self.shell_hdlr = logging.StreamHandler()
#TODO: add another logging handler which stores to a temporary file
#which should be cleaned up later
def setup_shell_logger(self, log_level):
self.root_logger.setLevel(LOG_LEVELS[log_level])
# in this way the root logger is not set but the handlers are set
self.shell_hdlr = logging.StreamHandler()
self.shell_hdlr.setLevel(LOG_LEVELS[log_level])
self.shell_hdlr.setFormatter(StarFormatter())
#FIXME: add the support for regular expression exclusion too
self.root_logger.addHandler(self.shell_hdlr)
def setup_log_include(self, log_include):
"""Set up the filter to include log messages
"""
if log_include:
incl = FilterInclude(log_include)
self.shell_hdlr.addFilter(incl)
def setup_log_exclude(self, log_exclude):
"""Set up the filters to exclude log messages
"""
if log_exclude:
excl = FilterExclude(log_exclude)
self.shell_hdlr.addFilter(excl)
def setup_file_logging(self):
"""Set up the file logger, which always logs in DEBUG mode
even if the top level logger is set to another level
"""
#XXX: not working, one possible way to make it work is to create
#only one log, and different handler/filters to make to handle all
#the different outputs
file_handler = logging.FileHandler(LOG_FILENAME)
# the file logging is always in debug mode
file_handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s - %(asctime)s')
file_handler.setFormatter(formatter)
self.root_logger.addHandler(file_handler)
“,但设置添加到根记录器的两个不同处理程序的级别不起作用“?你能澄清一下吗? – 2012-01-12 15:48:52