Python-如何刷新日志? (django的)

时间:2021-10-26 23:14:48

I'm working with Django-nonrel on Google App Engine, which forces me to use logging.debug() instead of print().

我在Google App Engine上使用Django-nonrel,这迫使我使用logging.debug()而不是print()。

The "logging" module is provided by Django, but I'm having a rough time using it instead of print().

“记录”模块由Django提供,但我很难使用它而不是print()。

For example, if I need to verify the content held in the variable x, I will put
logging.debug('x is: %s' % x). But if the program crashes soon after (without flushing the stream), then it never gets printed.

例如,如果我需要验证变量x中保存的内容,我将放置logging.debug('x is:%s'%x)。但是如果程序很快崩溃(没有刷新流),那么它永远不会打印出来。

So for debugging, I need debug() to be flushed before the program exits on error, and this is not happening.

因此,对于调试,我需要在程序退出错误之前刷新debug(),这不会发生。

4 个解决方案

#1


10  

I think this may work for you, assuming you're only using one(or default) handler:

我认为这可能对你有用,假设你只使用一个(或默认)处理程序:

>>> import logging
>>> logger = logging.getLogger()
>>> logging.debug('wat wat')
>>> logger.handlers[0].flush()

It's kind of frowned upon in the documentation, though.

不过,在文档中有点不好意思。

Application code should not directly instantiate and use instances of Handler. Instead, the Handler class is a base class that defines the interface that all handlers should have and establishes some default behavior that child classes can use (or override). http://docs.python.org/2/howto/logging.html#handler-basic

应用程序代码不应直接实例化和使用Handler的实例。相反,Handler类是一个基类,它定义了所有处理程序应具有的接口,并建立了子类可以使用(或覆盖)的一些默认行为。 http://docs.python.org/2/howto/logging.html#handler-basic

And it could be a performance drain, but if you're really stuck, this may help with your debugging.

它可能是一个性能消耗,但如果你真的陷入困境,这可能有助于你的调试。

#2


6  

I struggled with a similar problem and here is how I solved it. Instead of using logging module directly to output your logs, initialize your own logger as follows:

我遇到了类似的问题,这就是我解决它的方法。而不是直接使用日志记录模块输出日志,而是按如下方式初始化您自己的记录器:

import sys
import logging


def init_logger():
    logger = logging.getLogger()

    h = logging.StreamHandler(sys.stdout)
    h.flush = sys.stdout.flush
    logger.addHandler(h)

    return logger

Then, use it instead of logging in your code:

然后,使用它而不是登录您的代码:

def f():
    logger = init_logger()
    logger.debug('...')

As a result, you won't have problems with flushing logs anymore.

因此,您不会再有刷新日志的问题。

#3


5  

Django logging relies on the standard python logging module.

Django日志依赖于标准的python日志记录模块。

This module has a module-level method: logging.shutdown() which flushes all of the handlers and shuts down the logging system (i.e. logging can not longer be used after it is called)

该模块有一个模块级方法:logging.shutdown(),它刷新所有处理程序并关闭日志记录系统(即在调用后不能再使用日志记录)

Inspecting the code of this function shows that currently (python 2.7) the logging module holds a list of weak references to all handlers in a module-level variable called _handlerList so all of the handlers can be flushed by doing something like

检查此函数的代码显示当前(python 2.7)日志记录模块包含一个名为_handlerList的模块级变量中所有处理程序的弱引用列表,因此可以通过执行类似操作来刷新所有处理程序

[h_weak_ref().flush() for h_weak_ref in logging._handlerList]

because this solution uses the internals of the module @Mikes solution above is better, but it relies on having access to a logger, it can be generalized as follows:

因为这个解决方案使用模块内部的@Mikes解决方案更好,但它依赖于访问记录器,它可以概括如下:

 [h.flush() for h in my_logger.handlerList]

#4


2  

If the use case is that you have a python program that should flush its logs when exiting, use logging.shutdown().

