日志模块logging的四大组件:
logger: 志类应用程序往往通调用提供api记录志
handler: 志信息处理志发送(保存)同目标域
filter: 志信息进行滤
formatter:志格式化
Logger类:
构造
使用工厂方法返回一个Logger实例。
logging.getLogger([name=None])
指定name,返回一个名称为name的Logger实例。如果再次使用相同的名字,是实例化一个对象。未指定name,返回Logger实例,名称是root,即根Logger。
Logger是层次结构的,使用 '.' 点号分割,如'a'、'a.b'或'a.b.c.d','a'是'a.b'的父parent,a.b是a的子child。对于foo来说,名字为foo.bar、foo.bar.baz、foo.bam都是foo的后代。
举例:
import logging DATEFMT ="[%Y-%m-%d %H:%M:%S]" FORMAT = "%(asctime)s %(thread)d %(message)s" logging.basicConfig(level=logging.INFO,format=FORMAT,datefmt=DATEFMT,filename='class_test.log') root = logging.getLogger() print(root.name,type(root),root.parent,id(root)) logger = logging.getLogger(__name__) print(logger.name, type(logger), id(logger), id((logger.parent))) logger1 = logging.getLogger(__name__ + ".ok") print(logger1.name, type(logger1), id(logger1), id((logger1.parent))) print(logger1.parent,id(logger1.parent)) 运行结果: root <class 'logging.RootLogger'> None 4367575248 __main__ <class 'logging.Logger'> 4367575864 4367575248 __main__.ok <class 'logging.Logger'> 4367575920 4367575864 <logging.Logger object at 0x10453eb38> 4367575864
子child的级别设置,不影响父parent的级别:
import logging FORMAT = "%(asctime)s %(thread)d %(message)s" logging.basicConfig(level=logging.WARNING,format=FORMAT,datefmt="[%Y-%m-%d %H:%M:%S]") root = logging.getLogger() print(1,root,id(root)) #RootLogger,根Logger root.info('my root') #低于定义的WARNING级别,所以不会记录 loga = logging.getLogger(__name__) #Logger继承自RootLogger print(2,loga,id(loga),id(loga.parent)) print(3,loga.getEffectiveLevel()) #数值形式的有效级别 loga.warning('before') loga.setLevel(28) #设置级别为28 print(4,loga.getEffectiveLevel()) loga.info('after')# loga.warning('after1') 运行结果: [2017-12-17 16:31:20] 4320629568 before 1 <logging.RootLogger object at 0x104534f28> 4367535912 2 <logging.Logger object at 0x1044ef630> 4367250992 4367535912 3 30 4 28 [2017-12-17 16:31:20] 4320629568 after1
Handler:
Handler控制日志信息的输出目的地,可以是控制台、文件。
可以单独设置level
可以单独设置格式
可以设置过滤器
Handler
StreamHandler #不指定使用sys.strerr
FileHandler #文件
_StderrHandler #标准输出
NullHandler #什么都不做
level的继承:
import logging FORMAT = "%(asctime)s %(thread)d %(message)s" logging.basicConfig(level=logging.INFO,format=FORMAT,datefmt="[%Y-%m-%d %H:%M:%S]") root = logging.getLogger() #根Logger级别为INFO 20 print('root:',root.getEffectiveLevel()) log1 = logging.getLogger('s') log1.setLevel(logging.ERROR) #级别为ERROR 40 print('log1:',log1.getEffectiveLevel()) log1.error('log1 error') log2 = logging.getLogger('s.s1') #继承自log1 40,无法使用warning log2.setLevel(logging.WARNING) #设置为WARNING 30,才可以使用warning print('log2:',log2.getEffectiveLevel()) log2.warning('log2 warning') 运行结果: [2017-12-17 16:52:22] 4320629568 log1 error root: 20 log1: 40 [2017-12-17 16:52:22] 4320629568 log2 warning log2: 30
logger实例,如果设置了level,就用它和信息的级别比较,否则,继承最近的祖先的level。
handler处理:
import logging FORMAT = "%(asctime)s %(thread)d %(message)s" logging.basicConfig(level=logging.INFO,format=FORMAT,datefmt="[%Y-%m-%d %H:%M:%S]") root = logging.getLogger() print(1,root.getEffectiveLevel()) #RootLogger,根Logger log1 = logging.getLogger('s') print(2,log1.getEffectiveLevel()) h1 = logging.FileHandler('test.log') h1.setLevel(logging.WARNING) log1.addHandler(h1) print(3,log1.getEffectiveLevel()) log2 = logging.getLogger('s.s2') print(4,log2.getEffectiveLevel()) h2 = logging.FileHandler('test1.log') h2.setLevel(logging.WARNING) log1.addHandler(h2) print(3,log1.getEffectiveLevel()) log2.warning('log2 info---') 运行结果: 1 20 [2017-12-17 19:02:53] 7956 log2 info--- 2 20 3 20 4 20 3 20
test.log和test1.log最终都会记录一份"log2 info---"
同样,handler也可以设置使用logging.Formatter()设置格式和Logging.Filter()设置过滤器:
import logging FORMAT = "%(asctime)s %(thread)d %(message)s" logging.basicConfig(level=logging.INFO,format=FORMAT,datefmt="[%Y-%m-%d %H:%M:%S]") root = logging.getLogger() print(1,root.getEffectiveLevel()) #RootLogger,根Logger log1 = logging.getLogger('s')#模块化用__module__,函数化用__name__作为Logger名,Logger同名内存中也只有一个 print(2,log1.getEffectiveLevel()) h1 = logging.FileHandler('test.log') h1.setLevel(logging.WARNING) fmt1 = logging.Formatter('[%(asctime)s] %(thread)s %(threadName)s log1-handler1 %(message)s') h1.setFormatter(fmt1) #重新个性化定义记录的格式化字符串 log1.addHandler(h1) filter1 = logging.Filter('s') #过滤器 会记录s, s.s2的信息 log1.addFilter(filter1) print(3,log1.getEffectiveLevel()) log2 = logging.getLogger('s.s2') print(4,log2.getEffectiveLevel()) h2 = logging.FileHandler('test1.log') h2.setLevel(logging.WARNING) log1.addHandler(h2) filter1 = logging.Filter('s.s2') #过滤器不会记录s.s2的消息,只会记录自己的消息 log1.addFilter(filter1) print(3,log1.getEffectiveLevel()) log1.warning('log1 warning===') log2.warning('log2 warning---') 运行结果: test.log: #handler1记录了到了log1和log2的信息 [2017-12-17 19:43:12,654] 5872 MainThread log1-handler1 log1 warning=== [2017-12-17 19:43:12,654] 5872 MainThread log1-handler1 log2 warning---
test1.log: #handler2只记录了它自己的信息
log2 warning---
参考:https://www.cnblogs.com/i-honey/p/8052579.html