Django 1.8记录器设置被忽略

时间:2015-06-01 21:26:37

标签: python django logging

我从今天早上开始与django伐木战斗,什么都没有......我已经阅读了documentation然后搜索了Google并找到thisthis和我以这样的结局结束:

""" --------------------------------------------------------------------
Django settings for uploader project.
-------------------------------------------------------------------- """
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# ----------------------------------------------------------------------
# Loggers
# ----------------------------------------------------------------------
LOGGING_CONFIG = None
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse',
        },
        'require_debug_true': {
            '()': 'django.utils.log.RequireDebugTrue',
        },
    },
    'formatters': {
        'simple': {
            'format': '[%(asctime)s] %(levelname)s %(message)s',
            'datefmt': '%Y/%m/%d %H:%M:%S',
        },
        'verbose': {
            'format': '[%(asctime)s] %(levelname)s [%(name)s.%(funcName)s.%(lineno)d] %(message)s',
            'datefmt': '%Y/%m/%d %H:%M:%S',
        },
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'filters': ['require_debug_true'],
            'class': 'logging.StreamHandler',
            'formatter': 'simple',
        },
        'development_log': {
            'level': 'DEBUG',
            'filters': ['require_debug_true'],
            'class': 'logging.FileHandler',
            'filename': os.path.join(BASE_DIR, 'logs/development.log'),
            'formatter': 'verbose',
        },
        'production_log': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'logging.FileHandler',
            'filename': os.path.join(BASE_DIR, 'logs/production.log'),
            'formatter': 'simple',
        },
    },
    'loggers': {
        'uploader': {
            'handlers': ['console', 'development_log', 'production_log'],
        },
        'django': {
            'handlers': ['console', 'development_log', 'production_log'],
        },
        'py.warnings': {
            'handlers': ['console', 'development_log'],
        },
    },
}

import logging.config
logging.config.dictConfig(LOGGING)

以及我如何记录自己的数据:

import logging
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
from django.core.urlresolvers import reverse

from .models import MFile
from .forms import MFileForm


# ----------------------------------------------------------------------
def upload(request):
    logger = logging.getLogger(__name__)
    template = 'main/index.html'
    logger.debug('Executing upload() view')
    [...]

已创建文件development.logproduction.log,但它们是空的,控制台显示标准日志记录输出,如:

Performing system checks...

System check identified no issues (0 silenced).
June 01, 2015 - 20:39:23
Django version 1.8.2, using settings 'uploader.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[01/Jun/2015 20:39:29]"GET / HTTP/1.0" 200 2195
[01/Jun/2015 20:40:03]"POST /upload HTTP/1.0" 302 0
[01/Jun/2015 20:40:03]"GET / HTTP/1.0" 200 2195

所以我误解了某些东西或django忽略了那些设置?

所有内容都是在使用

创建的python 3的virtualenv下运行的
python3 -m venv --without-pip venv
source venv/bin/activate
wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py
pip install -r requirements.txt

和requirements.txt:

Django==1.8.2
flake8==2.4.1
gunicorn==19.3.0
mccabe==0.3
pep8==1.5.7
Pillow==2.8.1
pyflakes==0.8.1
wheel==0.24.0

1 个答案:

答案 0 :(得分:1)

您似乎缺少loggers下的应用名称本身的记录器。如果您的项目名称为django_project,请更改以下内容:

'loggers': {
        'uploader': {
            'handlers': ['console', 'development_log', 'production_log'],
        },

为:

'loggers': {
        'uploader': {
            'handlers': ['console', 'development_log', 'production_log'],
        },
        'django_project': {
            'handlers': ['console', 'development_log', 'production_log'],
        },
        [....]

我通常会创建一个“applogger”作为dict实例,以便在本节之前快速访问。

applogger = {
    'handlers': ['console', 'development_log', 'production_log'],
    'level': 'DEBUG',
    'propagate': True,
}

最后将loggers部分修改为如下内容:

'loggers': {
       'uploader': {
                'handlers': ['console', 'development_log', 'production_log'],
            },
        'django_project': applogger,
        'app_name_1': applogger,
        [....]