logging输出级别实现原理
祖先继承,优先自己
- logging.Logger比较
import logging
FATMAT="%(asctime)s %(message)s"
logging.basicConfig(format=FATMAT,datefmt="%Y-%m-%d %H:%M:%S")
#Logger是一个类所以两个实例不相等
a=logging.Logger('ll')
b=logging.Logger('ll')
print(a)
print(b)
print(a is b)
- logging.getLogger比较,以后都使用这个来定义新的log
import logging
FATMAT="%(asctime)s %(message)s"
logging.basicConfig(format=FATMAT,datefmt="%Y-%m-%d %H:%M:%S")
#在getLogger未有参数的时候默认调用root
c=logging.getLogger()
d=logging.getLogger()
print(c is d , c ,d)
原理
使用带参数的getLogger
import logging
FATMAT="%(asctime)s %(message)s"
logging.basicConfig(format=FATMAT,datefmt="%Y-%m-%d %H:%M:%S")
#在getLogger未有参数的时候默认调用root
c=logging.getLogger('ll')
d=logging.getLogger('ll')
print(c is d , c ,d)
原理:有一个字典管理这些
- logging输出原理
getEffectiveLevel()指定级别,有继承关系
import logging
FORMAT="%(asctime)s %(message)s"
logging.basicConfig(level=logging.INFO,format=FORMAT,datefmt="%Y/%m/%d %H:%M:%S")
log1=logging.getLogger(__name__)
log1.info('test info')
log1.debug('test debug')
print(log1.level,log1.getEffectiveLevel())
log1.setLevel(logging.NOTSET+1)
print(log1.level,log1.getEffectiveLevel())
log1.info('test info')
log1.debug('test debug')
假如是 logging.info info 和 getEffectiveLevel()
getEfficetiveLevel有继承关系
如果设置 logging.setLevel 则getEffectiveLevel等于当前self.level