【问题标题】:Django logs errorsDjango 记录错误
【发布时间】:2019-04-11 14:19:14
【问题描述】:

很抱歉问这个问题

我正在尝试设置日志以测试我的代码; 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 告诉我在库中自己的构建有问题(我认为)

【问题讨论】:

  • 您是否将/path/to/django/debug.log 替换为实际有效的路径?如果没有,您将遇到这些问题。将其替换为 r'C:\Users\Sk\Desktop\django.log' 之类的路径或其他位置(确保该目录首先存在,并且您有权在其中创建文件)。桌面、文档和下载文件夹是不错的选择。
  • /path/to/django/debug.log;不,因为我尝试使用我的文件夹时不知道要替换什么,但它没有用。后来真的不知道怎么办了,不知道用哪个。
  • 在您拥有settings.py 的同一文件夹中创建一个名为logs 的目录,然后将/path/to/django/debug.log 替换为该目录的完整路径,例如r'C:\Users\Sk\Desktop\FrounterWeb\logs\django.log'
  • burhan khalid,谢谢你给我指路,但它没有用,有这个错误 intsead; SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape 我为这个问题道歉
  • 布尔汉·哈立德;我找到了语法错误的答案,我没有在路径前添加“r”。我觉得好傻

标签: django logging


【解决方案1】:

您需要将formatter 添加到您的日志记录中:

LOGGING = {
'formatters': {
    'verbose': {
         'format': '{levelname} {message}',
         'style': '{',
    },
},
# Rest of the Logging Config
}

【讨论】:

  • 对不起,我不太明白,也许你可以解释一下,但我想:" 'format': {levelname} 'style': '{'," 是自定义日志记录吗?
【解决方案2】:

答案很简单,就像 Burhan Khalid 所说的那样,网站的日志路径没有绑定文件的日志路径。

我所做的只是将它添加到我的处理程序中

'filename': 'zigview\logging_files\debug.log',

并且正在工作

我觉得好尴尬,犯了这么简单的错误

【讨论】:

    猜你喜欢
    • 2011-01-08
    • 1970-01-01
    • 1970-01-01
    • 2017-12-26
    • 2011-09-12
    • 2015-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多