Mahammad Adil Azeem
Mahammad Adil Azeem

Reputation: 9392

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.

Upvotes: 2

Views: 2327

Answers (4)

Mahammad Adil Azeem
Mahammad Adil Azeem

Reputation: 9392

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.. :)

Upvotes: 1

sid1408
sid1408

Reputation: 424

Maybe Apache is not able to resolve the relative path given inside your settings.py file. To be on the safe side avoid hard coding the link in your settings.py file. You can use os module of python to construct the absolute path to the django_db.log file. For instance I have my SQLite database file path as db/mypy_app.db. Now to construct the absolute path to it, I would write something like this in my settings.py file:

import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
SETTINGS_DIR = os.path.dirname(__file__)
PROJECT_PATH = os.path.join(SETTINGS_DIR, os.pardir)
PROJECT_PATH = os.path.abspath(PROJECT_PATH)
DATABASE_PATH = os.path.join(PROJECT_PATH, 'db/mypy_app.db')
DATABASE_PATH = os.path.abspath(DATABASE_PATH)

And then use DATABASE_PATH variable in your code. Try doing something similar with the logs/django_db.log file. This would work regardless of any system you try it on.

Upvotes: 0

catavaran
catavaran

Reputation: 45575

Seems like your script has the / as the current directory. Try to set the absolute path to log file:

'db_handler': {
    ...
    'filename': '/path/to/logs/django_db.log',
    ...
},

And don't forget about permissions. Very likely that your script runs from www or nobody user.

Upvotes: 1

avenet
avenet

Reputation: 3043

Looks like your Apache server is trying to create this path /logs/django_db.log.

I recommend you setting an absolute path to the logs/django_db.log file.

Just create a file on a path, for example: /var/log/django_db.log and ensure apache user can read and write to that file.

Upvotes: 1

Related Questions