Python Forum

Full Version: logger behaviour
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This setup in python 3.10 is not logging DEBUG to log file.
As in the last line....rootLogger.debug("debugging MantisMigrateAdditionalInfo.py")
Why so?


for handler in logging.root.handlers[:]:
    logging.root.removeHandler(handler)
inputFileName="in.csv"
rootLogger = logging.getLogger("MantisMigrateAdditionalInfo")
rootLogger.setLevel(logging.DEBUG)
loggingFileName = inputFileName[0 : -4]
loggingFileName += "_additional.log"
logFormatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fileHandler = logging.FileHandler(filename=loggingFileName, mode='w')
fileHandler.setFormatter(logFormatter)
fileHandler.setLevel(logging.DEBUG)
rootLogger.addHandler(fileHandler)
consoleHandler = logging.StreamHandler(sys.stdout)
consoleHandler.setFormatter(logFormatter)
consoleHandler.setLevel(logging.INFO)
rootLogger.addHandler(consoleHandler)

rootLogger.info("starting MantisMigrateAdditionalInfo.py")
rootLogger.debug("debugging MantisMigrateAdditionalInfo.py")
Your code works for me with Python 3.10.6 in Linux. Here is the contents of the log file.
Output:
2023-04-15 07:27:58,446 - MantisMigrateAdditionalInfo - INFO - starting MantisMigrateAdditionalInfo.py 2023-04-15 07:27:58,447 - MantisMigrateAdditionalInfo - DEBUG - debugging MantisMigrateAdditionalInfo.py
What does your logfile contain?