Python记录问题在Django网站中RotatingFileHandler [英] Problem with Python logging RotatingFileHandler in Django website

查看:205
本文介绍了Python记录问题在Django网站中RotatingFileHandler的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个django供电的网站,我使用标准日志记录模块来跟踪Web活动。

I've a django powered website, and I use standard logging module to track web activity.

日志是通过配置了10个日志文件的RotatingFileHandler完成的,每个1000000字节。日志系统工作,但这是我获得的日志文件:

The log is done via RotatingFileHandler which is configured with 10 log files, 1000000 byte each. The log system works, but this are the log files I get:

-rw-r--r-- 1 apache      apache          83 Jul 23 13:30 hr.log
-rw-r--r-- 1 apache      apache      446276 Jul 23 13:03 hr.log.1
-rw-r--r-- 1 apache      apache      999910 Jul 23 06:00 hr.log.10
-rw-r--r-- 1 apache      apache         415 Jul 23 16:24 hr.log.2
-rw-r--r-- 1 apache      apache      479636 Jul 23 16:03 hr.log.3
-rw-r--r-- 1 apache      apache         710 Jul 23 15:30 hr.log.4
-rw-r--r-- 1 apache      apache      892179 Jul 23 15:03 hr.log.5
-rw-r--r-- 1 apache      apache         166 Jul 23 14:30 hr.log.6
-rw-r--r-- 1 apache      apache      890769 Jul 23 14:03 hr.log.7
-rw-r--r-- 1 apache      apache      999977 Jul 23 12:30 hr.log.8
-rw-r--r-- 1 apache      apache      999961 Jul 23 08:01 hr.log.9

如你所见,这是一团糟。最后一个日志已写入hr.log.2(7月23日16:24)而不是hr.log 记录文档指出:

As you can see it is a mess. Last log has been written to file hr.log.2 (Jul 23 16:24) instead of hr.log, and logging documentation states that:

[...]例如,使用backupCount 5和基本文件名app.log,你会得到app.log,app.log.1,app.log.2,最多app.log.5。 正在写入的文件始终是app.log 。当这个文件被填充时,它被关闭并重命名为app.log.1,如果存在app.log.1,app.log.2等文件,那么它们被重命名为app.log.2,app。 log.3等。

[...]For example, with a backupCount of 5 and a base file name of app.log, you would get app.log, app.log.1, app.log.2, up to app.log.5. The file being written to is always app.log. When this file is filled, it is closed and renamed to app.log.1, and if files app.log.1, app.log.2, etc. exist, then they are renamed to app.log.2, app.log.3 etc. respectively.

我做错了什么?

我的日志配置文件是:

logger.conf:

logger.conf:

[loggers]
keys=root

[handlers]
keys=fileHandler

[formatters]
keys=simple

#--------------------------------------------------------------------
# Formatters
[formatter_simple]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s

#--------------------------------------------------------------------
# Handlers
[handler_fileHandler]
class=handlers.RotatingFileHandler
level=DEBUG
formatter=simple
args=("/data/django/hr/hr.log",'a',1000000,10)

#--------------------------------------------------------------------
# Loggers
[logger_root]
level=DEBUG
handlers=fileHandler

和我的python模块设置日志系统是:

and my python module to set up the log system is:

logger.py

logger.py

import os, logging

# Load config file
logger_config_file = \
    os.path.join(os.path.abspath(os.path.dirname(__file__)), 'logger.conf')
logging.config.fileConfig(logger_config_file)

# Create logger
logger = logging.getLogger('hr_Logger')

# Log start message
logger.info("Logging system started")

然后,在我的views.py的顶部我有:

then, at the top of my views.py I have:

import logging
from hr import logger

log = logging.getLogger('hr.views')
log.info('Load hr.views')

[...]


推荐答案

当有多个进程正在运行时,我发现这个行为与你的代码。

I've found this behavior when there are multiple precesses are running with your code.

不幸的是

可以合并的一些想法是:

Some ideas, you can incorporate are:


  • 使用WatchedFileHandler(2.6中的新功能),并使用外部程序旋转为logrotate

  • 使用syslog或其他日志聚合服务器

  • 使用python日志聚合 sentry - 这对django尤其有用,因为您不仅可以记录日志消息,而且可以记录完整的stacktrace和404。

  • use WatchedFileHandler (new in 2.6) and rotate with external programs as logrotate
  • use syslog or other log aggregating server
  • use python log aggregation sentry - this is especially useful with django as you can log not only log messages, but exceptions with full stacktrace and 404s.

这篇关于Python记录问题在Django网站中RotatingFileHandler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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