【问题标题】:Sentry - how to log only exceptions, not loggerSentry - 如何仅记录异常,而不是记录器
【发布时间】:2015-11-03 09:58:53
【问题描述】:

我想使用哨兵记录测试异常。所以我配置了它:

# tests/__init__.py

from raven import Client
from raven.conf import setup_logging
from raven.handlers.logging import SentryHandler

client = Client(dsn='here goes dsn')
handler = SentryHandler(client, level=logging.ERROR)
setup_logging(handler)

当我运行测试时:

# tests/test_lolz.py

logger = logging.getLogger(__name__)

def test_log():
    logger.warning('do not want to see this - warn')
    logger.error('do not want to see this - error')
    1 / 0  # yolo

我在哨兵仪表板中看到:记录器错误和异常

记录级别为critical时,什么都不会出现。

那么,有没有一种方法可以只记录异常而不记录常规日志?

【问题讨论】:

    标签: python logging sentry raven


    【解决方案1】:

    Sentry 目前不提供“仅捕获附加异常的日志事件”的方法,但您可以为其编写 logging.Filter。 Python 文档有点稀疏,但这里有一个过滤器示例:

    https://docs.python.org/2/howto/logging-cookbook.html#using-filters-to-impart-contextual-information

    您基本上想检测条目上是否存在异常信息,如果存在,则返回 True(告诉它捕获条目)。

    【讨论】:

    • 感谢您的回复!我通过做两件事解决了问题:1. 记录异常的鼻子插件,2. 简单的过滤器,就像你说的那样。此解决方案工作正常。谢谢!
    猜你喜欢
    • 2021-09-02
    • 2015-04-10
    • 2020-10-26
    • 2021-04-24
    • 2014-02-28
    • 2020-10-01
    • 1970-01-01
    • 2016-12-09
    • 2021-04-29
    相关资源
    最近更新 更多