python日志轮转RotatingFileHandler在django中的一个bug
简介
大量过时的日志会占用硬盘空间,甚至长时间运行不注意会占满硬盘导致宕机,那么就可以使用内建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开启四个进程启动项目时未出现此错误,但并不代表没有此问题,因为我没有进行并发测试。
解决办法
开发环境
- 本地开发的话可以停止服务,然后删除旧的日志再启动
python manage.py runserver增加 --reload参数不启用自动重启,也就不会又两个进程,但是本地开发不能自动重启稍有不便
生产环境
- 推荐使用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的更多相关文章
- python日志按天分割,保存近一个月日志,日志自动清理
python日志按天分割,保存近一个月日志 import os import logging import re from logging.handlers import TimedRotatingF ...
- 微软BI 之SSIS 系列 - MVP 们也不解的 Scrip Task 脚本任务中的一个 Bug
开篇介绍 前些天自己在整理 SSIS 2012 资料的时候发现了一个功能设计上的疑似Bug,在 Script Task 中是可以给只读列表中的变量赋值.我记得以前在 2008 的版本中为了弄明白这个配 ...
- python开启httpserver服务在自动化测试中的一个小运用
httpserver可以在本机启动一个python实现的web服务器,在自动化测试中,可以将生成测试报告的目录开放给项目组同事. 先安装python 自动化测试框架,生成报告的目录D:\Automat ...
- 【python实例】要求输出字符串中最少一个最多八个的所有字符串组合(连续)
""" 题目:字符串str="ABCDEFGHIJK",要求输出最少一个最多八个的所有组合(向后连续字母) 输出如下: A [0::] AB ABC ...
- 关于ligerUI中ligerTree代码中的一个bug,造成该控件无法通过url的POST方式加载数据
该bug造成ligerTree参数中的method无论你怎么设置都只能用get方式提交 由于本人水平有限,只是找到原因,但无法修正 ligerUI v1.1.9 版本中的ligerui.all.js文 ...
- yarn client中的一个BUG的修复
org.apache.spark.deploy.yarn.Client.scala中的monitorApplication方法: /** * Report the state of an applic ...
- tensorflow代码中的一个bug
tensorflow-gpu版本号 pip show tensorflow-gpu Name: tensorflow-gpu Version: 1.11.0 Summary: TensorFlow i ...
- K&R《C语言》书中的一个Bug
最近在重温K&R的C语言圣经,第二章中的练习题2-2引起了我的注意. 原题是: Write a loop equivalent to the for loop above without us ...
- 记录一个使用HttpClient过程中的一个bug
最近用HttpClient进行链接请求,开了多线程之后发现经常有线程hang住,查看线程dump java.lang.Thread.State: RUNNABLE at java.net.Socket ...
随机推荐
- ASP获取上月本月下月的第一天和最后一天
上月第一天:<%=dateadd("m",-1,year(date)&"-"&month(date)&"-1" ...
- 5.Django数据库配置
Django默认支持sqlite.mysql.oracle.postgresql数据库,像db2和sqlserver需要安装第三方的支持 配置Django数据库:\hello_django\hello ...
- 牛客小白月赛1 E 圆与三角形 【数学】
题目链接 https://www.nowcoder.com/acm/contest/85/E 思路 在三角形中,这一串东西的值恒为1 又 SIN A 的最大值 为1 所以 这串式子的最大值 就是 r ...
- 【LeetCode】数组排列问题(permutations)(附加next_permutation解析)
描述 Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3 ...
- Oracle数据库体系结构(6)数据库归档重做日志文件管理
归档重做日志文件的概念和选择 Oracle数据库能够把已经写满了的重做日志文件保存到一个或多个指定的离线位置,这种保存的文件为归档重做日志文件.通常情况下一个归档重做日志时一个被LGWR写满的重做日志 ...
- 【leetcode刷题笔记】Sort List
Sort a linked list in O(n log n) time using constant space complexity. 题解:实现一个链表的归并排序即可.主要分为三部分: 1.找 ...
- 【leetcode刷题笔记】Linked List Cycle
Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...
- 【Flask】Sqlalchemy 常用数据类型
### SQLAlchemy常用数据类型:1. Integer:整形,映射到数据库中是int类型.2. Float:浮点类型,映射到数据库中是float类型.他占据的32位.3. Double:双精度 ...
- P4619 [SDOI2018]旧试题
题目 P4619 [SDOI2018]旧试题 Ps:山东的题目可真(du)好(liu),思维+码量的神仙题 推式 求\(\sum_{i=1}^A\sum_{j=1}^B\sum_{k=1}^Cd(ij ...
- hd acm1017
Problem Description Given two integers n and m, count the number of pairs of integers (a,b) such tha ...