Python 3.5.2记录器配置和使用

我是
python的新手,并尝试在我的简单应用程序中设置记录器.

这是应用程序结构:

 - checker
      - checking
         - proxy_checker.py
      - custom_threading
         - __init__.py
         - executor_my.py
         - long_task.py
      - tests
      - __init__.py
      - logging_config.ini
      - main.py

我正在尝试设置文件配置记录器
在主模块的checker / __ init__.py中:

from logging.config import fileConfig

fileConfig('logging_config.ini')

logging_config.ini

[loggers]
keys=root

[handlers]
keys=stream_handler

[formatters]
keys=formatter

[logger_root]
level=DEBUG
handlers=stream_handler

[handler_stream_handler]
class=StreamHandler
level=DEBUG
formatter=formatter
args=(sys.stderr,)

[formatter_formatter]
format=%(asctime)s %(name)-12s %(levelname)-8s %(message)s

并在/checker/custom_threading/exector_my.py中使用它:

import concurrent.futures
import logging

from custom_threading.long_task import LongTask


class MyExecutor(object):
    logger = logging.getLogger(__name__)
    _executor = concurrent.futures.ThreadPoolExecutor(max_workers=500)

    def __init__(self, thread_count, task):
        self._thread_count = thread_count
        self._task = LongTask(task)
        pass

    def start(self):
        self.logger.debug("Launching with thread count: " + str(self._thread_count))

*more irrelevant code*

试图使用logger.info / logger.debug.
对于这两个选项,我没有收到任何错误,并且控制台中没有记录任何内容我做错了什么?

附:也许我在Win 10 x64上运行它也很有用

最佳答案 我的(可能错误的:-)猜测是你通过python checker / main.py之类的东西启动脚本,因此__init__.py中的日志配置不会被执行.

请看看这个答案:
Why is __init__.py not being called?

此外,您需要确保在getLogger()之前调用fileConfig()(在导入时执行类主体).工作设置是在main.py开头的某处加载配置,并在MyExecutor .__ init __()中实例化记录器.

点赞