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

配置

# 配置工程日志
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': {
            '()': 'django.utils.log.RequireDebugTrue',
        },
    },
    'handlers': { # 日志的处理方式
        'console': {  # 终端输出日志
            'level': 'INFO',  # 大于INFO级别,才输出
            'filters': ['require_debug_true'],
            'class': 'logging.StreamHandler',
            'formatter': 'simple' # 输出简单的样式
        },
        'file': {
            'level': 'INFO',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(os.path.dirname(BASE_DIR) , "logs/meiduo.log"),  # 日志文件的位置
            '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\config.py", line 565, in configure
    handler = self.configure_handler(handlers[name])
  File "D:\study\Python\Anaconda\anaconda\envs\lixianhe\lib\logging\config.py", line 738, in configure_handler
    result = factory(**kwargs)
  File "D:\study\Python\Anaconda\anaconda\envs\lixianhe\lib\logging\handlers.py", line 150, in __init__
    BaseRotatingHandler.__init__(self, filename, mode, encoding, delay)
  File "D:\study\Python\Anaconda\anaconda\envs\lixianhe\lib\logging\handlers.py", line 57, in __init__
    logging.FileHandler.__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(self.baseFilename, self.mode, encoding=self.encoding)
FileNotFoundError: [Errno 2] No such file or directory: 'D:\\study\\Python\\meiduo_mall\\meiduo_mall\\logs\\meiduo.log'

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\autoreload.py", line 228, in wrapper
    fn(*args, **kwargs)
  File "D:\study\Python\Anaconda\anaconda\envs\lixianhe\lib\site-packages\django\core\management\commands\runserver.py", line 116, in inner_run
    autoreload.raise_last_exception()
  File "D:\study\Python\Anaconda\anaconda\envs\lixianhe\lib\site-packages\django\utils\autoreload.py", line 251, in raise_last_exception
    six.reraise(*_exception)
  File "D:\study\Python\Anaconda\anaconda\envs\lixianhe\lib\site-packages\django\utils\six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "D:\study\Python\Anaconda\anaconda\envs\lixianhe\lib\site-packages\django\utils\autoreload.py", 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, settings.LOGGING)
  File "D:\study\Python\Anaconda\anaconda\envs\lixianhe\lib\site-packages\django\utils\log.py", line 75, in configure_logging
    logging_config_func(logging_settings)
  File "D:\study\Python\Anaconda\anaconda\envs\lixianhe\lib\logging\config.py", line 802, in dictConfig
    dictConfigClass(config).configure()
  File "D:\study\Python\Anaconda\anaconda\envs\lixianhe\lib\logging\config.py", 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\\meiduo.log'

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

  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

龙域、白泽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值