79439737

Date: 2025-02-14 14:58:09
Score: 0.5
Natty:
Report link

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.

Reasons:
  • RegEx Blacklisted phrase (2): doesn't work for me
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • High reputation (-1):
Posted by: eric