Django-当文件等于maxBytes时,旋转文件处理程序卡住 [英] Django - Rotating File Handler stuck when file is equal to maxBytes

查看:87
本文介绍了Django-当文件等于maxBytes时,旋转文件处理程序卡住的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Django的RotatingFileHander时遇到问题。

I have a problem with the RotatingFileHander with Django.

问题是,当文件达到maxBytes大小时,它将不会创建新文件,并在尝试执行logger.info( any message)时给出错误消息:

The problem is that when the file is up to the maxBytes size, it will not create a new file, and give an error message when you are trying to do logger.info("any message"):

奇怪的部分是:


  1. 没有人共享记录器,视图将有自己的记录器,芹菜任务也有他们自己的记录器。

  2. 记录器仅在文件顶部启动一次(chartLogger = getLogger ...)。同一文件中的不同功能将使用相同的名称

  1. Nobody is sharing loggers, views would have their own logger, tasks from celery have their own loggers.
  2. Loggers are only initiated once at the top of the file (chartLogger = getLogger...) Different functions in the same file will be using the same name

Logged from file views.py, line 1561
Traceback (most recent call last):
  File "C:\Python27\lib\logging\handlers.py", line 77, in emit
    self.doRollover()
  File "C:\Python27\lib\logging\handlers.py", line 142, in doRollover
    os.rename(self.baseFilename, dfn)
WindowsError: [Error 32] The process cannot access the file because it is being used by another process


在我的settings.py中,我有:

Inside my settings.py, I have:

    LOGGING = {
            'version': 1,
            'disable_existing_loggers': True,
            'formatters' : {
                'standard' : {
                    'format' : '%(asctime)s [%(levelname)s] %(name)s: %(message)s'
                },
            },
            'handlers': {
                'celery.webapp' : {
                    'level' : 'ERROR',
                    'class' : 'django.utils.log.AdminEmailHandler',
                },
                'celery' : {
                    'level' : 'INFO',
                    'class' : 'logging.handlers.RotatingFileHandler',
                    'filename' : 'logs/celery.log',
                    'maxBytes' : 1024*1024*10, # 10MB
                    'backupCount' : 10,
                    'formatter' : 'standard',
                },
                'views.error' : {
                    'level' : 'ERROR',
                    'class' : 'django.utils.log.AdminEmailHandler',
                },
                'views' : {
                    'level' : 'INFO',
                    'class' : 'logging.handlers.RotatingFileHandler',
                    'filename' : 'logs/views.log',
                    'maxBytes' : 1024*1024*10, # 10MB
                    'backupCount' : 10,
                    'formatter' : 'standard',
                },
            },

            'loggers': {
                'celery.webapp' : {
                    'level' : 'ERROR',
                    'handlers' : ['celery.webapp'],
                    'propogate' : True,
                },
                'celery.webapp.task' : {
                    'level' : 'INFO',
                    'handlers' : ['celery'],
                    'propogate' : True,
                },
                'views.logger' : {
                    'level' : 'ERROR',
                    'handlers' : ['views.error'],
                    'propogate' : True,
                },
                'views.logger.login' : {
                    'level' : 'INFO',
                    'handlers' : ['views'],
                    'propogate' : True,
                },
                'views.logger.register' : {
                    'level' : 'INFO',
                    'handlers' : ['views'],
                    'propogate' : True,
                },
                'views.logger.chartConfigure' : {
                    'level' : 'INFO',
                    'handlers' : ['views'],
                    'propogate' : True,
                },
                'views.logger.sendEmail' : {
                    'level' : 'INFO',
                    'handlers' : ['views'],
                    'propogate' : True,
                },
            },
    }

我试图更改不同的文件大小,但卡在maxBytes上。

I have tried to change different file sizes, but it gets stuck at maxBytes.

它说该进程无法访问该文件,因为其他一些进程正在使用该文件。所有记录在到达maxBytes之前都很好。

Althought it said that the process cannot access the file because it is being used by some other processes. All the logging is fine before it hits maxBytes.

编辑:

我将记录分为芹菜

