f __name__ == '__main__':
? ? sLogger.info('OK')
? ? print len(logging.root.handlers), logging.root.handlers
? ? logging.info('Bad')
? ? print len(logging.root.handlers), logging.root.handlers
运行程序,得到如下运行结果:
0 []
1 []。
?
第一行结果为0 []很好的解释了,为什么正常情况下,root logging对象为什么没有打出LOG。
而调用logging.info('Bad')之后,root.handlers对象为StreamHandler对象。通过这个程序可以看到调用logging.info对象前后root logging对象发生的变化。
还有一点需要验证,就是logging调用前后正常模块logging的方式。
在__main__中写下如下代码:
if __name__ == '__main__':
? ? for i in xrange(0, 2):
? ? ? ? sLogger.info('OK')
? ? ? ? logging.info('Bad')
根据之前分析的,第一次调用sLogger.info('OK')是不会打LOG的,而logging.info本身是由于不到WARNING级别,所以也没有打LOG,而第二次会打LOG在屏幕中。所以,看到的结果是,LOG文件中有三条LOG,而屏幕上有一条INFO:batch:OK。跟之前猜想到的挺吻合的。
?为什么调用了logging.info之后,会发生如此转变?
继续看完上面root logging info,并对照着下面的basicConfig代码。会注意到len(root.handlers) == 0会去调用basicConfig,这个时候就可以注意下,basicConfig这个模块的实现。
def basicConfig(**kwargs):?
? ? if len(root.handlers) == 0:
? ? ? ? filename = kwargs.get("filename")
? ? ? ? if filename:
? ? ? ? ? ? mode = kwargs.get("filemode", 'a')
? ? ? ? ? ? hdlr = FileHandler(filename, mode)
? ? ? ? else:
? ? ? ? ? ? stream = kwargs.get("stream")
? ? ? ? ? ? hdlr = StreamHandler(stream)
? ? ? ? fs = kwargs.get("format", BASIC_FORMAT)
? ? ? ? dfs = kwargs.get("datefmt", None)
? ? ? ? fmt = Formatter(fs, dfs)
? ? ? ? hdlr.setFormatter(fmt)
? ? ? ? root.addHandler(hdlr)
? ? ? ? level = kwargs.get("level")
? ? ? ? if level is not None:
? ? ? ? ? ? root.setLevel(level)
可以看出,当root.handlers的长度为0的时候,会创建一个默认的StreamHandler对象,而这个对象设置的模式导致的情况是LOG会打到屏幕上。这个跟之前打出的logging.root.handlers的结果挺吻合。通过这些想必明白了,为什么我之前遇到的C文件中调用logging.info的方式会影响到上层模块以及其调用的子模块。
通过我遇到的问题,以及对logging的这相关部分的分析,想必会对logging模块有更深刻的认识。最关键的一点,如果想尽可能精确的控制logging方式,一定要注意,主模块以及对应的子模块中具体不要直接使用logging打LOG。
--------------------------------------分割线 --------------------------------------