Django是否仅发送5xx错误的邮件记录器?

时间:2019-02-07 09:40:57

标签: django logging django-logging

我也想发送有关自定义日志的电子邮件。这是我的日志记录设置。例如:-检出以下代码。

def function(url):
    if some_raise_condition:
        logger = logging.getLogger(__name__)
        kwargs = {'key1': value}
        logger.critical(str(e), kwargs)

我想要上述日志的电子邮件。

这是我的日志记录配置。

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,

    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse',
        },
        'require_debug_true': {
            '()': 'django.utils.log.RequireDebugTrue',
        },
    },

    'formatters': {
        # HAS BEEN COMMENTED IN CASE YOU WANT TO USE SOME IN FUTURE
        # 'verbose': {
        #     'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
        # },
        # 'simple': {
        #     'format': '%(levelname)s %(message)s'
        # },
        # 'timestampthread': {
        #     'format': "%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s] [%(name)-20.20s]  %(message)s",
        # },

        'keyvalue': {
            'format': "APP: %(name)s LEVEL: %(levelname)s PATH: %(pathname)s LINE: %(lineno)d FUNCTION:"
                      "%(funcName)s  TIME: %(asctime)s MESSAGE: %(message)s",
            'datefmt': '%Y-%m-%d %H:%M:%S',

        },
    },
    'handlers': {
        'mail_admins': {
            'level': 'INFO',
            # we don't want to send you mails while in dev mode
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler',
            'formatter': 'keyvalue',

        },

    },
    'loggers': {
        # Mail logger should log message to console as well.
        'django_mailer': {
            'handlers': ['mail_admins', ],
            'propagate': False,
            'level': 'INFO',
        },
    },
}

如何在电子邮件中发送以上日志?

PS:我的邮件配置正确。出现5xx错误时,我会收到日志。

0 个答案:

没有答案