Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No handlers found for logger __main__

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?

like image 440
Zihs Avatar asked Dec 10 '14 22:12

Zihs


2 Answers

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

like image 196
helloV Avatar answered Oct 10 '22 17:10

helloV


I found my error. It turns out the the root logger is used for main. I just need to attach a handler to the root logger as so,

[logger_root]
level=NOTSET
handlers=file_handler
like image 25
Zihs Avatar answered Oct 10 '22 17:10

Zihs