logging addLevelName

J

jjesso

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?
 
J

Jeremy Jones

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,222
Messages
2,571,140
Members
47,755
Latest member
Grazynkaa

Latest Threads

Top