LOGGING = {
        'version': 1,
        'disable_existing_loggers': True,
        'formatters' : {
            'standard' : {
                'format' : '%(asctime)s [%(levelname)s] %(name)s: %(message)s'
            },
        },
        'handlers': {
            'celery.webapp' : {
                'level' : 'ERROR',
                'class' : 'django.utils.log.AdminEmailHandler',
            },
            'celery' : {
                'level' : 'INFO',
                'class' : 'logging.handlers.RotatingFileHandler',
                'filename' : 'logs/celery.log',
                'maxBytes' : 1024*1024*10, # 10MB
                'backupCount' : 10,
                'formatter' : 'standard',
            },
            'celery_chartConfigure' : {
                'level' : 'INFO',
                'class' : 'logging.handlers.RotatingFileHandler',
                'filename' : 'logs/celery_chartConfigure.log',
                'maxBytes' : 1024*1024*10, # 10MB
                'backupCount' : 10,
                'formatter' : 'standard',
            },
            'celery_register' : {
                'level' : 'INFO',
                'class' : 'logging.handlers.RotatingFileHandler',
                'filename' : 'logs/celery_register.log',
                'maxBytes' : 1024*1024*10, # 10MB
                'backupCount' : 10,
                'formatter' : 'standard',
            },
            'views.error' : {
                'level' : 'ERROR',
                'class' : 'django.utils.log.AdminEmailHandler',
            },
            'views' : {
                'level' : 'INFO',
                'class' : 'logging.handlers.RotatingFileHandler',
                'filename' : 'logs/views.log',
                'maxBytes' : 1024*1024*10, # 10MB
                'backupCount' : 10,
                'formatter' : 'standard',
            },
            'views_login' : {
                'level' : 'INFO',
                'class' : 'logging.handlers.RotatingFileHandler',
                'filename' : 'logs/views_login.log',
                'maxBytes' : 1024*1024*10, # 10MB
                'backupCount' : 10,
                'formatter' : 'standard',
            },
            'views_sendEmail' : {
                'level' : 'INFO',
                'class' : 'logging.handlers.RotatingFileHandler',
                'filename' : 'logs/views_sendEmail.log',
                'maxBytes' : 1024*1024*10, # 10MB
                'backupCount' : 10,
                'formatter' : 'standard',
            },
            'views_register' : {
                'level' : 'INFO',
                'class' : 'logging.handlers.RotatingFileHandler',
                'filename' : 'logs/views_register.log',
                'maxBytes' : 1024*1024*10, # 10MB
                'backupCount' : 10,
                'formatter' : 'standard',
            },
            'views_chartConfigure' : {
                'level' : 'INFO',
                'class' : 'logging.handlers.RotatingFileHandler',
                'filename' : 'logs/views_chartConfigure.log',
                'maxBytes' : 1024*1024*10, # 10MB
                'backupCount' : 10,
                'formatter' : 'standard',
            },
        },

        'loggers': {
            'celery.webapp' : {
                'level' : 'ERROR',
                'handlers' : ['celery.webapp'],
                'propogate' : True,
            },
            'celery.webapp.task' : {
                'level' : 'INFO',
                'handlers' : ['celery'],
                'propogate' : True,
            },
            'celery.webapp.chartConfigure' : {
                'level' : 'INFO',
                'handlers' : ['celery_chartConfigure'],
                'propogate' : True,
            },
            'celery.webapp.register' : {
                'level' : 'INFO',
                'handlers' : ['celery_register'],
                'propogate' : True,
            },
            'views.logger' : {
                'level' : 'ERROR',
                'handlers' : ['views.error'],
                'propogate' : True,
            },
            'views.logger.login' : {
                'level' : 'INFO',
                'handlers' : ['views_login'],
                'propogate' : True,
            },
            'views.logger.register' : {
                'level' : 'INFO',
                'handlers' : ['views_register'],
                'propogate' : True,
            },
            'views.logger.chartConfigure' : {
                'level' : 'INFO',
                'handlers' : ['views_chartConfigure'],
                'propogate' : True,
            },
            'views.logger.sendEmail' : {
                'level' : 'INFO',
                'handlers' : ['views_sendEmail'],
                'propogate' : True,
            },
        },
}

但是,在执行doRollOver时仍然遇到问题。

However, it is still having problems doing doRollOver.

不会在celery和Django之间拆分日志来解决此问题吗?因为不是只有几个进程访问日志,所以只有Django或Celery。

Wouldn't splitting logs between celery and Django solve it issue? Because it isn't several processes accessing the log, but only Django or Celery.

编辑2:

我也在做Ajax电话。

I'm also doing Ajax calls. Would this somehow spawn another process that might be interfere with logging?

推荐答案

我猜想您正在面临这篇文章中描述的问题: 带有RotatingFileHandler错误的Django日志记录

I guess you are facing the problem described in this post: Django logging with RotatingFileHandler error

也就是说,当运行Django开发服务器时,实际上有两个进程正在运行。

That is, when running Django development server, actually there are two processes running.


默认情况下,两个Django服务器进程正在运行。一种是
实际服务器,另一种是检测代码中的更改,然后
重新加载服务器。因此,settings.py被导入两次,因此
因此这两个进程在相同的
时间访问日志文件。

by default, two processes of Django servers are running. One is the actual server, while the other is to detect changes in the code and reload the server. Therefore, settings.py is imported twice, and consequently the two processes are accessing the log file at the same time.

按照那里的建议尝试

python manage.py runserver --noreload

这篇关于Django-当文件等于maxBytes时,旋转文件处理程序卡住的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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