简介

大量过时的日志会占用硬盘空间,甚至长时间运行不注意会占满硬盘导致宕机,那么就可以使用内建logging模块根据文件大小(logging.handlers.RotatingFileHandler)或者时间(logging.handlers.TimedRotatingFileHandler)进行日志轮转。使用细节参考python官方文档

问题

在django中使用时通常如下(只截取了RotatingFileHandler部分配置示例):

LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'handlers': {
'default': {
'level':'DEBUG',
'class':'logging.handlers.RotatingFileHandler',
'filename': os.path.join(LOGDIR, LOGFILE), # 或者直接写路径:'c:\logs\all.log',
'maxBytes': 1024*1024*5, # 5 MB
'backupCount': 5,
}
}
}

以上配置时当文件大于5Mb时将当前日志重命名备份,新建一个空日志记录新日志。当备份日志达到5个时删除旧的备份文件。

当在python开发模式(manage.py runserver)时如果备份文件已经达到backupCount后会报错:

  • linux下:
Traceback (most recent call last):
File "/usr/lib64/python2.7/logging/handlers.py", line 77, in emit
self.doRollover()
File "/usr/lib64/python2.7/logging/handlers.py", line 136, in doRollover
os.rename(sfn, dfn)
OSError: [Errno 13] Permission denied
Logged from file utils.py, line 89
  • windows下
Traceback (most recent call last):
File "F:\Python27\lib\logging\handlers.py", line 77, in emit
self.doRollover()
File "F:\Python27\lib\logging\handlers.py", line 142, in doRollover
os.rename(self.baseFilename, dfn)
WindowsError: [Error 32]
Logged from file utils.py, line 89

原因

这是由于django开发模式时会同时启动两个进程加载settings.py,导致日志文件占用后无法重命名或者删除

都知道django开发模式下如果有文件变动会自动重新启动,所以同时又两个进程,一个是程序正常运行的进程,另一个是用来监听变更并重启服务的进程,他们都会加载一遍settings.py,可以在settings.py中加print然后启动会看到控制台又两次输出。

我怀疑使用uwsgi等wsgi程序时的多进程也会导致此问题,但是我使用uwsgi开启四个进程启动项目时未出现此错误,但并不代表没有此问题,因为我没有进行并发测试。

解决办法

开发环境

  1. 本地开发的话可以停止服务,然后删除旧的日志再启动
  2. python manage.py runserver增加 --reload参数不启用自动重启,也就不会又两个进程,但是本地开发不能自动重启稍有不便

生产环境

  1. 推荐使用sentry、elk或者一些集中日志服务使用socket或者http方式记录日志

As describe in other answers python manage.py runserver --noreload will work. But here's another solution that still works with code reload.

Add this at the end of your settings.py

if DEBUG and os.environ.get('RUN_MAIN', None) != 'true':
LOGGING = {}

python manage.py runserver starts a python process that launches your server in a child python process. Each time the parent detects a change it recreates a new child. The problem is that the log rotation by the child process fails because the parent still has a handle on that file. This solution tells the parent that there are no log file.

