logging addLevelName

Jeremy Jones zanesdad at bellsouth.net
Wed Mar 9 07:32:27 EST 2005


jjesso at global-matrix.com wrote:

>I am trying to add a new logging level.
>
>logging.config.fileConfig("bengineLog.cfg")
>logging.CLIENT = logging.INFO + 1
>logging.addLevelName( logging.CLIENT, 'CLIENT' )
>logging.root.setLevel( [logging.INFO, logging.CLIENT, logging.WARNING,
>logging.ERROR, logging.CRITICAL] )
>logger = logging.getLogger(None)
>logging.Logger.client('test')
>
>I get error:
>
>AttributeError: class Logger has no attribute 'client'
>
>Any help?
>
>  
>
Looks like what you want is logger.log().  Here is an example that takes 
your addLevelName code and logs at levels info to critical:

#!/usr/bin/env python

import logging

#create new log level
logging.CLIENT = logging.INFO + 1
logging.addLevelName(logging.CLIENT, "CLIENT")
logging.root.setLevel([logging.INFO, logging.CLIENT, logging.WARNING, 
logging.ERROR, logging.CRITICAL])

#create logger with "mylogger"
logger = logging.getLogger("mylogger")
logger.setLevel(logging.INFO)
#create file handler and set level to debug
fh = logging.FileHandler("test.log")
fh.setLevel(logging.DEBUG)
#create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
#create formatter
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - 
%(message)s")
#add formatter to handlers
fh.setFormatter(formatter)
ch.setFormatter(formatter)
#add handlers to logger
logger.addHandler(fh)
logger.addHandler(ch)

logger.debug("this is debug")
logger.info("this is info")
logger.log(logging.CLIENT, "this is client")
logger.warning("this is warning")
logger.error("this is error")
logger.critical("this is critical")


It produces this output:

2005-03-09 12:28:33,399 - mylogger - INFO - this is info
2005-03-09 12:28:33,401 - mylogger - CLIENT - this is client
2005-03-09 12:28:33,458 - mylogger - WARNING - this is warning
2005-03-09 12:28:33,460 - mylogger - ERROR - this is error
2005-03-09 12:28:33,518 - mylogger - CRITICAL - this is critical

HTH,

Jeremy Jones



More information about the Python-list mailing list