No handlers found for logger __main__

I get a registrar error and I'm not sure how this happened. I created much simpler test programs and they worked. Any thoughts that this error might come from?

Running this program in python 2.6!

Error:

No handlers could be found for logger "__main__" 

code:

 import logging import subprocess as sp logger = logging.getLogger(__name__) def runpig(filename): # does not use logger .... .... return def main(): try: runpig(filename) except sp.CalledProcessError as ex: logger.error(ex.message) except: logger.info("Error occured") if __name__ == "__main__": main() 
+5
source share
2 answers

You either need to call logging.basicConfig first, or just call logging.info , which will automatically call it.

 if __name__ == "__main__": logging.info("Begin") main() 

This should work

+7
source

You can configure the handler this way:

 logger = logging.getLogger(__name__) handler = logging.StreamHandler() formatter = logging.Formatter("%(asctime)s - %(name)s (%(lineno)s) - %(levelname)s: %(message)s", datefmt='%Y.%m.%d %H:%M:%S') handler.setFormatter(formatter) logger.addHandler(handler) 

fooobar.com/questions/245350 / ... https://docs.python.org/2/library/logging.html#logging.Logger.addHandler

Or set the basic configuration for all registrars:

 logger = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s (%(lineno)s) - %(levelname)s: %(message)s", datefmt='%Y.%m.%d %H:%M:%S') 

https://docs.python.org/2/library/logging.html#logging.basicConfig

0
source

Source: https://habr.com/ru/post/1246427/


All Articles