django logging报错os.rename(self.baseFilename, dfn)

报错信息:

每次当天测试都正常,第二天测试时就报错,后来发现是日志文件切割导致的。

Traceback (most recent call last):
  File "D:\Program Files\Python27\lib\logging\handlers.py", line 77, in emit
    self.doRollover()
  File "D:\Program Files\Python27\lib\logging\handlers.py", line 350, in doRollo
ver
    os.rename(self.baseFilename, dfn)
WindowsError: [Error 32]

django logging配置

django 使用的是python自带logging配置,只需要在settings.py添加配置:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'standard': {
            'format': '[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]'
                      '[%(levelname)s][%(message)s]'
        },
        'simple': {
            'format': '[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s'
        },
        'collect': {
            'format': '%(message)s'
        }
    },
    'filters': {
        'require_debug_true': {
            '()': 'django.utils.log.RequireDebugTrue',
        },
    },
    'handlers': {
        'console': {
            'level': 'INFO',
            'filters': ['require_debug_true'],
            'class': 'logging.StreamHandler',
            'formatter': 'simple'
        },
        'default': {
            'level': 'INFO',
            'class': 'logging.handlers.TimedRotatingFileHandler',
            'filename': os.path.join(BASE_LOG_DIR, "run.log"),
            'when': 'midnight',
            'interval': 1,
            'backupCount': 30,
            'formatter': 'standard',
            'encoding': 'utf-8',
        },
        'error': {
            'level': 'ERROR',
            'class': 'logging.handlers.TimedRotatingFileHandler',
            'filename': os.path.join(BASE_LOG_DIR, "run.log"),
            'when': 'midnight',
            'interval': 1,
            'backupCount': 30,
            'formatter': 'standard',
            'encoding': 'utf-8',
        },
        'collect': {
            'level': 'INFO',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(BASE_LOG_DIR, "run.log"),
            'maxBytes': 1024 * 1024 * 50,
            'backupCount': 5,
            'formatter': 'collect',
            'encoding': "utf-8"
        }
    },
    'loggers': {
        '': {
            'handlers': ['default', 'console', 'error'],
            'level': 'DEBUG',
            'propagate': True,
        },
        'collect': {
            'handlers': ['console', 'collect'],
            'level': 'INFO',
        }
    },
}

后来找了原因,这个配置是表示每天会重命名日志文件,但重命名时失败了,原因是日志文件被占用,我手工试着删除,也是提示文件被占用。网上说原因:是python后台启了多进程(多线程)导致文件被占用,无法重命名。

 'class': 'logging.handlers.TimedRotatingFileHandler',
 'filename': os.path.join(BASE_LOG_DIR, "run.log"),
 'when': 'midnight',
 'interval': 1,

暂没找到解决方法,临时把所有配置改为按文件大小切割日志文件:

'class': 'logging.handlers.RotatingFileHandler',
'filename': os.path.join(BASE_LOG_DIR, "run.log"),
'maxBytes': 1024 * 1024 * 50,

当文件大小到达需要切割时,应该还是会报错,网上找到了解决方法,但还没测试:

## 使用 concurrent_log_handler.ConcurrentRotatingFileHandler 代替 RotatingFileHandler
## 需要安装: pip install concurrent-log-handler
'class': 'concurrent_log_handler.ConcurrentRotatingFileHandler',
'filename': os.path.join(LOGS_DIR, 'run.log'),
'maxBytes': 1024,

参考链接:
http://www.chinacion.cn/article/3666.html
https://www.cnblogs.com/luozx207/p/10986397.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值