Python logging & custom message levels…

Having played with this for a little too long…  I found a simple fix…

My problem was when defining a new custom logging level –

    logger = logging.getLogger(myProj)
    logging.addLevelName(alertNo,"ALERT")
    logger.setLevel(logging.INFO)   # default

and then checking that user edited config options are valid, with –

            x = (sys.argv[1]).upper()
            n = getattr(logging,x,None)
            if not isinstance(n, int):
                logger.error('Invalid log level argument: %s'%x)
            else:
                dbugLvl = x
                logger.info("Logging level : %s"%dbugLvl)
                logger.setLevel(dbugLvl)

This did not recognise my new “ALERT” level.

The simple fix is to add to the initial code –

    logging.ALERT=alertNo

I hope this helps someone else avoid hours of fun…

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s