No handlers found for logger __main__

Zihs picture Zihs · Dec 10, 2014 · Viewed 17.5k times · Source

I set up logging throughout my python package using a logconfig.ini file.

[loggers]
keys=extracts,root

[formatters]
keys=simple,detailed

[handlers]
keys=file_handler

[formatter_simple]
format=%(module)s - %(levelname)s - %(message)s
datefmt=%Y-%m-%d %H:%M:%S

[formatter_detailed]
format=%(asctime)s %(name)s:%(lineno)s %(levelname)s %(message)s
datefmt=%Y-%m-%d %H:%M:%S

[handler_file_handler]
class=logging.handlers.RotatingFileHandler
level=DEBUG
formatter=detailed
args=('/ebs/logs/foo.log', 'a', 100000000, 3)

[logger_extracts]
level=DEBUG
handlers=file_handler
propagate=1
qualname=extracts

[logger_root]
level=NOTSET
handlers=

But whenever I run my application, I get the following warning message in prompt,

No handlers found for logger __main__

How can I fix this?

Answer

helloV picture helloV · Dec 11, 2014

You have to call logging.basicConfig() first:

Logging HOWTO

The call to basicConfig() should come before any calls to debug(), info() etc. As it’s intended as a one-off simple configuration facility, only the first call will actually do anything: subsequent calls are effectively no-ops.

Or all logging.info('Starting logger for...') which will call logging.basicConfig() automatically. So something like:

import logging
logging.info('Starting logger for...') # or call logging.basicConfig()
LOG = logging.getLogger(name)

The module author's reason for this behavior is here