Django中的Airbrake记录器仍然会发送通知,即使其级别设置为“CRITICAL”'?

时间:2018-06-14 21:57:01

标签: python django logging airbrake

我有一个包含多个设置的Django项目;有一个类似于

settings/base.py
# Airbrake (pybrake) settings
AIRBRAKE = dict(
    project_id=os.getenv('AIRBRAKE_PROJECT_ID'),
    project_key=os.getenv('AIRBRAKE_PROJECT_KEY'),
    environment=os.getenv('AIRBRAKE_ENVIRONMENT', default='production'),
    root_directory=os.path.dirname(os.path.dirname(BASE_DIR)))

# Auxiliary variable used in LOGGING
_AIRBRAKE_LOGGER = {
    'handlers': ['airbrake'],
    'level': 'ERROR',
    'propagate': True,
}

# Airbrake logging integration (cf. https://github.com/airbrake/pybrake#django-integration)
# In our case, 'app' is replaced by three apps, 'lucy_web', 'api', and 'activation'.
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'airbrake': {
            'level': 'ERROR',
            'class': 'pybrake.LoggingHandler',
        },
    },
    'loggers': {
        'lucy_web': _AIRBRAKE_LOGGER,
        'api': _AIRBRAKE_LOGGER,
        'activation': _AIRBRAKE_LOGGER,
    },
}

settings/development.py包含

import os
import anyconfig
from lucy.settings.base import *

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

# Set the Airbrake environment to 'development'
AIRBRAKE.update(environment='development')

# Log database errors to sys.stderr
ADDITIONAL_LOGGING = {
    'filters': {
        'require_debug_true': {
            '()': 'django.utils.log.RequireDebugTrue',
        }
    },
    'handlers': {
        'console': {
            'level': os.getenv('LOG_LEVEL', 'ERROR'),
            'filters': ['require_debug_true'],
            'class': 'logging.StreamHandler',
        },
    },
    'loggers': {
        'django.db.backends': {
            'level': os.getenv('LOG_LEVEL', 'ERROR'),
            'handlers': ['console'],
        },
        # Disable Airbrake logging in development
        # (cf. https://github.com/airbrake/pybrake#disabling-pybrake-logs)
        'lucy_web': {'level': 'CRITICAL'},
        'api': {'level': 'CRITICAL'},
        'activation': {'level': 'CRITICAL'},
    }
}

# Update the default Airbrake logging with logging to the console
anyconfig.merge(self=LOGGING, other=ADDITIONAL_LOGGING)

我已使用anyconfig从其基本设置更新每个环境中的LOGGING字典配置架构。

我已经在Django shell中验证了应用级记录器的level确实设置为'CRITICAL'

(venv) Kurts-MacBook-Pro-2:lucy-web kurtpeek$ python manage.py shell
Python 3.6.4 (v3.6.4:d48ecebad5, Dec 18 2017, 21:07:28) 
Type 'copyright', 'credits' or 'license' for more information
IPython 6.3.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from django.conf import settings

In [2]: settings.LOGGING
Out[2]: 
{'version': 1,
 'disable_existing_loggers': False,
 'handlers': {'airbrake': {'level': 'ERROR',
   'class': 'pybrake.LoggingHandler'},
  'console': {'level': 'ERROR',
   'filters': ['require_debug_true'],
   'class': 'logging.StreamHandler'}},
 'loggers': {'lucy_web': {'handlers': ['airbrake'],
   'level': 'CRITICAL',
   'propagate': True},
  'api': {'handlers': ['airbrake'], 'level': 'CRITICAL', 'propagate': True},
  'activation': {'handlers': ['airbrake'],
   'level': 'CRITICAL',
   'propagate': True},
  'django.db.backends': {'level': 'ERROR', 'handlers': ['console']}},
 'filters': {'require_debug_true': {'()': 'django.utils.log.RequireDebugTrue'}}}

我还检查过setLevel方法将字符串'CRITICAL'解释为常量logging.CRITICAL 50

In [7]: import logging

In [8]: logger = logging.getLogger()

In [9]: logger.setLevel('CRITICAL')

In [11]: logger.level
Out[11]: 50

In [12]: logging.CRITICAL
Out[12]: 50

但是,如果我点击一个提升ZeroDivisionError的测试视图,我仍会收到Airbrake通知。但奇怪的是,如果我在使用lucy_weblogging.getLogger(__name__)的{​​{1}}应用中触发视图,我就不会看到Airbrake通知,因此在这种情况下将关卡设置为logging.error()似乎有效。

简而言之,我仍然得到了“将军”这样的“通用”。即使手动提升的错误似乎成功了,也会出现错误。知道这里出了什么问题吗?

0 个答案:

没有答案