メニュー 閉じる

Python Djangoでlogフォルダが無いときのエラー

python manage.py startappやpython manage.py runserverで、
以下のエラーが発生したときはlogフォルダを作成してあげると良いです。

 

Djangoプロジェクトにlogフォルダがないときのエラー

(django) linda@lindaMBP hirameki % python manage.py runserver
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/Users/linda/.pyenv/versions/3.7.4/lib/python3.7/logging/config.py", line 562, in configure
handler = self.configure_handler(handlers[name])
File "/Users/linda/.pyenv/versions/3.7.4/lib/python3.7/logging/config.py", line 735, in configure_handler
result = factory(**kwargs)
File "/Users/linda/.pyenv/versions/3.7.4/lib/python3.7/logging/handlers.py", line 200, in __init__
BaseRotatingHandler.__init__(self, filename, 'a', encoding, delay)
File "/Users/linda/.pyenv/versions/3.7.4/lib/python3.7/logging/handlers.py", line 55, in __init__
logging.FileHandler.__init__(self, filename, mode, encoding, delay)
File "/Users/linda/.pyenv/versions/3.7.4/lib/python3.7/logging/__init__.py", line 1087, in __init__
StreamHandler.__init__(self, self._open())
File "/Users/linda/.pyenv/versions/3.7.4/lib/python3.7/logging/__init__.py", line 1116, in _open
return open(self.baseFilename, self.mode, encoding=self.encoding)
FileNotFoundError: [Errno 2] No such file or directory: '/Users/linda/cfn/hirameki/log/app.log'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/Users/linda/.pyenv/versions/3.7.4/lib/python3.7/threading.py", line 926, in _bootstrap_inner
self.run()
File "/Users/linda/.pyenv/versions/3.7.4/lib/python3.7/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "/Users/linda/django/lib/python3.7/site-packages/django/utils/autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "/Users/linda/django/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 110, in inner_run
autoreload.raise_last_exception()
File "/Users/linda/django/lib/python3.7/site-packages/django/utils/autoreload.py", line 76, in raise_last_exception
raise _exception[1]
File "/Users/linda/django/lib/python3.7/site-packages/django/core/management/__init__.py", line 357, in execute
autoreload.check_errors(django.setup)()
File "/Users/linda/django/lib/python3.7/site-packages/django/utils/autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "/Users/linda/django/lib/python3.7/site-packages/django/__init__.py", line 19, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "/Users/linda/django/lib/python3.7/site-packages/django/utils/log.py", line 75, in configure_logging
logging_config_func(logging_settings)
File "/Users/linda/.pyenv/versions/3.7.4/lib/python3.7/logging/config.py", line 799, in dictConfig
dictConfigClass(config).configure()
File "/Users/linda/.pyenv/versions/3.7.4/lib/python3.7/logging/config.py", line 570, in configure
'%r' % name) from e
ValueError: Unable to configure handler 'file'

注目するところはここでした。

FileNotFoundError: [Errno 2] No such file or directory: '/Users/linda/cfn/hirameki/log/app.log'

Djangoプロジェクトの直下にmanage.pyというファイルが作成されていますが、
それと同じ階層にlogを保存するためのフォルダを作成することで解決します。

私はapp.logまで作成してしまいましたが中身のlogファイルは、
setting.py作成しなくてもOKだそうです。

 

今回のエラーで参考にしたサイト

ValueError: Unable to configure handler ‘file’: [Errno 2] No such file or directory:
https://stackoverflow.com/questions/39101488/valueerror-unable-to-configure-handler-file-errno-2-no-such-file-or-direct