简体   繁体   English

登录 Python/Django 未按预期工作

[英]Logging in Python/Django not working as expected

I have the following handler configuration for logging:我有以下用于日志记录的处理程序配置:

    "handlers": {
      'system_file': {
        'level': 'DEBUG',
        'class': 'logging.handlers.TimedRotatingFileHandler',
        'filename': os.path.join(LOG_FOLDER, "all.log"),
        'formatter': 'verbose',
        'when': 'midnight',
        'backupCount': '30',
      }
   }

Now based on this configuration my logs should get rotated every midnight, ie it should create date wise logs.现在基于这个配置,我的日志应该在每个午夜轮换一次,即它应该创建日期明智的日志。

In the all.log file, everything gets logged properly with correct timestamp, but when the rotation happens, I do not see all the logs in the backup log files (previous day log files).all.log文件中,所有内容都使用正确的时间戳正确记录,但是当轮换发生时,我看不到备份日志文件(前一天日志文件)中的所有日志。

For example:例如:

Let's say today is 2019-10-29, the all.log file starts storing all the logs from 2019-10-29 00:00:00 to 2019-10-29 23:59:59.假设今天是 2019-10-29,all.log 文件开始存储从 2019-10-29 00:00:00 到 2019-10-29 23:59:59 的所有日志。

The next day ie on 2019-10-30 (when rotation would have happened), when I go and check all.log.2019-10-29, it contains log from 2019-10-30 00:00:00 till 2019-10-30 01:00:00 and the all.log file starts storing logs of 2019-10-30 from 00:00:00 onwards.第二天,即 2019 年 10 月 30 日(会发生轮换),当我 go 并检查 all.log.2019-10-29 时,它包含从 2019 年 10 月 30 日 00:00:00 到 2019 年的日志- 10-30 01:00:00 和 all.log 文件从 00:00:00 开始存储 2019-10-30 的日志。 So basically all my backup files only contain log of the next day from 00:00:00-01:00:00.所以基本上我所有的备份文件只包含第二天 00:00:00-01:00:00 的日志。

all.log as on 2019-10-30 all.log 于 2019-10-30

[DEBUG 2019-10-30 00:00:07,463 cron.py:44] .....
[DEBUG 2019-10-30 00:00:11,692 cron.py:44] ....
[DEBUG 2019-10-30 00:00:13,679 cron.py:44] ....
.
.
[DEBUG 2019-10-30 00:00:55,692 cron.py:44] ....
[DEBUG 2019-10-30 00:59:58,679 cron.py:44] ....

SERVER SHUTS DOWN HERE AT 1AM AND STARTS STORING LOGS WHEN IT RESTARTS

[DEBUG 2019-10-30 07:00:02,692 cron.py:44] ....
[DEBUG 2019-10-30 07:00:04,679 cron.py:44] ....
.
.
*Till current time*

all.log.2019-10-29 all.log.2019-10-29

[DEBUG 2019-10-30 00:00:07,463 cron.py:44] .....
[DEBUG 2019-10-30 00:00:11,692 cron.py:44] ....
[DEBUG 2019-10-30 00:00:13,679 cron.py:44] ....
.
.
.
[DEBUG 2019-10-30 00:00:52,463 cron.py:44] .....
[DEBUG 2019-10-30 00:00:55,692 cron.py:44] ....
[DEBUG 2019-10-30 00:59:58,679 cron.py:44] ....

all.log.2019-10-28 all.log.2019-10-28

[DEBUG 2019-10-29 00:00:04,463 cron.py:44] .....
[DEBUG 2019-10-29 00:00:09,692 cron.py:44] ....
[DEBUG 2019-10-29 00:00:11,679 cron.py:44] ....
.
.
.
[DEBUG 2019-10-29 00:00:49,463 cron.py:44] .....
[DEBUG 2019-10-29 00:00:52,692 cron.py:44] ....
[DEBUG 2019-10-29 00:59:56,679 cron.py:44] ....

I'm using a server which runs on a schedule, the server shuts down at 1AM and starts up at 7AM.我正在使用按计划运行的服务器,服务器在凌晨 1 点关闭并在早上 7 点启动。 This is the only reason I see why this weird behavior happens at 1AM, but I'm not able to figure out why this will cause a problem这是我明白为什么会在凌晨 1 点发生这种奇怪行为的唯一原因,但我无法弄清楚为什么这会导致问题

Any help is appreciated.任何帮助表示赞赏。

I'm using Django 1.9.7 and Python 2.7.15我正在使用 Django 1.9.7 和 Python 2.7.15

As mentioned here and in many other places, you can't use this handler for concurrent logging (and that is the case with Django that runs in few threads).正如这里和许多其他地方所提到的,您不能将此处理程序用于并发日志记录(在少数线程中运行的 Django 就是这种情况)。
Potentially, because of concurrency they can override theirself.潜在地,由于并发性,它们可以覆盖自己。

To log to a single destination from multiple processes, you can use one of the following approaches:要从多个进程登录到单个目标,您可以使用以下方法之一:

  • Use something like ConcurrentLogHandler使用类似ConcurrentLogHandler的东西
  • Use a SysLogHandler (or NTEventLogHandler on Windows)使用SysLogHandler (或 Windows 上的NTEventLogHandler
  • Use a SocketHandler which sends the logs to a separate process for writing to file使用SocketHandler将日志发送到单独的进程以写入文件
  • Use a QueueHandler with a multiprocessing.Queue , as outlined here .QueueHandlermultiprocessing.Queue一起使用,如此所述。

And if you really need to do this using base on time - you can redefine ConcurrentRotatingFileHandler and _shouldRollover() method with your own condition.如果您真的需要使用基于时间的方法来执行此操作 - 您可以根据自己的条件重新定义ConcurrentRotatingFileHandler_shouldRollover()方法。
It is not perfect way, but it should work.这不是完美的方式,但它应该工作。

Also you can have a look to project on GitHub that is working on solving this issue:您还可以查看正在解决此问题的 GitHub 上的项目:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM