Django日志记录请求 [英] Django logging requests

查看:80
本文介绍了Django日志记录请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在aws弹性beantalk中使用django 1.11,并且我一直在尝试让我的应用程序顺利登录. .

I'm using django 1.11 in aws elastic beanstalk and I've been trying to get my app to log with no luck . . .

我的设置中有这些.py

I have these in my 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)

然后我尝试调用我的API之一,并期望某些内容会出现在日志中,但事实并非如此.我django.request记录程序应该自动记录所有传入的请求,还是我必须创建一个执行logger.debug('something')的中间件?

then I try to call one of my API and expecting something would show up in the log but it didn't. 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中登录比我想象的要复杂得多:(

logging in django is a lot more complicated than I thought :(

推荐答案

在django中登录的演示,日志是/path/to/your/project/log/info.log中的位置, 您需要先在log/dir中创建info.log.

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.

settings.py

settings.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,
        }
    },
}

记录器的作用是:

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

更多信息此处

views.py

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

您将获得

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

稍后在log/info.log中.

in log/info.log later.

这篇关于Django日志记录请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