命名 Python 记录器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/401277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 20:01:10  来源:igfitidea点击:

Naming Python loggers

pythondjangologging

提问by S.Lott

In Django, I've got loggers all over the place, currently with hard-coded names.

在 Django 中,我到处都有记录器,目前使用硬编码的名称。

For module-level logging (i.e., in a module of view functions) I have the urge to do this.

对于模块级日志记录(即,在视图函数模块中),我有这样做的冲动。

log = logging.getLogger(__name__)

For class-level logging (i.e., in a class __init__method) I have the urge to do this.

对于类级别的日志记录(即,在类__init__方法中),我有这样做的冲动。

self.log = logging.getLogger("%s.%s" % (
    self.__module__, self.__class__.__name__))

I'm looking for second opinions before I tackle several dozen occurrences of getLogger("hard.coded.name").

在处理几十个getLogger("hard.coded.name").

Will this work? Anyone else naming their loggers with the same unimaginative ways?

这会奏效吗?还有其他人用同样缺乏想象力的方式命名他们的记录器吗?

Further, should I break down and write a class decorator for this log definition?

此外,我应该为这个日志定义分解并编写一个类装饰器吗?

回答by cdleary

I typically don't use or find a need for class-level loggers, but I keep my modules at a few classes at most. A simple:

我通常不使用或发现需要类级别的记录器,但我最多将我的模块保留在几个类中。一个简单的:

import logging
LOG = logging.getLogger(__name__)

At the top of the module and subsequent:

在模块顶部及后续:

LOG.info('Spam and eggs are tasty!')

from anywhere in the file typically gets me to where I want to be. This avoids the need for self.logall over the place, which tends to bother me from both a put-it-in-every-class perspective and makes me 5 characters closer to 79 character lines that fit.

从文件中的任何位置通常可以让我到达我想去的地方。这避免了对self.log所有地方的需求,这往往会困扰我从每个班级的角度来看,并使我的 5 个字符更接近 79 个适合的字符行。

You could always use a pseudo-class-decorator:

你总是可以使用伪类装饰器:

>>> import logging
>>> class Foo(object):
...     def __init__(self):
...             self.log.info('Meh')
... 
>>> def logged_class(cls):
...     cls.log = logging.getLogger('{0}.{1}'.format(__name__, cls.__name__))
... 
>>> logged_class(Foo)
>>> logging.basicConfig(level=logging.DEBUG)
>>> f = Foo()
INFO:__main__.Foo:Meh

回答by Matt Anderson

For class level logging, as an alternative to a pseudo-class decorator, you could use a metaclass to make the logger for you at class creation time...

对于类级别的日志记录,作为伪类装饰器的替代方案,您可以使用元类在类创建时为您制作记录器...

import logging

class Foo(object):
    class __metaclass__(type):
        def __init__(cls, name, bases, attrs):
            type.__init__(name, bases, attrs)
            cls.log = logging.getLogger('%s.%s' % (attrs['__module__'], name))
    def __init__(self):
        self.log.info('here I am, a %s!' % type(self).__name__)

if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)
    foo = Foo()

回答by Steve Losh

That looks like it will work, except that selfwon't have a __module__attribute; its class will. The class-level logger call should look like:

看起来它会起作用,只是它self没有__module__属性;它的班级会。类级记录器调用应如下所示:

self.log = logging.getLogger( "%s.%s" % ( self.__class__.__module__, self.__class__.__name__ ) )