#定义Formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
#为Handler添加Formatter
fh.setFormatter(formatter)
ch.setFormatter(formatter)
Formatter参数中可能用到的格式化串参见上文(logging.basicConfig()函数format参数中可能用到的格式化串:)
Filter
限制只有满足过滤规则的日志才会输出。
比如我们定义了filter = logging.Filter('a.b.c'),并将这个Filter添加到了一个Handler上,则使用该Handler的Logger中只有名字带a.b.c前缀的Logger才能输出其日志。
取消下列两行程序的注释
#filter = logging.Filter('mylogger.child1.child2')
#fh.addFilter(filter)
#filter = logging.Filter('mylogger.child1.child2')
#fh.addFilter(filter)
#fh.addFilter(filter)
并取消如下几行的注释
#logger.addFilter(filter)
#logger1.addFilter(filter)
#logger3.addFilter(filter)
#logger4.addFilter(filter)
输出结果
2014-05-06 15:32:10,746 - mylogger.child1.child2 - DEBUG - logger4 debug message
2014-05-06 15:32:10,746 - mylogger.child1.child2 - DEBUG - logger4 debug message
2014-05-06 15:32:10,746 - mylogger.child1.child2 - DEBUG - logger4 debug message
2014-05-06 15:32:10,746 - mylogger.child1.child2 - DEBUG - logger4 debug message
2014-05-06 15:32:10,748 - mylogger.child1.child2 - INFO - logger4 info message
2014-05-06 15:32:10,748 - mylogger.child1.child2 - INFO - logger4 info message
2014-05-06 15:32:10,748 - mylogger.child1.child2 - INFO - logger4 info message
2014-05-06 15:32:10,748 - mylogger.child1.child2 - INFO - logger4 info message
2014-05-06 15:32:10,751 - mylogger.child1.child2 - WARNING - logger4 warning message
2014-05-06 15:32:10,751 - mylogger.child1.child2 - WARNING - logger4 warning message
2014-05-06 15:32:10,751 - mylogger.child1.child2 - WARNING - logger4 warning message
2014-05-06 15:32:10,751 - mylogger.child1.child2 - WARNING - logger4 warning message
2014-05-06 15:32:10,753 - mylogger.child1.child2 - ERROR - logger4 error message
2014-05-06 15:32:10,753 - mylogger.child1.child2 - ERROR - logger4 error message
2014-05-06 15:32:10,753 - mylogger.child1.child2 - ERROR - logger4 error message
2014-05-06 15:32:10,753 - mylogger.child1.child2 - ERROR - logger4 error message
2014-05-06 15:32:10,754 - mylogger.child1.child2 - CRITICAL - logger4 critical message
2014-05-06 15:32:10,754 - mylogger.child1.child2 - CRITICAL - logger4 critical message
2014-05-06 15:32:10,754 - mylogger.child1.child2 - CRITICAL - logger4 critical message
2014-05-06 15:32:10,754 - mylogger.child1.child2 - CRITICAL - logger4 critical message
2014-05-06 15:32:10,755 - mylogger.child1.child2.child3 - DEBUG - logger5 debug message
2014-05-06 15:32:10,755 - mylogger.child1.child2.child3 - DEBUG - logger5 debug message
2014-05-06 15:32:10,755 - mylogger.child1.child2.child3 - DEBUG - logger5 debug message
2014-05-06 15:32:10,755 - mylogger.child1.child2.child3 - DEBUG - logger5 debug message
2014-05-06 15:32:10,755 - mylogger.child1.child2.child3 - DEBUG - logger5 debug message
2014-05-06 15:32:10,757 - mylogger.child1.child2.child3 - INFO - logger5 info message
2014-05-06 15:32:10,757 - mylogger.child1.child2.child3 - INFO - logger5 info message
2014-05-06 15:32:10,757 - mylogger.child1.child2.child3 - INFO - logger5 info message
2014-05-06 15:32:10,757 - mylogger.child1.child2.child3 - INFO - logger5 info message
2014-05-06 15:32:10,757 - mylogger.child1.child2.child3 - INFO - logger5 info message
2014-05-06 15:32:10,759 - mylogger.child1.child2.child3 - WARNING - logger5 warning message
2014-05-06 15:32:10,759 - mylogger.child1.child2.child3 - WARNING - logger5 warning message
2014-05-06 15:32:10,759 - mylogger.child1.child2.child3 - WARNING - logger5 warning message
2014-05-06 15:32:10,759 - mylogger.child1.child2.child3 - WARNING - logger5 warning message
2014-05-06 15:32:10,759 - mylogger.child1.child2.child3 - WARNING - logger5 warning message
2014-05-06 15:32:10,761 - mylogger.child1.child2.child3 - ERROR - logger5 error message
2014-05-06 15:32:10,761 - mylogger.child1.child2.child3 - ERROR - logger5 error message
2014-05-06 15:32:10,761 - mylogger.child1.child2.child3 - ERROR - logger5 error message
2014-05-06 15:32:10,761 - mylogger.child1.child2.child3 - ERROR - logger5 error message
2014-05-06 15:32:10,761 - mylogger.child1.child2.child3 - ERROR - logger5 error message
2014-05-06 15:32:10,762 - mylogger.child1.child2.child3 - CRITICAL - logger5 critical message
2014-05-06 15:32:10,762 - mylogger.child1.child2.child3 - CRITICAL - logger5 critical message
2014-05-06 15:32:10,762 - mylogger.child1.child2.child3 - CRITICAL - logger5 critical message
2014-05-06 15:32:10,762 - mylogger.child1.child2.child3 - CRITICAL - logger5 critical message
2014-05-06 15:32:10,762 - mylogger.child1.child2.child3 - CRITICAL - logger5 critical message
发现root、mylogger、mylogger.child1的输出全部被过滤掉了。
除了直接在程序中设置Logger,Handler,Filter,Formatter外还可以将这些信息写进配置文件中。
例如典型的logging.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=
程序可以这么写