Django日志记录旋转文件不起作用 [英] Django Logging Rotating files not working

查看:116
本文介绍了Django日志记录旋转文件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,当Django日志记录的大小达到maxBytes时,我遇到了一些问题.基本上,发生这种情况时,文件似乎不会旋转并创建新文件.

So i am having some issues with Django logging when it gets to the maxBytes size. Basically when that happens the file does not seem to rotate and create a new file.

有人告诉我,这可能与服务器的写入权限有关,但是我不确定如何正确设置该值,以便django能够在旧文件已满时创建新的日志文件.

Someone told me this could have something to do with the writing permissions of the server but i am not sure how to set that properly so that django is able to create a new log file when the old one is full.

我的设置

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'formatters': {
        'verbose': {
            'format': "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s",
            'datefmt': "%d/%b/%Y %H:%M:%S"
        },
        'simple': {
            'format': '[%(levelname)-7s] %(asctime)s - %(message)s'
        },
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        },
        'console': {
            'level': 'INFO',
            'class': 'logging.StreamHandler'
        },
        'boom_file': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'maxBytes': 1024*1024*10,  # 10 MB
            'backupCount': 10,
            'filename': '/var/log/boom.log',
            'formatter': 'simple'
        },
        'binglaw_crawler_file': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'maxBytes': 1024*1024*10,  # 10 MB
            'backupCount': 10,
            'filename': '/var/log/boom-binglaw-crawler.log',
            'formatter': 'simple'
        },
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
        'boom': {
            'handlers': ['console', 'boom_file'],
            'propagate': True,
            'level': 'DEBUG',
        },
        'boom.binglaw_crawler': {
            'handlers': ['binglaw_crawler_file', ],
            'propagate': False,
            'level': 'DEBUG',
        },
    }
}

我注意到我的另一根芹菜似乎在旋转,就好了..那不奇怪吗?

i noticed my other log celeryd seems o be rotaing just fine.. isnt that strange?

-rw-r--rw- 1 root          root          10485721 Aug 18 12:12 boom-binglaw-crawler.log
-rw-r--r-- 1 root          root            403506 Nov  8 23:42 boom-celeryd.log
-rw-r--r-- 1 root          root             20201 Oct  2 12:47 boom-celeryd.log.1
-rw-r--rw- 1 root          root           1049478 Oct  1 18:49 boom-celeryd.log.2

更新:

当我尝试运行创建日志文件的管理命令时出现此错误

i am getting this error when i try to run the manage command that creates the log file

Traceback (most recent call last):
  File "/usr/lib/python2.7/logging/handlers.py", line 77, in emit
    self.doRollover()
  File "/usr/lib/python2.7/logging/handlers.py", line 142, in doRollover
    os.rename(self.baseFilename, dfn)
OSError: [Errno 13] Permission denied

推荐答案

在Django日志记录中使用"logging.handlers.RotatingFileHandler"时,出现此错误:

When using the 'logging.handlers.RotatingFileHandler' in Django logging, I had this error:

Traceback (most recent call last):
  File "C:\Python33\lib\logging\handlers.py", line 73, in emit
    self.doRollover()
  File "C:\Python33\lib\logging\handlers.py", line 176, in doRollover
    self.rotate(self.baseFilename, dfn)
  File "C:\Python33\lib\logging\handlers.py", line 116, in rotate
    os.rename(source, dest)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'log.txt.1'

这个错误发生在我身上,因为当我启动Django时,它似乎启动了两个不同的进程.这两个过程都将设置日志记录,这意味着它们都将获取在"settings.py"配置文件中定义的相同LOGGING文件的句柄.

This error was happening to me because when I start Django, it seems to start 2 different processes. Both processes setup logging, which means both get a handle to the same LOGGING files defined in 'settings.py' config file.

我在设置LOGGING变量之前,将这一行添加到settings.py文件中.

I added this line to my settings.py file, right before I set my LOGGING variable.

print("Initializing LOGGING in settings.py - if you see this more than once use 'runserver --noreload'")

如果使用"manage.py runserver -noreload "参数启动应用,则可以解决文件争用问题.

If you start the app with 'manage.py runserver --noreload' parameter, it may cure your file contention issue.

这篇关于Django日志记录旋转文件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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