By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,389 Members | 2,010 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,389 IT Pros & Developers. It's quick & easy.

logging addLevelName

P: n/a
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?

Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
jj****@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
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.