TimedRotatingFileHandler在Django中与多实例不兼容 [英] TimedRotatingFileHandler doesn't work fine in Django with multi-instance

查看:1945
本文介绍了TimedRotatingFileHandler在Django中与多实例不兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用TimedRotatingFileHandler来记录Django日志并且每天旋转,但检查日志文件,奇怪的问题是昨天的日志被截断并且记录了几个今天的日志,昨天的日志丢失了!

I use TimedRotatingFileHandler to logging Django log and rotate every day, but check the log file, strange issue is yesterday log is truncated and logging few today's log, yesterday log is lost!

Django 1.4

uwsgi 1.4.9

Python 2.6

Django 1.4
uwsgi 1.4.9
Python 2.6

我用uwsgi启动8 django实例。 setting.py是

I start 8 django instance with uwsgi. The setting.py is

'handlers': {
    'apilog': {
        'level': 'INFO',
        'class': 'logging.handlers.TimedRotatingFileHandler',
        'filename': os.path.join(APILOG, "apilog.log" ),
        'when': 'midnight',
        'formatter': 'info',
        'interval': 1,
        'backupCount': 0,
    },
 },
 'loggers': {                                                                                                                        
    'apilog': {
        'handlers': ['apilog'],
        'level': 'INFO',
        'propagate': True  
     },
  }

我错过了什么吗?为什么旧的日志记录丢失?

Did I miss something? Why old logging is lost?

推荐答案

您不应该同时从多个进程记录到基于文件的处理程序 - 支持,因为它没有便携式操作系统支持。

You should not be logging to a file-based handler from multiple processes concurrently - that is not supported, as there is no portable OS support for it.

要从多个进程登录到单个目标,可以使用以下方法之一:

To log to a single destination from multiple processes, you can use one of the following approaches:


  • 使用类似 ConcurrentLogHandler

  • 使用 SysLogHandler (或 NTEventLogHandler 在Windows上)

  • 使用 SocketHandler 将日志发送到单独的进程以写入文件

  • 使用 QueueHandler multiprocessing.Queue ,概述这里

  • Use something like ConcurrentLogHandler
  • Use a SysLogHandler (or NTEventLogHandler on Windows)
  • Use a SocketHandler which sends the logs to a separate process for writing to file
  • Use a QueueHandler with a multiprocessing.Queue, as outlined here.

这篇关于TimedRotatingFileHandler在Django中与多实例不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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