Here is my code
logger = logging.getLogger("JarvisAI")# Create handlersc_handler = logging.StreamHandler()f_handler = logging.FileHandler(logname)c_handler.setLevel(logging.WARNING)f_handler.setLevel(logging.INFO)# Create formatters and add it to handlersc_format = logging.Formatter('%(name)s - %(levelname)s - %(message)s', "%Y-%m-%d %H:%M:%S")f_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s', "%Y-%m-%d %H:%M:%S")c_handler.setFormatter(c_format)f_handler.setFormatter(f_format)# Add handlers to the logginglogger.addHandler(c_handler)logger.addHandler(f_handler)Runninglogger.info("Test") does not produce anything in the logfile.Howeverlogger.warning and other higher log level works fine both in console and file.Pls help.
- Relatead:stackoverflow.com/questions/7016056/… . I'm not flagging this as a duplicate, because that code and answer are not directly helpful to this question, even though the issue is the same. But a simple search forpython logging no output here on StackOverflow would have been a good thing to do for the OP.9769953– 97699532022-09-04 10:54:12 +00:00CommentedSep 4, 2022 at 10:54
1 Answer1
The logger itself also has a logging level, and that needs to be below (or above, depending on your point of view) that of the handlers to show the handlers' output:
logger.setLevel(logging.INFO)(or even debug level: the formatters' levels will prevent debugging info from being output anyway) will do that.
This is also shown in the first code block ofthe Python logging cookbook. Have a read through it.
The reason you are getting warning and higher log level output, is because warning is the default logging level.
Comments
Explore related questions
See similar questions with these tags.