4

I'm new to python and trying to setup a logger in my simple app.

This is the app structure:

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

i'm trying to setup the file configured logger in the main module's 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

and the use it in the /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*

tried to use logger.info / logger.debug. for both options i don't get any error and nothing is logged in console. What do i do wrong?

P.S. maybe also useful that i run it on Win 10 x64

2
  • How is the MyExecutor.start() method called? Commented Oct 11, 2016 at 12:58
  • in the main.py -> executor = MyExecutor(thread_count, _proxy_checker.check_countries_in_proxylist) executor.start() Commented Oct 11, 2016 at 13:18

1 Answer 1

1

My (possibly wrong :-) guess is that you start the script by something like python checker/main.py, thus the logging configuration in __init__.py is not executed.

Please, take a look at this answer: Why is __init__.py not being called?

Moreover, you need to ensure that fileConfig() is called before getLogger() (class body is executed at the time of import). A working setup would be to load the configuration somewhere at the beginning of main.py and instantiate the logger in MyExecutor.__init__().

Sign up to request clarification or add additional context in comments.

8 Comments

starting as python main.py while in /checker dir. which i think the same. Where should i set the fileConfig('logging_config.ini') then, based on the good practice? just in the main.py ? But also, just tried to set it in main.py - same result.
Hmm, could you please try to also remove __name__ inside getLogger?
Updated the answer with another change needed. If this still doesn't suffice then the last comment is also worth trying, but that's a more subtle thing.
yes i did tried without __name__, same. also sure that the config is loaded before getLogger()
I found one interesting thing. I run this proj in pycharm. in tests folder - pytest test for main.py. When i configure pycharm to execute the pytest tests, and the test_main.py is doing assert on the method which is calling the executor - i see the logs in console. However, if i just run main.py or run the test by pytest command in console - no logs in console.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.