gpt4 book ai didi

python - Django/乌鸦/Sentry : different loggers for different DSNs

转载 作者:太空狗 更新时间:2023-10-29 21:08:27 26 4
gpt4 key购买 nike

如何配置 Django logging 以支持不同 loggers 的不同 DSN?

像这样:

settings.py

  LOGGING = {
..
'handlers': {
'sentry1': {
'level': 'ERROR',
'class': 'raven.contrib.django.handlers.SentryHandler',
'dsn': '<DSN1>',
},
'sentry2': {
'level': 'ERROR',
'class': 'raven.contrib.django.handlers.SentryHandler',
'dsn': '<DSN2>',
},
},
'loggers': {
'sentry1':{
'handlers': ['console', 'sentry1'],
'level': 'ERROR',
'propagate': False,
},
'sentry2':{
'handlers': ['console', 'sentry2'],
'level': 'ERROR',
'propagate': False,
},
}

views.py

import logging
logger = logging.getLogger('sentry2')
logger.error('yeey!')

最佳答案

试试这种对我有用的方法

1) 编写自定义处理程序

from __future__ import absolute_import

import logging

from raven.handlers.logging import SentryHandler


class CustomHandler(SentryHandler):
def __init__(self, *args, **kwargs):
dsn = kwargs.pop('dsn', None)

self.tags = kwargs.pop('tags', None)

logging.Handler.__init__(self, level=kwargs.get('level', logging.NOTSET))

super(CustomHandler, self).__init__(dsn, **kwargs)

2) 更改您的登录设置

LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'verbose': {
'format': '%(levelname)s %(asctime)s %(module)s '
'%(process)d %(thread)d %(message)s'
},
},
'handlers': {
'sentry1': {
'level': 'ERROR',
'class': 'project.sentry.CustomHandler',
'dsn': '<DSN1>',
},
'sentry2': {
'level': 'ERROR',
'class': 'project.sentry.CustomHandler',
'dsn': '<DSN2>',
},
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'verbose'
}
},
'loggers': {
'sentry1': {
'handlers': ['sentry1'],
'level': 'ERROR',
'propagate': False,
},
'sentry2': {
'handlers': ['sentry2'],
'level': 'ERROR',
'propagate': False,
},
}
}

3) 在任何地方记录你的错误

import logging
logger = logging.getLogger('sentry2')
logger.error('yeey!')

它对我有用,用 python3.6 django2 测试过。

关于python - Django/乌鸦/Sentry : different loggers for different DSNs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32513553/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com