@JunQiu
2018-12-14T15:24:25.000000Z
字数 2608
阅读 1166
language_py
summary_2018/12
import logging
root = logging.getLogger()
root.setLevel(logging.INFO)
root.info('this is info')
root.error('this is error')
// 输出
this is error
def error(self, msg, *args, **kwargs):
"""
Log 'msg % args' with severity 'ERROR'.
To pass exception information, use the keyword argument exc_info with
a true value, e.g.
logger.error("Houston, we have a %s", "major problem", exc_info=1)
"""
# 判断可见性
if self.isEnabledFor(ERROR):
self._log(ERROR, msg, args, **kwargs)
def isEnabledFor(self, level):
"""
Is this logger enabled for level 'level'?
"""
# 如果logging模块设置了disable level,<=level则不可见
if self.manager.disable >= level:
return False
return level >= self.getEffectiveLevel()
def getEffectiveLevel(self):
"""
Get the effective level for this logger.
Loop through this logger and its parents in the logger hierarchy,
looking for a non-zero logging level. Return the first one found.
"""
# 查找当前logger,如果未设置level,查找父节点的level,如果均无,返回NOTSET
logger = self
while logger:
if logger.level:
return logger.level
logger = logger.parent
return NOTSET
// 并没有发现问题,可以直接传递到handler进行处理
// 生成record对象,放入handler中进行处理
def handle(self, record):
"""
Call the handlers for the specified record.
This method is used for unpickled records received from a socket, as
well as those created locally. Logger-level filtering is applied.
"""
# logger设置disabled是否传递给handler处理、filter过滤
if (not self.disabled) and self.filter(record):
self.callHandlers(record)
# 从当前logger对象和父logger对象查找handler,未找到
# 放入lastResort handler处理,但level为warn级别
def callHandlers(self, record):
"""
Pass a record to all relevant handlers.
Loop through all handlers for this logger and its parents in the
logger hierarchy. If no handler was found, output a one-off error
message to sys.stderr. Stop searching up the hierarchy whenever a
logger with the "propagate" attribute set to zero is found - that
will be the last logger whose handlers are called.
"""
c = self
found = 0
while c:
for hdlr in c.handlers:
found = found + 1
if record.levelno >= hdlr.level:
hdlr.handle(record)
if not c.propagate:
c = None # break out
else:
c = c.parent
if (found == 0):
if lastResort:
if record.levelno >= lastResort.level:
lastResort.handle(record)
elif raiseExceptions and not self.manager.emittedNoHandlerWarning:
sys.stderr.write("No handlers could be found for logger"
" \"%s\"\n" % self.name)
self.manager.emittedNoHandlerWarning = True
// example
import logging
root = logging.getLogger()
# set logger level 为 INFO
root.setLevel(logging.INFO)
# set handler
handler = logging.StreamHandler()
handler.setLevel(logging.INFO)
root.addHandler(handler)
root.info('this is info')
root.error('this is error')
//输出
this is info
this is error