简体   繁体   English

Django 记录请求

[英]Django logging requests

I'm using django 1.11 in aws elastic beanstalk and I've been trying to get my app to log with no luck .我在aws elastic beanstalk 中使用 django 1.11 并且我一直试图让我的应用程序在没有运气的情况下登录。 . . . .

I have these in my settings.py我的 settings.py 中有这些

LOGGING_CONFIG = None
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': '/opt/python/current/app/staging.log',
        },
    },
    'loggers': {
        '': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': True,
        },
        'django': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': True,
        },
        'django.request': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': True,
        },
    },
}
import logging.config
logging.config.dictConfig(LOGGING)

then I try to call one of my API and expecting something would show up in the log but it didn't.然后我尝试调用我的一个 API 并期望日志中显示某些内容,但它没有。 I django.request logger supposed to automatically log all incoming request or do I have to create a middleware that does logger.debug('something') ?django.request logger 应该自动记录所有传入的请求还是我必须创建一个中间件来执行logger.debug('something')

logging in django is a lot more complicated than I thought :(登录 django 比我想象的要复杂得多:(

Django logger extensions work differently in development than in production (settings.DEBUG=True|False). Django 记录器扩展在开发和生产中的工作方式不同(settings.DEBUG=True|False)。

What you expect to see - http request logs - are written by django.server logger,您希望看到的 - http 请求日志 - 由django.server记录器编写,
which is active only in development server (runserver command).仅在开发服务器中有效(runserver 命令)。

I see that in your configuration you use django.request logger.我看到在您的配置中您使用了django.request记录器。 This logger name is very confusing - because as its name suggest - users will expect it to log all http requests - but it won't.这个记录器名称非常令人困惑 - 因为顾名思义 - 用户会期望它记录所有 http 请求 - 但它不会。

django.request will log only 4xx and 5xx requests. django.request只会记录 4xx 和 5xx 请求。

I've made a screencast which explain this issue in detail.我制作了一个截屏视频,详细解释了这个问题。

Django documentation about loggers.关于记录器的 Django 文档。

demo of logging in django,log is location in /path/to/your/project/log/info.log , your need to create info.log in log/ dir first.登录django的演示,日志位于/path/to/your/project/log/info.log ,您需要先在 log/ dir 中创建 info.log 。

settings.py设置.py

LOG_PATH = os.path.join(BASE_DIR, "log/")
LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'standard': {
            'format': '%(asctime)s [%(levelname)s]- %(message)s'}

    },
    'handlers': {
        'django_error': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': LOG_PATH + 'django.log',
            'formatter': 'standard'
        },
        'info': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': LOG_PATH + 'info.log',
            'formatter': 'standard'
        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'standard'
        }
    },
    'loggers': {
        'info': {
            'handlers': ['info', "console"],
            'level': 'DEBUG',
            'propagate': True
        },
        'django': {
            'handlers': ['console'],
            'level': 'INFO',
            'propagate': True,
        },
        'django.request': {
            'handlers': ['django_error', 'console'],
            'level': 'DEBUG',
            'propagate': True,
        },
        'django.db.backends': {
            'handlers': ['console'],
            'level': 'ERROR',
            'propagate': True,
        }
    },
}

the effect of loggers is:记录器的作用是:

  • info: your custom debug info info:您的自定义调试信息
  • django: the request record django:请求记录
  • django.request: the error request django.request:错误请求
  • django.db.backends:Messages relating to the interaction of code with the database django.db.backends:与代码与数据库交互相关的消息

more info here更多信息在这里

views.py视图.py

import logging
logger = logging.getLogger("info")
logger.info('something')

you will get你会得到

2017-08-31 13:05:40,492 [INFO]- something

in log/info.log later.稍后在 log/info.log 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM