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()