简体   繁体   中英

apache2 with django - can't write to log file

I have django running on apache2 server.

my settings.py looks like this:

WSGI_APPLICATION = 'my_app.wsgi.application'
...

LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
    'verbose': {
        'format': '%(levelname)s %(asctime)s %(module)s: %(message)s'
    }
},
'handlers': {
    'file': {
        'level': 'INFO',
        'class': 'logging.handlers.RotatingFileHandler',
        'filename': '../django.log',
        'formatter': 'verbose',
        'maxBytes': 1024 * 1024 * 5,  # 5 MB
        'backupCount': 5,
    },
},
'loggers': {
    'my_app': {
        'handlers': ['file'],
        'level': 'INFO',
        'propagate': True,
    },
},

}

My idea was basically to create a log that all the components of my django app will write to, with level INFO.

The issue I am facing is, when running the server, the log is created with root permissions:

 ll ../django.log 
 -rw-r--r-- 1 root root 0 Jan  9 10:17 ../django.log

And so, what happens when I try to log to it is:

/var/log/apache2/error.log

[Wed Jan 09 11:37:43.677755 2019] [:error] [pid 1457:tid 140554321598208] [remote 192.168.254.52:60257] ValueError: Unable to configure handler 'file': [Errno 13] Permission denied: '../django.log'

I did find those issues: Permission Denied when writing log file and Django: Setup an efficient logging system for a website in production .

and if I change the permissions of the file to be owned by www-data and not root , it works.

My question is - where is the right place to set this in production? should it be changing it manually? maybe somewhere in the settings.py or the apache2-config ?

I am looking for the best practice of django logging.

EDIT: ps aux | grep apache2 shows:

root      1444  0.0  0.0  97916  7452 ?        Ss   13:22   0:00 /usr/sbin/apache2 -k start

ps aux | grep wsgi shows:

www-data  1447  0.0  0.2 510528 23692 ?        Sl   13:22   0:00 (wsgi:name -k start

Thanks!

事先在服务器中创建一个具有www-data权限的日志文件夹,并将其路径保存在环境变量中,在settings.py文件中使用python的os.getenv('LOG_PATH')使用该路径。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM