简体   繁体   English

用于登录Django的文件描述符不会更新

[英]File descriptor doesn't update for logging in Django

We use Python(2.7)/Django(1.8.1) and Gunicorn(19.4.5) for our web application and supervisor(3.0) to monitor it. 我们使用Python(2.7)/ Django(1.8.1)和Gunicorn(19.4.5)作为我们的Web应用程序和管理程序(3.0)来监视它。 I have recently encountered 2 issues in logging: 我最近遇到了2个日志问题:

  1. Django was logging into previous day logs(We have log rotation enabled) Django登录前一天的日志(我们启用了日志轮换)
  2. Django was not logging anything at all. Django根本没有记录任何东西。

The first scenario is understandable where the log rotation changed the file but Django was not updated. 第一种情况是可以理解的,其中日志轮换更改了文件但Django未更新。

The second scenario fixed when I restarted the supervisor process. 我重新启动主管进程时修复了第二个方案。 Which led me to believe again the file descriptor was not updated in the django process. 这让我再次相信文件描述符没有在django进程中更新。

I came by this SO thread which states: 我来自这个SO线程 ,其中指出:

Each child is an independent process, and file handles in the parent may be closed in the child after a fork (assuming POSIX). 每个子进程都是一个独立的进程,并且父进程中的文件句柄可以在fork之后在子进程中关闭(假设为POSIX)。 In any case, logging to the same file from multiple processes is not supported. 在任何情况下,都不支持从多个进程记录到同一文件。

So I have few questions: 所以我几乎没有问题:

  1. My gunicorn has 4 child processes and if one of them fails while writing to a log file will the other child process won't be able to use it? 我的gunicorn有4个子进程,如果其中一个在写入日志文件时失败,另一个子进程将无法使用它吗? and how to debug these kind of scenarios? 以及如何调试这些场景?

  2. Personally I found debugging errors in python logging module to be difficult. 我个人发现python日志模块中的调试错误很难。 Can some one point how to debug errors such as this or is there any way I can monkey patch logging to not fail silently? 有人可以指出如何调试这样的错误,或者有什么方法可以让补丁日志记录不会无声地失败? *(Kindly read update section)*

  3. I have seen Django LogRotation causes the Issue type 1 as explained above and not some script scheduled via cron. 我已经看到Django LogRotation导致问题类型1如上所述,而不是通过cron安排的一些脚本。 So what is preferable? 那么最好的是什么?

Note: The logging config is not a problem. 注意:日志配置不是问题。 I have already spent fair amount of time trying to figure that out. 我已经花了相当多的时间试图解决这个问题。 Also if the config is the issue Django will not write log files after a process restart. 此外,如果配置是问题,Django将不会在进程重新启动后写入日志文件。

Update : 更新

For my second question I see that logging modules provides an option to raiseExceptions on failure although this is discourages in production environment. 对于我的第二个问题,我看到日志记录模块提供了一个在失败时提升事件的选项,尽管这在生产环境中是不鼓励的。 Documentation here . 文档在这里 So now my question becomes how do I set this in Django? 所以现在我的问题是如何在Django中设置它?

I felt like closing this question. 我想结束这个问题。 Bit awkward and seems stupid after 2 months. 有点尴尬,2个月后似乎很愚蠢。 But I guess being stupid is part of the learning and want this to be as a reference for people who stumble across this. 但我觉得愚蠢是学习的一部分,并希望这能成为偶然发现这一点的人的参考。

Scenario 1: Django on using TimedRotatingFileHandler seems not to update the file descriptor some times and hence writes to old log files unless we restart the supervisor. 场景1:使用TimedRotatingFileHandler Django似乎不会更新文件描述符,因此写入旧的日志文件,除非我们重新启动supervisor。 We are yet to find the reason for this behaviour and update the reason if found. 我们还没有找到这种行为的原因并更新原因如果找到。 For now we are using WatchedFileHandler and then using logrotate utility to rotate the logs. 现在我们使用WatchedFileHandler ,然后使用logrotate实用程序来旋转日志。

Scenario 2: This is the stupid question. 场景2:这是一个愚蠢的问题。 When I was logging with some string formatting I forgot to give enough variables which is why the logger was erring. 当我使用一些字符串格式记录时,我忘记提供足够的变量,这就是记录器错误的原因。 But this didn't get propagated. 但这没有得到宣传。 But locally when I was testing I found that logging module was actually throwing that error but silently and any logs after it in the module were not getting printed. 但在我测试的本地时,我发现日志记录模块实际上是在抛出该错误,但是在模块中的任何日志都没有打印出来。 Lessons learns from this scenario were: 从这种情况中吸取的教训是:

  1. If there is a problem in logging find out if the string formatting does not err 如果日志记录存在问题,请查明字符串格式是否错误
  2. Using log.debug('example: {msg}'.format(msg=msg)) of python instead of log.debug('example: %s', msg) . 使用log.debug('example: {msg}'.format(msg=msg))而不是log.debug('example: %s', msg)

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

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