Sentry使用

Sentry使用

以django为例.实际上sentry本身文档已经有介绍了.这里只是再总结

1、全局异常捕获

此方法可以全局捕获任何的异常(甚至包括你自己raise的异常),在实际使用过程中不太推荐.但胜在快速推广.

settings
INSTALLED_APPS = (
    "raven.contrib.django.raven_compat",
)
RAVEN_CONFIG = {
    "dsn": "http://9ece3c3a511a456c88d76905e0b07f46:3f9a1c77e4cb4f91814a5d83de2f67c4@172.17.102.29:9000/2",
    # If you are using git, you can also automatically configure the
    # release based on the git info.
    # 'release': raven.fetch_git_sha(os.path.abspath(os.pardir)),
}
只需要这样配置即可全局捕获异常.release字段是获取git并加密的,通常容易报错.注销即可

2、配合logging

最推荐的一种使用方法,当然前提是要有好的日志习惯.

settings
RAVEN_CONFIG = {
    "dsn": "http://9ece3c3a511a456c88d76905e0b07f46:3f9a1c77e4cb4f91814a5d83de2f67c4@172.17.102.29:9000/2",
    # If you are using git, you can also automatically configure the
    # release based on the git info.
    # 'release': raven.fetch_git_sha(os.path.abspath(os.pardir)),
}

LOGGING = {
    "version": 1,
    "disable_existing_loggers": True,
    "root": {
        "level": "WARNING",
        "handlers": ["sentry"],
    },
    "formatters": {
        "verbose": {
            "format": "%(levelname)s  %(asctime)s  %(module)s "
                      "%(process)d  %(thread)d  %(message)s"
        },
    },
    "handlers": {
        "sentry": {
            "level": "ERROR",
            "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,
        },
    },
}
使用:
        try:
            1 / 0
        except Exception as e:
            logger.exception(e)

3、临时调用(类python语言调用):

这种方法适合用于django调用其他python文件或python脚本中使用

from raven import Client
client = Client(dsn="http://9ece3c3a511a456c88d76905e0b07f46:3f9a1c77e4cb4f91814a5d83de2f67c4@172.17.102.29:9000/2")
try:
    1/0
except Exception as e:
    client.captureException()
223916_bL9y_2663968.jpg
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值