很抱歉询问这个问题
我正在尝试设置日志以能够测试我的代码; Django pass variable into template
我正在遵循https://docs.djangoproject.com/en/2.1/topics/logging/
中的示例这是他们使用的代码
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'file': {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'filename': r'C:\Users\Sk\Desktop\project\FrounterWeb\FrounterWeb\logs',
},
},
'loggers': {
'django': {
'handlers': ['file'],
'level': 'DEBUG',
'propagate': True,
},
},
}
但是我遇到了这个问题,复制并粘贴并回答我是这个
Microsoft Windows [Version 10.0.10586]
(c) 2015 Microsoft Corporation. All rights reserved.
C:\Users\Sk\Desktop\project\FrounterWeb>py runserver_plus
(null): can't open file 'runserver_plus': [Errno 2] No such file or directory
C:\Users\Sk\Desktop\project\FrounterWeb>py manage.py runserver_plus
Traceback (most recent call last):
File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\logging\config.py", line 555, in configure
handler = self.configure_handler(handlers[name])
File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\logging\config.py", line 728, in configure_handler
result = factory(**kwargs)
File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\logging\__init__.py", line 1041, in __init__
StreamHandler.__init__(self, self._open())
File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\logging\__init__.py", line 1070, in _open
return open(self.baseFilename, self.mode, encoding=self.encoding)
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\Sk\\Desktop\\project\\FrounterWeb\\FrounterWeb\\logs'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\__init__.py", line 357, in execute
django.setup()
File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\site-packages\django\__init__.py", line 19, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\site-packages\django\utils\log.py", line 76, in configure_logging
logging_config_func(logging_settings)
File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\logging\config.py", line 792, in dictConfig
dictConfigClass(config).configure()
File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\logging\config.py", line 563, in configure
'%r' % name) from e
ValueError: Unable to configure handler 'files'
我相信字典会出现此问题,因此我删除了'File':{}和'django':{}字典,但我遇到了另一个问题
新代码:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'file': {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'filename': r'C:\Users\Sk\Desktop\project\FrounterWeb\FrounterWeb\logs',
},
},
'loggers': {
'django': {
'handlers': ['file'],
'level': 'DEBUG',
'propagate': True,
},
},
}
这是问题;
Microsoft Windows [Version 10.0.10586]
(c) 2015 Microsoft Corporation. All rights reserved.
C:\Users\Sk\Desktop\project\FrounterWeb>py runserver_plus
(null): can't open file 'runserver_plus': [Errno 2] No such file or directory
C:\Users\Sk\Desktop\project\FrounterWeb>py manage.py runserver_plus
Traceback (most recent call last):
File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\logging\config.py", line 555, in configure
handler = self.configure_handler(handlers[name])
File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\logging\config.py", line 728, in configure_handler
result = factory(**kwargs)
File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\logging\__init__.py", line 1041, in __init__
StreamHandler.__init__(self, self._open())
File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\logging\__init__.py", line 1070, in _open
return open(self.baseFilename, self.mode, encoding=self.encoding)
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\Sk\\Desktop\\project\\FrounterWeb\\FrounterWeb\\logs'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\__init__.py", line 357, in execute
django.setup()
File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\site-packages\django\__init__.py", line 19, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\site-packages\django\utils\log.py", line 76, in configure_logging
logging_config_func(logging_settings)
File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\logging\config.py", line 792, in dictConfig
dictConfigClass(config).configure()
File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\logging\config.py", line 563, in configure
'%r' % name) from e
ValueError: Unable to configure handler 'files'
C:\Users\Sk\Desktop\project\FrounterWeb>py manage.py runserver_plus
Traceback (most recent call last):
File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\logging\config.py", line 555, in configure
handler = self.configure_handler(handlers[name])
File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\logging\config.py", line 688, in configure_handler
config_copy = dict(config) # for restoring in case of error
ValueError: dictionary update sequence element #0 has length 1; 2 is required
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\__init__.py", line 357, in execute
django.setup()
File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\site-packages\django\__init__.py", line 19, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\site-packages\django\utils\log.py", line 76, in configure_logging
logging_config_func(logging_settings)
File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\logging\config.py", line 792, in dictConfig
dictConfigClass(config).configure()
File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\logging\config.py", line 563, in configure
'%r' % name) from e
ValueError: Unable to configure handler 'class'
此问题意味着它可以读取自己的库
很抱歉麻烦您,但是请您帮我,我真的不知道该怎么做,因为Django告诉我库中自己的构建有问题(我认为)
答案 0 :(得分:1)
您需要将formatter
添加到您的日志记录中:
LOGGING = {
'formatters': {
'verbose': {
'format': '{levelname} {message}',
'style': '{',
},
},
# Rest of the Logging Config
}
答案 1 :(得分:1)
答案非常简单,就像Burhan Khalid所说的那样,该站点的日志路径未绑定该文件的日志路径。
我所做的就是将其添加到我的处理程序中
key
正在工作
犯这么简单的错误我很尴尬