gpt4 book ai didi

python - Django 1.8 记录器设置被忽略

转载 作者:太空宇宙 更新时间:2023-11-03 17:40:46 37 4
gpt4 key购买 nike

从今天早上开始,我就在与 django 日志记录作斗争,但什么也没有......我读过 documentation然后谷歌搜索发现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

和要求.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

最佳答案

您似乎在 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,
[....]

关于python - Django 1.8 记录器设置被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30583590/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com