【问题标题】:Django log errors and traceback in file in production environment生产环境中文件中的 Django 日志错误和回溯
【发布时间】:2021-07-28 00:20:13
【问题描述】:

我是登录 django 的新手,我正在对应用程序进行生产部署 (DEBUG=False),但我不允许使用像 Sentry 这样的任何错误跟踪器来捕获可能的问题,所以我是想知道是否可以使用记录器将生产环境中的回溯或其中的一部分记录到文件中。

我在生产设置中做了这样的事情:

import logging.config
# Clear prev config
LOGGING_CONFIG = None

# Get loglevel from env
LOGLEVEL = os.getenv('DJANGO_LOGLEVEL', 'debug').upper()

logging.config.dictConfig(
        {
            'version': 1,
            'disable_existing_loggers': False,
            'filters': {
                'require_debug_false': {
                    '()': 'django.utils.log.RequireDebugFalse',
                },
                'require_debug_true': {
                    '()': 'django.utils.log.RequireDebugTrue',
                },
            },

            'handlers': {
                'console': {
                    'level': 'INFO',
                    'filters': ['require_debug_false'],
                    'class': 'logging.StreamHandler',
                },
                'file': {
                    'level': 'DEBUG',
                    'class': 'logging.FileHandler',
                    'filename': 'debug.log',
                },
            },
            'loggers': {
                "": {
                    "level": "ERROR",
                    "handlers": ["console", "file"],
                },

                'django': {
                    'handlers': ['console', 'file'],
                    'level': 'DEBUG',
                    'propagate': True,
                },
                'django.request': {
                    'handlers': ['console', 'file'],
                    'propagate': True,
                    'level': 'ERROR',
                },
            }
        }
    )

但我在 debug.log 上得到的唯一信息是:

Internal Server Error: /job/create/
Internal Server Error: /job/create/
Internal Server Error: /job/create/

在没有任何其他信息的情况下将日志记录到文件中,我想在 debug.log 中包含一些有意义的信息,以了解错误可能在哪里,这是我想要得到的:

[2021-07-27 23:47:29 +0000] [ERROR] Internal Server Error: /job/create/ 
AttributeError
'str' object has no attribute 'strftime'
jobs/views.py in CreateJob at line 29
[2021-07-27 23:49:18 +0000] [ERROR] Internal Server Error: /job/create/ 
AttributeError
'str' object has no attribute 'strftime'
jobs/views.py in CreateJob at line 29

有没有办法通过日志记录来实现?或者如果没有像 Sentry 这样的服务在生产环境中可能出现错误,我如何获得回溯或回溯的恢复?

【问题讨论】:

    标签: python django


    【解决方案1】:

    您可以定义日志格式并将其添加到您的处理程序。

    对于您的示例,这可能如下所示:

    'formatters': {
        'mystyle': {
            'format': u'%(asctime)s %(levelname)-6s %(lineno)-4s%(name)-15s %(message)s',
            'datefmt': '%H:%M:%S',
        },
    'handlers': {
        'console': {...},
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': 'debug.log',
            'format': 'mystyle',
        },
    },
    

    格式化程序在自己的部分中定义,以使其可重用。 IE。多个处理程序可以使用相同的格式化程序。 可以在此处找到可以在格式中用作占位符的列表(我认为也有类似堆栈跟踪的内容):

    https://docs.python.org/3/library/logging.html#logrecord-attributes

    【讨论】:

      猜你喜欢
      • 2015-10-30
      • 2018-10-30
      • 2012-03-05
      • 2012-05-03
      • 2021-09-27
      • 1970-01-01
      • 1970-01-01
      • 2012-11-23
      • 2020-09-13
      相关资源
      最近更新 更多