python日志轮转RotatingFileHandler在django中的一个bug的更多相关文章

  1. python日志按天分割,保存近一个月日志,日志自动清理

    python日志按天分割,保存近一个月日志 import os import logging import re from logging.handlers import TimedRotatingF ...

  2. 微软BI 之SSIS 系列 - MVP 们也不解的 Scrip Task 脚本任务中的一个 Bug

    开篇介绍 前些天自己在整理 SSIS 2012 资料的时候发现了一个功能设计上的疑似Bug,在 Script Task 中是可以给只读列表中的变量赋值.我记得以前在 2008 的版本中为了弄明白这个配 ...

  3. python开启httpserver服务在自动化测试中的一个小运用

    httpserver可以在本机启动一个python实现的web服务器,在自动化测试中,可以将生成测试报告的目录开放给项目组同事. 先安装python 自动化测试框架,生成报告的目录D:\Automat ...

  4. 【python实例】要求输出字符串中最少一个最多八个的所有字符串组合(连续)

    """ 题目:字符串str="ABCDEFGHIJK",要求输出最少一个最多八个的所有组合(向后连续字母) 输出如下: A [0::] AB ABC ...

  5. 关于ligerUI中ligerTree代码中的一个bug,造成该控件无法通过url的POST方式加载数据

    该bug造成ligerTree参数中的method无论你怎么设置都只能用get方式提交 由于本人水平有限,只是找到原因,但无法修正 ligerUI v1.1.9 版本中的ligerui.all.js文 ...

  6. yarn client中的一个BUG的修复

    org.apache.spark.deploy.yarn.Client.scala中的monitorApplication方法: /** * Report the state of an applic ...

  7. tensorflow代码中的一个bug

    tensorflow-gpu版本号 pip show tensorflow-gpu Name: tensorflow-gpu Version: 1.11.0 Summary: TensorFlow i ...

  8. K&R《C语言》书中的一个Bug

    最近在重温K&R的C语言圣经,第二章中的练习题2-2引起了我的注意. 原题是: Write a loop equivalent to the for loop above without us ...

  9. 记录一个使用HttpClient过程中的一个bug

    最近用HttpClient进行链接请求,开了多线程之后发现经常有线程hang住,查看线程dump java.lang.Thread.State: RUNNABLE at java.net.Socket ...

随机推荐

  1. 摘要: CentOS 6.5搭建Redis3.2.8伪分布式集群

    from https://my.oschina.net/ososchina/blog/856678     摘要: CentOS 6.5搭建Redis3.2.8伪分布式集群 前言 最近在服务器上搭建了 ...

  2. android菜鸟学习笔记23----ContentProvider(三)利用内置ContentProvider监听短信及查看联系人

    要使用一个ContentProvider,必须要知道的是它所能匹配的Uri及其数据存储的表的结构. 首先想办法找到访问短信及联系人数据的ContentProvider能接受的Uri: 到github上 ...

  3. 6.2-SingletonBeanRegistry-DefaultSingletonBeanRegistry

    SingletonBeanRegistry package org.springframework.beans.factory.config; public interface SingletonBe ...

  4. 教你管理SQL数据库系列(1-4)

    原文 教你管理 SQL Server 数据库(1)数据库的结构  http://bbs.51cto.com/thread-1084951-1.html教你管理 SQL Server 数据库(2)系统数 ...

  5. Broadcast Intent & Broadcast Receiver

    当Android系统发生某种状况,必须通知所有程序进行处理时,例如电量不足等,可利用Broadcast Intent对象的功能来进行信息广播. 运行机制包括两部:送出Intent对象的程序:监听广播信 ...

  6. matlab常用的一些程序和功能

    ~ 去除误匹配算法(matlab) 1.ransac算法 [tform,matchedPoints1,matchedPoints2] = ...    estimateGeometricTransfo ...

  7. mybatis中查询结果进行分组

    在用mybatis进行数据库查询时,对查询结果进行自动分组,在mapper.xml中的配置有些注意的地方,下面是实际项目中一个例子.在数据库中查询中如下: 在结果集中需要对alarmDate进行分组, ...

  8. 利用CocoaPods管理本地工程和发布开源框架

    发布自己三方框架 发布云端库 1.创建spec pod spec create xxx 2.编辑spec s.name:名称,pod search 搜索的关键词,注意这里一定要和.podspec的名称 ...

  9. 每天一个Linux命令(17)whereis命令

    whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b).man说明文件(参数-m)和源代码文件(参数-s).如果省略参数,则返回所有信息.     (1)用法:   用法:    w ...

  10. Please enable network time synchronisation in system settings

    eth区块同步出现这样的WARN: WARN [06-17|13:02:42] System clock seems off by -51.509894715s, which can prevent ...