首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Structlog:如何测试由哪个处理程序发出的内容

Structlog:如何测试由哪个处理程序发出的内容
EN

Stack Overflow用户
提问于 2021-07-06 01:15:37
回答 1查看 94关注 0票数 1

我在我的项目中使用了structlog,并且我想(单元)测试哪个处理程序发出了哪个消息。有没有一种规范的方法来做到这一点?我注意到pytest-structlog,但在那里找不到任何这样的功能。或者我可以使用stdlib / pytest中的一些东西?

假设我的最小示例如下

代码语言:javascript
复制
# implementation
import logging.handlers

import structlog

structlog.configure(
    wrapper_class=structlog.make_filtering_bound_logger(logging.NOTSET),
    context_class=dict,
    logger_factory=structlog.stdlib.LoggerFactory(),
    cache_logger_on_first_use=False,
)

h1 = logging.StreamHandler()
h1.setLevel(logging.ERROR)

h2 = logging.StreamHandler()
h2.setLevel(logging.DEBUG)

logging.root.addHandler(h1)
logging.root.addHandler(h2)


# test
import structlog

from dummy import minimal


def test_minimal(log):
    logger = structlog.getLogger()

    logger.warn("I am a warning.")
    logger.error("I am an error.")

    assert log.has("I am a warning.")
    assert log.has("I am an error.")

    # how to test what has been emitted by which handler?
    # assert not log.handler1.has("I am a warning.")
EN

回答 1

Stack Overflow用户

发布于 2021-07-06 01:48:39

您可以在https://www.structlog.org/en/stable/testing.html中找到有关structlog的测试成本的文档

捕获部分可以使用capture_logs (上下文管理器)或CapturingLogger (低级)来完成。

要获得发射记录器的名称,请确保添加了add_logger_name处理器。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68259893

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档