[关闭]
@JunQiu 2018-12-14T07:24:25.000000Z 字数 2608 阅读 401

python_logging 日志处理流程

language_py summary_2018/12


1、py logger level=info 未输出info级别日志

1.1、原因
  1. import logging
  2. root = logging.getLogger()
  3. root.setLevel(logging.INFO)
  4. root.info('this is info')
  5. root.error('this is error')
  6. // 输出
  7. this is error
1.2、探究
  1. def error(self, msg, *args, **kwargs):
  2. """
  3. Log 'msg % args' with severity 'ERROR'.
  4. To pass exception information, use the keyword argument exc_info with
  5. a true value, e.g.
  6. logger.error("Houston, we have a %s", "major problem", exc_info=1)
  7. """
  8. # 判断可见性
  9. if self.isEnabledFor(ERROR):
  10. self._log(ERROR, msg, args, **kwargs)
  11. def isEnabledFor(self, level):
  12. """
  13. Is this logger enabled for level 'level'?
  14. """
  15. # 如果logging模块设置了disable level,<=level则不可见
  16. if self.manager.disable >= level:
  17. return False
  18. return level >= self.getEffectiveLevel()
  19. def getEffectiveLevel(self):
  20. """
  21. Get the effective level for this logger.
  22. Loop through this logger and its parents in the logger hierarchy,
  23. looking for a non-zero logging level. Return the first one found.
  24. """
  25. # 查找当前logger,如果未设置level,查找父节点的level,如果均无,返回NOTSET
  26. logger = self
  27. while logger:
  28. if logger.level:
  29. return logger.level
  30. logger = logger.parent
  31. return NOTSET
  32. // 并没有发现问题,可以直接传递到handler进行处理
  1. // 生成record对象,放入handler中进行处理
  2. def handle(self, record):
  3. """
  4. Call the handlers for the specified record.
  5. This method is used for unpickled records received from a socket, as
  6. well as those created locally. Logger-level filtering is applied.
  7. """
  8. # logger设置disabled是否传递给handler处理、filter过滤
  9. if (not self.disabled) and self.filter(record):
  10. self.callHandlers(record)
  11. # 从当前logger对象和父logger对象查找handler,未找到
  12. # 放入lastResort handler处理,但level为warn级别
  13. def callHandlers(self, record):
  14. """
  15. Pass a record to all relevant handlers.
  16. Loop through all handlers for this logger and its parents in the
  17. logger hierarchy. If no handler was found, output a one-off error
  18. message to sys.stderr. Stop searching up the hierarchy whenever a
  19. logger with the "propagate" attribute set to zero is found - that
  20. will be the last logger whose handlers are called.
  21. """
  22. c = self
  23. found = 0
  24. while c:
  25. for hdlr in c.handlers:
  26. found = found + 1
  27. if record.levelno >= hdlr.level:
  28. hdlr.handle(record)
  29. if not c.propagate:
  30. c = None # break out
  31. else:
  32. c = c.parent
  33. if (found == 0):
  34. if lastResort:
  35. if record.levelno >= lastResort.level:
  36. lastResort.handle(record)
  37. elif raiseExceptions and not self.manager.emittedNoHandlerWarning:
  38. sys.stderr.write("No handlers could be found for logger"
  39. " \"%s\"\n" % self.name)
  40. self.manager.emittedNoHandlerWarning = True
1.3、结论
  1. // example
  2. import logging
  3. root = logging.getLogger()
  4. # set logger level 为 INFO
  5. root.setLevel(logging.INFO)
  6. # set handler
  7. handler = logging.StreamHandler()
  8. handler.setLevel(logging.INFO)
  9. root.addHandler(handler)
  10. root.info('this is info')
  11. root.error('this is error')
  12. //输出
  13. this is info
  14. this is error
1.4、参考
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注