I got here because I was having the same problem in Colab: why the heck isn't the logger honoring my formatting, and why is it giving me this root
output?
It's because Colab sets up default logging handlers when a notebook starts, and these take precedence over those you define using basicConfig()
. As others have pointed out, you can override this behavior using the force=True
keyword argument, which resets the handlers and will apply the desired format.
Sample code:
import logging
log_format = "%(asctime)s - %(filename)s - %(funcName)s - line %(lineno)d - %(levelname)s - %(message)s"
logging.basicConfig(level=logging.INFO, format=log_format, force=True)
logger.info("Logger is now correctly formatted in Colab!")
The accepted answer doesn't work for me. The top answer gives the right information but I found it confusing so am adding this for people that might appreciate something a bit simpler.