Search code examples
pythonloggingpython-2.6

Logging module not working in python 2.6.6


Below code is working fine on Python 2.7 but unfortunately i have to deploy my script on Linux 5.9 which has python 2.6.6 installed in it and there is no option for me to upgrade. I am not able to make logging work on python 2.6.6

import logging

class Test():
    def __init__(self, args):
        self.args = args

    def call_this(self):
        logger.info("Info Log %s" % self.args)

if __name__ == "__main__":
    t = Test("Hello")
    logger = logging.getLogger()
    file_formatter = logging.Formatter(fmt="%(levelname)-1s [%(filename)s:%(lineno)d] %(message)s")
    file_handler = logging.FileHandler('LogFile.log')
    file_handler.setFormatter(file_formatter)
    logger.addHandler(file_handler)

    console_handler = logging.StreamHandler()
    logger.addHandler(console_handler)
    logger.setLevel("INFO")
    t.call_this()

Solution

  • Changed below line of code and now it works.

    logger.setLevel(logging.INFO)