1

Here is my code

logger = logging.getLogger("JarvisAI")
# Create handlers
c_handler = logging.StreamHandler()
f_handler = logging.FileHandler(logname)
c_handler.setLevel(logging.WARNING)
f_handler.setLevel(logging.INFO)

# Create formatters and add it to handlers
c_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 logging
logger.addHandler(c_handler)
logger.addHandler(f_handler)

Running logger.info("Test") does not produce anything in the logfile. However logger.warning and other higher log level works fine both in console and file. Pls help.

  • Relatead: https://stackoverflow.com/questions/7016056/python-logging-not-outputting-anything . 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 for [python logging no output](https://stackoverflow.com/search?q=python+logging+no+output) here on StackOverflow would have been a good thing to do for the OP. – 9769953 Sep 04 '22 at 10:54

1 Answers1

2

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 of the 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.

9769953
  • 10,344
  • 3
  • 26
  • 37