简体   繁体   English

python loggers作为__main__的子项

[英]python loggers as children of __main__

I'm having trouble getting child loggers named properly in python (2.7). 我无法在python(2.7)中正确命名子记录器。 I have the following file structure: 我有以下文件结构:

-mypackage
  -__init__.py
  -main.py
  -log
    -__init__.py
    -logfile.log
  -src
    -__init__.py
    -logger.py
    -otherfile.py

The contents of main.py are: main.py的内容是:

import logging
import src.logger
from src.otherfile import Foo

logger = logging.getLogger(__name__)
logger.info('Logging from main')
foo = Foo()

The contents of otherfile.py are: otherfile.py的内容是:

import logging
class Foo():
    def __init__(self):
        self.logger = logging.getLogger(__name__)
        self.logger.info('Logging from class in otherfile')

The contents of logger.py are: logger.py的内容是:

import os
import logging
logdir = os.path.dirname(__file__)
logfile = os.path.join(logdir, '../log/controller.log')
logger = logging.getLogger('__main__')
logger.setLevel(logging.DEBUG)

fh = logging.FileHandler(logfile)
fh.setLevel(logging.DEBUG)

formatter = logging.Formatter('%(asctime)s - $(name)s - %(levelname)s: %(message)s')
fh.setFormatter(formatter)

logger.addHandler(fh)

logger.info('logging from logger.py')

I used logging.getLogger(__name__) in each file based on the docs . 我根据文档在每个文件中使用了logging.getLogger(__name__) The exception is logger.py, where I name the logger __main__ so that it would run from top down and not try to derive everything from a buried file. 例外是logger.py,我在其中命名logger __main__以便它从上到下运行,而不是尝试从隐藏文件中派生所有内容。

When I run main.py, it logs correctly from logger.py and main.py, but the logger from otherfile.py isn't derived correctly from the main logger. 当我运行main.py时,它会从logger.py和main.py中正确记录,但是来自otherfile.py的记录器不是从主记录器正确导出的。

How do I get the logger in otherfile.py to derive from my main logger? 如何从otherfile.py中获取记录器以从我的主记录器派生?

In logger.py you are configuring the "__main__" logger. 在logger.py中,您正在配置"__main__"记录器。 I was tricked by the fact that in main.py you use __name__ . 在main.py中使用__name__这一事实让我感到__name__ Since you are invoking python main.py , __name__ evaluates to "__main__" . 由于您正在调用python main.py ,因此__name__计算结果为"__main__" Right. 对。

This can become a problem since when imported (instead of executed), main.py's logger won't be "__main__" but "main" . 这可能会成为一个问题,因为当导入(而不是执行)时,main.py的记录器不会是"__main__"而是"main" It can be fixed by making your package executable: rename main.py to __main__.py and running your package like this: 它可以通过使您的包可执行来修复:将main.py重命名为__main__.py并运行您的包,如下所示:

python -m mypackage

This way, logger names (actually module __name__ 's) will remain consistent. 这样,记录器名称(实际上是模块__name__ )将保持一致。

That said, in no way the logger that you configure in logger.py is a parent of the logger in otherfile.py . 这就是说,在没有办法,你在配置记录器logger.py是在记录器的父otherfile.py The real parent of that logger is called "mypackage" but you haven't configured it, so it's logs are invisible. 该记录器的真正父级称为"mypackage"但您尚未对其进行配置,因此它的日志是不可见的。

You have several choices, you can configure (set log level, handler and formatter): 您有多种选择,您可以配置(设置日志级别,处理程序和格式化程序):

  • the root logger : logger = logging.getLogger() 根记录器: logger = logging.getLogger()
  • mypackage's logger : logger = logger.getLogger(__name__) in mypackage.__init__ mypackage的记录器:mypackage中的logger logger = logger.getLogger(__name__) mypackage.__init__
  • ... or go down to the level of granularity you wish. ...或者降低到您希望的粒度级别。

You can easily create hierarchies of loggers by calling separating levels of loggers by a dot ("."). 您可以通过用点(“。”)调用记录器的分隔级别来轻松创建记录器的层次结构。 For example, the logger returned by calling logging.getLogger('__main__.' + __name__) inherits all properties from the logger returned by logging.getLogger('__main__') . 例如,通过调用logging.getLogger('__main__.' + __name__)返回的记录器继承了logging.getLogger('__main__')返回的记录器中的所有属性。 This behaviour is described here: https://docs.python.org/2/library/logging.html#module-level-functions . 此行为在此处描述: https//docs.python.org/2/library/logging.html#module-level-functions

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

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