如果用例是你有一个python程序,它应该在退出时刷新它的日志,使用logging.shutdown()。

From the python documentation:

从python文档:

logging.shutdown()

logging.shutdown()

Informs the logging system to perform an orderly shutdown by flushing and closing all handlers. This should be called at application exit and no further use of the logging system should be made after this call.

通过刷新和关闭所有处理程序,通知日志记录系统执行有序关闭。这应该在应用程序出口处调用,并且在此调用之后不应再使用日志记录系统。

#1


10  

I think this may work for you, assuming you're only using one(or default) handler:

我认为这可能对你有用,假设你只使用一个(或默认)处理程序:

>>> import logging
>>> logger = logging.getLogger()
>>> logging.debug('wat wat')
>>> logger.handlers[0].flush()

It's kind of frowned upon in the documentation, though.

不过,在文档中有点不好意思。

Application code should not directly instantiate and use instances of Handler. Instead, the Handler class is a base class that defines the interface that all handlers should have and establishes some default behavior that child classes can use (or override). http://docs.python.org/2/howto/logging.html#handler-basic

应用程序代码不应直接实例化和使用Handler的实例。相反,Handler类是一个基类,它定义了所有处理程序应具有的接口,并建立了子类可以使用(或覆盖)的一些默认行为。 http://docs.python.org/2/howto/logging.html#handler-basic

And it could be a performance drain, but if you're really stuck, this may help with your debugging.

它可能是一个性能消耗,但如果你真的陷入困境,这可能有助于你的调试。

#2


6  

I struggled with a similar problem and here is how I solved it. Instead of using logging module directly to output your logs, initialize your own logger as follows:

我遇到了类似的问题,这就是我解决它的方法。而不是直接使用日志记录模块输出日志,而是按如下方式初始化您自己的记录器:

import sys
import logging


def init_logger():
    logger = logging.getLogger()

    h = logging.StreamHandler(sys.stdout)
    h.flush = sys.stdout.flush
    logger.addHandler(h)

    return logger

Then, use it instead of logging in your code:

然后,使用它而不是登录您的代码:

def f():
    logger = init_logger()
    logger.debug('...')

As a result, you won't have problems with flushing logs anymore.

因此,您不会再有刷新日志的问题。

#3


5  

Django logging relies on the standard python logging module.

Django日志依赖于标准的python日志记录模块。

This module has a module-level method: logging.shutdown() which flushes all of the handlers and shuts down the logging system (i.e. logging can not longer be used after it is called)

该模块有一个模块级方法:logging.shutdown(),它刷新所有处理程序并关闭日志记录系统(即在调用后不能再使用日志记录)

Inspecting the code of this function shows that currently (python 2.7) the logging module holds a list of weak references to all handlers in a module-level variable called _handlerList so all of the handlers can be flushed by doing something like

检查此函数的代码显示当前(python 2.7)日志记录模块包含一个名为_handlerList的模块级变量中所有处理程序的弱引用列表,因此可以通过执行类似操作来刷新所有处理程序

[h_weak_ref().flush() for h_weak_ref in logging._handlerList]

because this solution uses the internals of the module @Mikes solution above is better, but it relies on having access to a logger, it can be generalized as follows:

因为这个解决方案使用模块内部的@Mikes解决方案更好,但它依赖于访问记录器,它可以概括如下:

 [h.flush() for h in my_logger.handlerList]

#4


2  

If the use case is that you have a python program that should flush its logs when exiting, use logging.shutdown().

如果用例是你有一个python程序,它应该在退出时刷新它的日志,使用logging.shutdown()。

From the python documentation:

从python文档:

logging.shutdown()

logging.shutdown()

Informs the logging system to perform an orderly shutdown by flushing and closing all handlers. This should be called at application exit and no further use of the logging system should be made after this call.

通过刷新和关闭所有处理程序,通知日志记录系统执行有序关闭。这应该在应用程序出口处调用,并且在此调用之后不应再使用日志记录系统。