嗨,我有一个使用 Django 2.2.28 和 python 3.5.2 的 Django 项目。
除了我正在尝试调试的一个功能外,实时站点运行良好。
我以前使用过日志记录,但由于某种原因它现在不起作用。
这是我的代码...
我将此文件称为 wsgi.py。所有设置都可以正常工作,但
LOGGING
却不然。
from .settings import *
DEBUG = False
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'file': {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'filename': '/home/myuser/proj_container/debug.log',
},
},
'loggers': {
'django.request': {
'handlers': ['file'],
'level': 'DEBUG',
'propagate': False,
},
},
}
import logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
def some_func(request):
...
if some_condition:
logger.debug('In some_condition block')
elif some_other_condition:
logger.debug('In some_other_condition block')
我已经尝试了上述方法,仅使用
production_settings.py
中的设置...没有任何显示。
与 1 相同,但在每个重要代码块处导入并调用
logging
。
我阅读了logger.debug()
LOGGING
从
production_settings.py
移动到
settings.py
...昨天我修复了一个错误,其中django没有覆盖来自
DATABASES
的
settings.py
中的
production_settings.py
变量,而是创建了一个sqlite3数据库并给出来自
settings.py
的错误,修复它的唯一方法就是这样做。
(绑定的unix套接字服务,因为有超过1个站点在此服务器上运行,并且除了这个问题之外,两个站点都工作正常在其中之一)。它们的缓存值不冲突,我的文件路径是正确的,等等。
这之前在其他项目上效果很好,但我不知道为什么现在不行。
谢谢!
无论如何,对于也遇到此问题的任何人来说,这里都有一个解决方案。
只需确保您的 Linux 用户有权写入以下文件路径。
视图.pyimport logging
logging.basicConfig(level=logging.DEBUG, filename='/home/myuser/path/to/debug.log') # will append by default, docs allow other values too.
logger = logging.getLogger(__name__)
def some_func(request):
...
if some_condition:
logger.debug('In some_condition block')
logger.debug('Object type == {}'.format(type(some_condition.attribute['key']['nested_key'])))
logger.debug('\n\n')
# etc
elif some_other_condition:
logger.debug('In some_other_condition block')
settings.py 或 production_settings.py# LOGGING = {...} # is not necessary since the above does it fine. Probably just a bug like I mentioned with DATABASES.