I have a django 1.4.2 application logging going to a rotating files. In my settings.py I have:

LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
    'standard': {
        'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s'
    },
},
'handlers': {
    'default': {
        'level':'DEBUG',
        'class':'logging.handlers.RotatingFileHandler',
        'filename': '/var/www/html/logs/mylog.log',
        'maxBytes': 1024*1024*5, # 5 MB
        'backupCount': 5,
        'formatter':'standard',
    },
    'request_handler': {
            'level':'DEBUG',
            'class':'logging.handlers.RotatingFileHandler',
            'filename': '/var/www/html/logs/django_request.log',
            'maxBytes': 1024*1024*5, # 5 MB
            'backupCount': 5,
            'formatter':'standard',
    },
},
'loggers': {
    '': {
        'handlers': ['default'],
        'level': 'ERROR',
        'propagate': True
    },
    'django.request': { # Stop SQL debug from logging to main logger
        'handlers': ['request_handler'],
        'level': 'DEBUG',
        'propagate': False
    },
}

}

So in the logging directory I see the files:

mc.log
mc.log.1
mc.log.2
mc.log.3
mc.log.4
mc.log.5

When mc.log reach 5M the files are correctly rotated but the new mc.log is created with ownership root.root. Since apache is running under apache user, it cannot access anymore to the files and the application stops working. Any idea why the new log is created with root.root ownership instead of apache.apache?

Thanks

  • 2
    I'm seeing this behaviour too. Did you ever figure it out? – fluffels Jun 26 '15 at 12:38
  • I was having this problem too. I have configured wsgi to run as the user apache and everything was fine until the log rotated and Django started throwing 500 internal serve errors because apache could not write to the log file. I suspected something was wrong in my apache conf or in my Django logging code. It turned out that I have some cronjobs that run custom Django management commands and these cronjobs ran as root and also logged a lot of output to the log file. This would cause the log files to rotate while the user was root! I changed my cron job to run as apache and all is well now. – Red Cricket Aug 30 at 1:29

New files can be created if the user/group of the parent directory allows it. I believe you either need to change the owner of the directory, or add a group to the directory which includes apache users, or use some advanced technique such as ACL.

To test it out, try the following: Login as root. Switch to apache user. Try to create a file manually. Switch back to root, change the permissions/ownership of the folder, switch to apache user, try again. This should give you some more info about whether the script will fail while trying to create the file.

Finally, it is a bit counterintuitive, make sure that apache user somehow has the execute permission in that directory, otherwise it won't allow you to cd into that.

Also, I think you will need to set the "s" bit of the directory (chmod g+s or chmod 2755 etc), so that newly added files inherit the permissions of the directory. Then, you need to make sure that the group's s bit is set, and the group owns the directory. (or you can set the group of the directory to www-data).

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.