django 日志多个服务连接_在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!

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.

Does anyone know about this?

Thanks,

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,

},

}

}

my uwsgi config:

0.0.0.0:8888

src_dir

..

wsgi

4

8

true

uwsgi.pid

true

true

/log/uwsgi/uwsgi.log

解决方案

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

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

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

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',

}

}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值