Gunicorn:访问日志到文件而不是Stndout

时间:2019-02-05 03:49:36

标签: django gunicorn digital-ocean

我正在使用以下版本的Python,Django和Gunicorn使用Digitalocean一键式Django服务器部署。

gunicorn (version 19.7.1)

python 2.7

我似乎无法弄清楚如何将访问日志记录到文件中。以下是我的gunicorn.service文件。

[Unit]
Description=Gunicorn daemon for Django Project
Before=nginx.service
After=network.target

[Service]
WorkingDirectory=/home/django/egre
ExecStart=/usr/bin/gunicorn --name=egre --pythonpath=/home/django/egre --bind unix:/home/django/gunicorn.socket --config /etc/gunicorn.d/gunicorn.py --access-logfile /var/log/emperor.log eGRE.wsgi:application
Restart=always
SyslogIdentifier=gunicorn
User=django
Group=django


[Install]
WantedBy=multi-user.target

https://docs.gunicorn.org/en/19.7.1/settings.html#logging

按照上述说明操作后,我尝试使用--access-logfile=-,该方法可以正常工作,但在通过相对路径后不会将stndout记录到文件中,因此,gunicorn不会重新启动并给出退出代码1。

如何将日志写入emperor.log?

1 个答案:

答案 0 :(得分:1)

我认为您可以使用django's logging这样尝试:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': '/path/to/logger/gunicorn_access.log',
        },
    },
    'loggers': {
        'gunicorn.access': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': True,
        },
    },
}
相关问题