python - 设置高级 Python 日志记录

标签 python logging

我想为我的模块使用日志记录,但我不确定如何设计以下要求:

  • 正常的日志记录级别(信息、错误、警告、调试)以及一些额外的更详细的调试级别
  • 日志消息可以有不同的类型;有些是为开发人员准备的,有些是为用户准备的;这些类型去不同的输出
  • 错误应该转到stderr
  • 我还需要跟踪哪个模块/函数/代码行编写了调试消息,以便我可以激活或停用配置中的各个调试消息
  • 我需要跟踪是否发生错误,以便最终在程序结束时执行 sys.exit()
  • 在设置记录器之前,所有消息都应该发送到stdout

我已经阅读了日志记录文档,但我不确定使用符合上述要求的日志记录模块的最简单方法是什么(如何使用 Logger、Handler、Filter 等概念)。你能指出一个想法来设置它吗? (例如,使用两个记录器“用户”、“开发人员”编写模块;从 Logger 派生;执行 getLogger(__name__);保持这样的错误标志,...等等。 )

最佳答案

1) 添加更详细的调试级别。

你考虑清楚了吗?

看看doc是什么说:

Defining your own levels is possible, but should not be necessary, as the existing levels have been chosen on the basis of practical experience. However, if you are convinced that you need custom levels, great care should be exercised when doing this, and it is possibly a very bad idea to define custom levels if you are developing a library. That's because if multiple library authors all define their own custom levels, there is a chance that the logging output from such multiple libraries used together will be difficult for the using developer to control and/or interpret, because a given numeric value might mean different things for different libraries.

另请查看 When to use logging ,有两个非常好的表格解释了何时使用什么。

无论如何,如果您认为您需要那些额外的日志记录级别,请查看:logging.addLevelName() .

2) 一些日志消息给开发者,一些给用户

使用具有不同处理程序的不同记录器系列。在每个家庭的基地 Logger.propagateFalse

3) 错误应该转到stderr

这已经默认发生在 StreamHandler 中:

class logging.StreamHandler(stream=None)

Returns a new instance of the StreamHandler class. If stream is specified, the instance will use it for logging output; otherwise, sys.stderr will be used.

4) 跟踪日志消息的来源

获取具有不同名称的记录器,并在您的 Formatter 中使用带有 %(name)s 的格式字符串。

5) 所有消息都应该发送到stdout,直到设置记录器

日志系统的设置应该是最先要做的事情之一,所以我真的不明白这是什么意思。如果您需要将消息发送到标准输出,请按原样使用 print 并且已在 When to use logging 中进行了说明。 .

最后的建议:仔细阅读 Logging Cookbook因为它很好地涵盖了您的需求。


来自评论:我将如何设计以输出到不同的来源并过滤我的模块?

我一开始就不会过滤,文件管理器很难维护,如果它们都在一个地方,那个地方将不得不保存太多信息。每个模块都应该使用或不使用其父设置来设置自己的记录器(具有自己的处理程序或过滤器)。

非常简单的例子:

# at the very beginning
root = logging.getLogger()
fallback_handler = logging.StreamHandler(stream=sys.stdout)
root.addHandler(fallback_handler)

# first.py
first_logger = logging.getLogger('first')
first_logger.parent = False
# ... set 'first' logger as you wish
class Foo:
    def __init__(self):
        self.logger = logging.getLogger('first.Foo')
    def baz(self):
        self.logger.info("I'm in baz")

# second.py
second_logger = logging.getLogger('first.second') # to use the same settings

# third.py
abstract_logger = logging.getLogger('abs')
abstract_logger.parent = False
# ... set 'abs' logger
third_logger = logging.getLogger('abs.third')
# ... set 'abs.third' particular settings

# fourth.py
fourth_logger = logging.getLogger('abs.fourth')
# [...]

关于python - 设置高级 Python 日志记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9210976/

相关文章:

python - 如何创建月迭代器

c# - 如何将参数添加到 .Net Core 结构化日志记录而不在消息中引用它?

python - Login_required Django 装饰器

Java 加密日志

c# - 将值传递给 nlog 自定义目标

logging - 如何避免在Linux内核中丢失printk日志

java - 以编程方式访问在 Logback 中定义的属性

python - 作为文件夹中的音乐文件,将它们从 .ogg 转换为 .mp3 (python)

python - 用大数组中的值 c 替换从值 a 到 b 的 float

python - R 和 Python 之间加权 t 检验的不同结果