#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    28
    Rep Power
    0

    Custom logging Formatter class


    I'm adding a better debugging to my program and would like to be able to set muliple Formatters to my logger from the logging module.

    For example:
    Code:
    Logger = logging.getLogger('example')
    logger_streamhandler = logging.StreamHandler()
    
    logger_formatter_streamhandler_debug = logging.Formatter('\033[33;1m[%(levelname)s] %(message)s\033[0m')
    logger_formatter_streamhandler_info = logging.Formatter('\033[34;1m[%(levelname)s] %(message)s\033[0m')
    
    logger_streamhandler.setFormatter(logger_formatter_streamhandler_debug)
    logger_streamhandler.setFormatter(logger_formatter_streamhandler_info)
    
    Logger.addHandler(logger_streamhandler)
    
    Logger.debug('This is a debug message')
    Logger.info('This is an info message')
    Would output:
    [DEBUG] This is a debug message
    [INFO] This is an info message


    Does anyone know how to do this?
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,854
    Rep Power
    481
    I think you also need to set the level.

    Logger.setLevel(logging.DEBUG)

    And also you might want to use
    Logger.propagate
    set to a Boolean value.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    28
    Rep Power
    0
    Originally Posted by b49P23TIvg
    I think you also need to set the level.

    Logger.setLevel(logging.DEBUG)

    And also you might want to use
    Logger.propagate
    set to a Boolean value.
    This isn't my original file, I just wrote this for on here. I have all of it in my original file. But you need a custom Formatter class for the stuff I need. I'm asking whether someone has experience with this, I don't really know where to begin.

IMN logo majestic logo threadwatch logo seochat tools logo