Django中使用sentry做错误日志收集

Sentry介绍

Sentry是一个实时事件日志记录和汇集的平台。其专注于错误监控以及提取一切事后处理所需信息而不依赖于麻烦的用户反馈,

sentry本身提供了web界面(基于django),当程序 出现问题,会自动展示在界面上,并且可以包含上下文信息,对于排查问题非常有效。

Sentry安装

Sentry是一个日志平台, 它分为客户端和服务端,

  • 客户端(目前客户端有Python、PHP、C#, Ruby等多种语言)就嵌入在你的应用程序中间,程序出现异常就向服务端发送消息
  • 服务端将消息记录到数据库中并提供一个web节目方便查看。

Sentry由python编写,源码开放,性能卓越,易于扩展,目前著名的用户有Disqus, Path, mozilla, Pinterest等。

官方文档(python):https://docs.sentry.io/clients/python/

配置服务端的时候,按照文档上边 很简单,可是安装过程中,会碰到各种各样奇葩的问题,最终放弃服务端的配置,直接使用另一个同事配置好的服务端。

Sentry配置

有了服务端之后,客户端配置就很简单了,只需要拿到服务器上注册的DSN地址即可,如果没有服务端也没关系,sentry官方提供了在线注册,邮箱注册下用户就行,当然,数据就在人家那里了。。

python配置:
from raven import Client

client = Client('https://<key>:<secret>@sentry.io/<project>')

try:
    1 / 0
except ZeroDivisionError:
    client.captureException()
django配置:

版本要求:django>=1.4
版本要求:django>=1.4

INSTALLED_APPS = (
    'raven.contrib.django.raven_compat',
)

import os
import raven

RAVEN_CONFIG_DEV = {
    'dsn': 'https://<key>:<secret>@sentry.io/<project>',
    # If you are using git, you can also automatically configure the
    # release based on the git info.
    'release': raven.fetch_git_sha(os.path.dirname(os.pardir)),
}

RAVEN_CONFIG_PROD ={}

生产和测试最好分别配置两套sentry,毕竟报错量是不一样的。

测试连通性

测试连通性:python manage.py raven test

日志配置:


LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'root': { 'level': 'WARNING', 'handlers': ['sentry', 'console'], }, 'formatters': { 'verbose': { 'format': '%(levelname)s %(asctime)s %(module)s ' '%(process)d %(thread)d %(message)s' }, }, 'handlers': { 'sentry': { 'level': 'WARNING', # To capture more than ERROR, change to WARNING, INFO, etc. 'class': 'raven.contrib.django.raven_compat.handlers.SentryHandler', 'tags': {'custom-tag': 'x'}, }, 'console': { 'level': 'DEBUG', 'class': 'logging.StreamHandler', 'formatter': 'verbose' } }, 'loggers': { 'django.db.backends': { 'level': 'ERROR', 'handlers': ['console'], 'propagate': False, }, 'raven': { 'level': 'DEBUG', 'handlers': ['console'], 'propagate': False, }, 'sentry.errors': { 'level': 'DEBUG', 'handlers': ['console'], 'propagate': False, }, }, }

Written by

说点什么

欢迎讨论

avatar

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

  Subscribe  
提醒