为外部/第三方libs定制日志记录

时间:2022-04-02 22:03:20

I followed the advice of the django docs, and use logging like this:

我听从了django文档的建议,并像这样使用日志记录:

import logging
logger = logging.getLogger(__name__)

def today(...):
    logger.info('Sun is shining, the weather is sweet')

With my current configuration, the output looks like this:

对于当前配置,输出如下所示:

2016-08-11 14:54:06 mylib.foo.today: INFO Sun is shining, the weather is sweet

Unfortunately some libraries which I can't modify use logging like this:

不幸的是,我不能修改的一些库使用这样的日志:

import logging

def third_party(...):
    logging.info('Make you want to move your dancing feet')

The output unfortunately looks like this:

不幸的是,输出是这样的:

2016-08-09 08:28:04 root.third_party: INFO Make you want to move your dancing feet

I want to see this:

我想看看这个:

2016-08-09 08:28:04 other_lib.some_file.third_party: INFO Make you want to move your dancing feet

Difference:

的区别:

root.third_party ==> other_lib.some_file.third_party

根。third_party = = > other_lib.some_file.third_party

I want to see the long version (not root) if code uses logging.info() instead of logger.info()

如果代码使用的是logger.info()而不是logger.info(),我希望看到长版本(而不是root)

Update

更新

This is not a duplicate of Elegant setup of Python logging in Django, since the solution of it is:

这并不是对Django中Python日志记录的优雅设置的复制,因为它的解决方案是:

Start of quote

开始报价

In each module, I define a logger using

在每个模块中,我都使用日志记录器定义日志记录器

logger = logging.getLogger(__name__)

End of quote.

报价结束。

No, I won't modify third-party-code which uses logging.info() instead of logger.info().

不,我不会修改使用logger.info()而不是logger.info()的第三方代码。

Follow Up Question

跟进的问题

Avoid logger=logging.getLogger(__name__) without loosing way to filter logs

避免logger= logger . getlogger (__name__)而不使用筛选日志的方式

2 个解决方案

#1


4  

As Wayne Werner suggested, I would use the Log Record format options. Here's an example.

正如Wayne Werner所建议的,我将使用日志记录格式选项。这是一个例子。

File 1: external_module

文件1:external_module

import logging
def third_party():
    logging.basicConfig(level=logging.DEBUG)
    logger = logging.getLogger()

    logger.info("Hello from %s!"%__name__)

File 2: main

文件2:主要

import external_module
import logging

logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(module)s.%(funcName)s: %(levelname)s %(message)s')
logger = logging.getLogger(__name__)

def cmd():
    logger.info("Hello from %s!"%__name__)
    external_module.third_party()
cmd()

Output:

输出:

2016-08-11 09:18:17,993 main.cmd: INFO Hello from __main__!
2016-08-11 09:18:17,993 external_module.third_party(): INFO Hello from external_module!

#2


3  

That's because they're using the root logger (which is what you get by default when you just do

这是因为它们使用的是根日志记录器(这是您在默认情况下所得到的日志记录器)

import logging

logging.info("Hi! I'm the root logger!")

If you want to do something different you have two (or three) options. The best would be to use the Log Record format options. Alternatively, you could monkey patch the libraries that you're using, e.g.

如果你想做不同的事情,你有两个(或三个)选择。最好是使用日志记录格式选项。或者,你可以用monkey patch你正在使用的库,例如:

import logging
import mod_with_lazy_logging

mod_with_lazy_logging.logger = logging.getLogger(mod_with_lazy_logging.__name__)

Or you could do something gnarly with parsing the ast and rewriting their bits of logging code. But, don't do that.

或者,您可以对ast进行解析并重写它们的日志代码。但是,不要这样做。

#1


4  

As Wayne Werner suggested, I would use the Log Record format options. Here's an example.

正如Wayne Werner所建议的,我将使用日志记录格式选项。这是一个例子。

File 1: external_module

文件1:external_module

import logging
def third_party():
    logging.basicConfig(level=logging.DEBUG)
    logger = logging.getLogger()

    logger.info("Hello from %s!"%__name__)

File 2: main

文件2:主要

import external_module
import logging

logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(module)s.%(funcName)s: %(levelname)s %(message)s')
logger = logging.getLogger(__name__)

def cmd():
    logger.info("Hello from %s!"%__name__)
    external_module.third_party()
cmd()

Output:

输出:

2016-08-11 09:18:17,993 main.cmd: INFO Hello from __main__!
2016-08-11 09:18:17,993 external_module.third_party(): INFO Hello from external_module!

#2


3  

That's because they're using the root logger (which is what you get by default when you just do

这是因为它们使用的是根日志记录器(这是您在默认情况下所得到的日志记录器)

import logging

logging.info("Hi! I'm the root logger!")

If you want to do something different you have two (or three) options. The best would be to use the Log Record format options. Alternatively, you could monkey patch the libraries that you're using, e.g.

如果你想做不同的事情,你有两个(或三个)选择。最好是使用日志记录格式选项。或者,你可以用monkey patch你正在使用的库,例如:

import logging
import mod_with_lazy_logging

mod_with_lazy_logging.logger = logging.getLogger(mod_with_lazy_logging.__name__)

Or you could do something gnarly with parsing the ast and rewriting their bits of logging code. But, don't do that.

或者,您可以对ast进行解析并重写它们的日志代码。但是,不要这样做。