Search code examples
pythondjangoapache

Django logging not working in apache.?


I configured django logging and run ./manage.py runserver with DEBUG = False and everything worked fine. But as soon as I access my server through the URL, Apache raises a 500 error and here are the last lines of the error.log

ValueError: Unable to configure handler 'db_handler': [Errno 2] N
o such file or directory: '/logs/django_db.log'

Here is snippet from the settings.py file that declares db_handler.

    #settings.py
    ...
    'db_handler': {
        'level':'DEBUG',
        'class':'logging.handlers.RotatingFileHandler',
        'filename': 'logs/django_db.log',
        'maxBytes': 1024*1024*5, # 5 MB
        'backupCount': 5,
        'formatter':'standard',
    },
    ...

I am not sure what am I doing wrong. I've also created a directory named logs in the parent directory of the directory containing settings.py and when I ./manage.py runserver it works fine and creates the file in logs folder. I am confused.


Solution

  • All right as the other recommended. I have to point to an absolute path. So here is what I did in settings.py

    settings.py

        'db_handler': {
            'level':'DEBUG',
            'class':'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(BASE_DIR, 'logs/django_db.log'),
            'maxBytes': 1024*1024*5, # 5 MB
            'backupCount': 5,
            'formatter':'standard',
        }
    

    and then in the parent directory I did this.

    chmod 666 logs/*.log
    

    Every thing is good now.. :)