Django Logging - Log File from Handler Not Found

2019-07-20 16:01发布

I have logging set up in my Django project. It works locally in development but it causing trouble in production. settings.py bits are as follows:

import os

PROJECT_PATH = os.path.abspath(os.path.dirname(__name__))

. . .

'handlers': {
    'log_file': {
        'level': 'INFO',
        'class': 'logging.handlers.RotatingFileHandler',
        'filename': os.path.join(PROJECT_PATH, 'logs/django.log'),
        'maxBytes': '16777216',
        'formatter': 'verbose'
    },

. . .

I keep getting this error in /var/log/apache2/error.log:

ValueError: Unable to configure handler 'log_file': [Errno 2] No such file or directory: '/logs/django.log'

I've tested this in the Django shell by running the code and setting PROJECT_PATH manually and it seems to return the correct path. Plus, I've checked and checked the permissions on the log file. Although unnecessary, I changed it to 777 just to test. Still no luck.

My local log fills up with all that I expect while the production log is empty.

In thinking about this, I suppose this log location is probably not the best, but if it worked in production, at least I'd know... Then, I could work on changing the location.

What am I missing?

1条回答
戒情不戒烟
2楼-- · 2019-07-20 16:08

Although I would really like to get an answer to this, I have decided to take another approach so I can move on. It seems this question is not spurring enough attention and I don't feel like a bounty right now, so I disabled logging to a file for now. I am going to redesign my approach anyway, so this seems like a decent idea.

What I came up with temporarily that seems to work and not give errors is this (for the file handler definition):

'file': {
    'level': 'DEBUG',
    'class': 'logging.FileHandler',
    'filename': '/some/fixed/path/to/logs/django.log',
},

Note that before I was using a value derived from os.path.join(PROJECT_PATH, 'logs/django.log'). Not sure why this didn't work, but it was the culprit. So even though this bit was correct: PROJECT_PATH = os.path.abspath(os.path.dirname(__name__)) (even confirmed in the python shell), it wasn't working in production.

查看更多
登录 后发表回答