在uwsgi中托管多个进程时,某些django的日志丢失 [英] some django's logs are missing when host in uwsgi with multiple process

查看:324
本文介绍了在uwsgi中托管多个进程时,某些django的日志丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将django + uwsgi用于Web项目.但是我发现在uwsgi运行一段时间后,一些django日志会丢失!

I'm using django+uwsgi for a web project. But I found some django logs would be missing after uwsgi is running for a while!

情况是:我用8个进程配置了uwsgi.当我启动uwsgi时,所有django日志都将写入单个日志文件中.但是几个小时后,一些日志未写入文件中.我将django日志文件与uwsgi日志文件进行了比较.我发现只有一个uwsgi进程的请求是用Django文件编写的.其他7个进程的django日志丢失.重新启动uwsgi时,结果相同.

The situation is that: I config the uwsgi with 8 process. When I start the uwsgi, all the django logs would be written in single log file. But after a few hours, some logs are not written in file. I compared the django log file to uwsgi log file. I found only one uwsgi process's requests were written in django file. The other 7 process's django logs were missing. When I restart the uwsgi, it is the same result.

有人知道吗?

谢谢

我的django日志配置:

my django logging config:

LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
    'simple': {
        'format': '%(levelname)s %(asctime)s %(message)s'
    },
    'detail': {
        'format': '%(levelname)s %(asctime)s [%(module)s.%(funcName)s line:%(lineno)d] %(message)s',
    },
},
'handlers': {
    'file': {
        'level': 'INFO',
        'formatter': 'simple',
        'class': 'logging.handlers.TimedRotatingFileHandler',
        'filename': LOG_FILE,
        'when': 'midnight',
        'backupCount': 366,
    },
    'err_file': {
        'level': 'WARN',
        'formatter': 'detail',
        'class': 'logging.handlers.TimedRotatingFileHandler',
        'filename': LOG_ERR_FILE,
        'when': 'midnight',
        'backupCount': 366,
    },
},
'loggers': {
    'django_request': {
        'handlers': ['file', 'err_file'],
        'level': 'DEBUG',
        'propagate': True,
    },
}
}

我的uwsgi配置:

<uwsgi>
<socket>0.0.0.0:8888</socket>
<chdir>src_dir</chdir>
<pythonpath>..</pythonpath>
<module>wsgi</module>
<workers>4</workers>
<processes>8</processes>
<master>true</master>
<pidfile>uwsgi.pid</pidfile>
<enable-threads>true</enable-threads>
<logdate>true</logdate>
<daemonize>/log/uwsgi/uwsgi.log</daemonize>
</uwsgi>

推荐答案

这是因为从多个工作线程写入同一文件并不安全. 您应该将日志写入每个工作人员的不同文件中,或尝试使用SysLogHandler

This is because it is not safe to write to the same file from several workers. You should write logs to different files from each worker, or try to use SysLogHandler

http://docs.python.org/library/logging.handlers.html

/etc/rsyslog.d/local-myapp.conf:

/etc/rsyslog.d/local-myapp.conf:

local0.*                         -/var/log/myapp/app.log

settings.py:

settings.py:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'user': {
            '()': 'core.log.UserFilter',
        }
    },
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(user)s %(message)s'
        },
        'simple': {
            'format': '%(levelname)s %(asctime)s %(module)s %(user)s %(message)s'
        },
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler',
            'filters': ['user'],
        },
        'syslog':{
            'level': 'INFO',
            'class': 'logging.handlers.SysLogHandler',
            'formatter': 'verbose',
            'filters': ['user'],
            'facility': 'local0',
            'address': '/dev/log',
        },
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
    },
    'root': {
        'handlers': ['syslog'],
        'level': 'INFO',
    }
}

您还可以尝试将日志消息写入控制台,并让uwsgi自己编写日志.

You also can try to write log messages into console and let uwsgi write the logs by himself.

这篇关于在uwsgi中托管多个进程时,某些django的日志丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