Sentry - 如何仅记录异常,而不记录记录器

时间:2015-11-03 09:58:53

标签: python logging sentry raven

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

# 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

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

如果记录级别严重,则不会显示任何内容。

那么,是否有一种方法只记录异常,而不是常规日志?

1 个答案:

答案 0 :(得分:2)

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

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

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

相关问题