Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging messages using the stream handler appear red in console

Tags:

python

I've setup a logger using the following code

def setup_logging():
    import logging
    import logging.handlers
    import os
    #from time import gmtime, strftime
    #import logging.handlers

    logger = logging.getLogger('apt')
    logger.setLevel(logging.DEBUG)

    # create file handler
    fh = logging.handlers.RotatingFileHandler(os.path.join('..','logs','apt.log'), maxBytes=1000000, backupCount=5)
    fh.setLevel(logging.DEBUG)

    # create console handler
    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)

    # create formatter and add it to the handlers
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    ch.setFormatter(formatter)
    fh.setFormatter(formatter)

    # add the handlers to logger
    logger.addHandler(ch)
    logger.addHandler(fh)

The prints the log messages to both a file and the console (which is what I was after). The only issue is that the console message are red. This is distracting since red makes everything look like an error (when it just info). How can I change it so that the console messages are a different color?

Ideally, black for debug and info, red for warning and above.

I'm using Eclipse and PyDev.

like image 909
Miebster Avatar asked Sep 20 '12 15:09

Miebster


1 Answers

The PyDev console highlights messages to stderr in red by default. Python's logging.DEBUG will send messages to stderr. If you wish to change this behavior, see this post: Logging, StreamHandler and standard streams

To change the colors in PyDev, see here: http://pydev.org/manual_adv_interactive_console.html

like image 151
Gareth Avatar answered Sep 30 '22 03:09

Gareth