Django配置日志报错:ValueError: Unable to configure handler ‘file‘: [Errno 2] No such file or directory

时间:2025-04-12 15:10:53

配置

# 配置工程日志
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False, # 是否禁用已经存在的日志器
    'formatters': {  # 输出日志的格式
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(lineno)d %(message)s'
        },
        'simple': {
            'format': '%(levelname)s %(module)s %(lineno)d %(message)s'
        },
    },
    'filters': {  # 对日志进行过滤
        'require_debug_true': {
            '()': '',
        },
    },
    'handlers': { # 日志的处理方式
        'console': {  # 终端输出日志
            'level': 'INFO',  # 大于INFO级别,才输出
            'filters': ['require_debug_true'],
            'class': '',
            'formatter': 'simple' # 输出简单的样式
        },
        'file': {
            'level': 'INFO',
            'class': '',
            'filename': ((BASE_DIR) , "logs/"),  # 日志文件的位置
            'maxBytes': 300 * 1024 * 1024,  # 文件最多存储 300M 的内存   日志文件满了,他会自动新建meiduo1 meiduo2
            'backupCount': 10, # 最多十个文件
            'formatter': 'verbose'
        },
    },
    'loggers': {
        'django': {  # 定义了一个名为django的日志器
            'handlers': ['console', 'file'], # 可以同时在终端跟文件中输出
            'propagate': True,
            'level;':'INFO' # 日至输出的最低级别
        },
    }
}

错误信息

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x0000025C40F9F2F0>
Traceback (most recent call last):
  File "D:\study\Python\Anaconda\anaconda\envs\lixianhe\lib\logging\", line 565, in configure
    handler = self.configure_handler(handlers[name])
  File "D:\study\Python\Anaconda\anaconda\envs\lixianhe\lib\logging\", line 738, in configure_handler
    result = factory(**kwargs)
  File "D:\study\Python\Anaconda\anaconda\envs\lixianhe\lib\logging\", line 150, in __init__
    BaseRotatingHandler.__init__(self, filename, mode, encoding, delay)
  File "D:\study\Python\Anaconda\anaconda\envs\lixianhe\lib\logging\", line 57, in __init__
    .__init__(self, filename, mode, encoding, delay)
  File "D:\study\Python\Anaconda\anaconda\envs\lixianhe\lib\logging\__init__.py", line 1032, in __init__
    StreamHandler.__init__(self, self._open())
  File "D:\study\Python\Anaconda\anaconda\envs\lixianhe\lib\logging\__init__.py", line 1061, in _open
    return open(, , encoding=)
FileNotFoundError: [Errno 2] No such file or directory: 'D:\\study\\Python\\meiduo_mall\\meiduo_mall\\logs\\'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\study\Python\Anaconda\anaconda\envs\lixianhe\lib\site-packages\django\utils\", line 228, in wrapper
    fn(*args, **kwargs)
  File "D:\study\Python\Anaconda\anaconda\envs\lixianhe\lib\site-packages\django\core\management\commands\", line 116, in inner_run
    autoreload.raise_last_exception()
  File "D:\study\Python\Anaconda\anaconda\envs\lixianhe\lib\site-packages\django\utils\", line 251, in raise_last_exception
    (*_exception)
  File "D:\study\Python\Anaconda\anaconda\envs\lixianhe\lib\site-packages\django\utils\", line 685, in reraise
    raise value.with_traceback(tb)
  File "D:\study\Python\Anaconda\anaconda\envs\lixianhe\lib\site-packages\django\utils\", line 228, in wrapper
    fn(*args, **kwargs)
  File "D:\study\Python\Anaconda\anaconda\envs\lixianhe\lib\site-packages\django\__init__.py", line 22, in setup
    configure_logging(settings.LOGGING_CONFIG, )
  File "D:\study\Python\Anaconda\anaconda\envs\lixianhe\lib\site-packages\django\utils\", line 75, in configure_logging
    logging_config_func(logging_settings)
  File "D:\study\Python\Anaconda\anaconda\envs\lixianhe\lib\logging\", line 802, in dictConfig
    dictConfigClass(config).configure()
  File "D:\study\Python\Anaconda\anaconda\envs\lixianhe\lib\logging\", line 573, in configure
    '%r: %s' % (name, e))
ValueError: Unable to configure handler 'file': [Errno 2] No such file or directory: 'D:\\study\\Python\\meiduo_mall\\meiduo_mall\\logs\\'

解决:创建配置日志输出位置的文件夹logs,不会自己创建,不创建会报错