Django logging error: Log file not created

Question:

While running Django project, I am getting the following error:

Unhandled exception in thread started by <function wrapper at 0x7fe72b054b50> 

Traceback (most recent call last):   
File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py", line 229, in wrapper
        fn(*args, **kwargs)   
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/runserver.py", line 107, in inner_run
        autoreload.raise_last_exception()   
File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py", line 252, in raise_last_exception
        six.reraise(*_exception)   
File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py", line 229, in wrapper
        fn(*args, **kwargs)   

File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 17, in setup
        configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)   File "/usr/local/lib/python2.7/dist-packages/django/utils/log.py", line 86, in configure_logging
        logging_config_func(logging_settings)   
File "/usr/lib/python2.7/logging/config.py", line 794, in dictConfig
        dictConfigClass(config).configure()   
File "/usr/lib/python2.7/logging/config.py", line 576, in configure
        '%r: %s' % (name, e)) 
    
ValueError: Unable to configure handler 'ap_migration_log': [Errno 2] No such file or directory: '/home/dg/Desktop/project_dir/django_project/
    module_a/management/commands/log/ap_migration_log'

One solution I found on stackoverflow is to create these files and then this error will not occur. But, why can’t Django create log files on its own when required? Is there any config setting or command to make sure that logging file isn’t created manually?

Asked By: Stuti Verma

||

Answers:

Make sure that if you’re writing the logs to /my/log/dir/log.txt, that path exists along with the empty log.txt file. Also make sure that your directory has the right permissions to perform CRUD operations. Same goes to the file.

It’s best practice to store log files in /var/log/ directory.

Have a look at https://docs.djangoproject.com/en/3.0/topics/logging/#configuring-logging for examples on how Django logging works.

Answered By: Zer0
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.