create a log level for python logging module

dj d.a.abernathy at gmail.com
Tue Mar 31 17:38:30 EDT 2009


On Mar 31, 4:01 pm, MRAB <goo... at mrabarnett.plus.com> wrote:
> dj wrote:
> > On Mar 31, 12:58 pm, MRAB <goo... at mrabarnett.plus.com> wrote:
> >> dj wrote:
> >>> On Mar 30, 4:18 pm, Vinay Sajip <vinay_sa... at yahoo.co.uk> wrote:
> >>>> On Mar 30, 4:13 pm, dj <d.a.aberna... at gmail.com> wrote:
> >>>>> I am trying to create a log level called userinfo for the pythonlogging. I read the source code and tried to register the level to theloggingnamespace with the following source:
> >>>>>              fromloggingimport Logger
> >>>>>                          # create the custom log level
> >>>>>                                   class userinfo(Logger):
> >>>>>                                                def userinfo(self, msg,
> >>>>> *args, **kwargs):
> >>>>>                                                           if
> >>>>> self.isEnabledFor(WARNING):
> >>>>> self._log(WARNING, msg, args, **kwargs)
> >>>>>                               # Register log level in thelogging.Logger namespace
> >>>>>                               Logger.userinfo = userinfo
> >>>>> Has I am sure you guessed, it did not work. If you know how this is
> >>>>> done or know what I am doing work or can provide a link to example
> >>>>> code (because I have not been able to locate any), I would greatly
> >>>>> appreciate it.
> >>>>> My sincere and heartfelt thanks in advance.
> >>>> See the example script at
> >>>>http://dpaste.com/hold/21323/
> >>>> which contains, amongst other things, an illustration of how to use
> >>>> custom logging levels in an application.
> >>>> Regards,
> >>>> Vinay Sajip
> >>> I got the code setup, however, I still get an error for my custom log
> >>> level.
> >>> ############################### Python code
> >>> #######################################################
> >>> import sys, logging
> >>> # log levels
> >>> CRITICAL = 50
> >>> ERROR = 40
> >>> WARNING = 30
> >>> USERINFO =25 # my custom log level
> >>> INFO = 20
> >>> DEBUG  = 10
> >>> # define the range
> >>> LEVEL_RANGE = range(DEBUG, CRITICAL +1)
> >>> # level names
> >>> log_levels = {
> >>>     CRITICAL : 'critical',
> >>>     ERROR : 'error',
> >>>     WARNING : 'warning',
> >>>     USERINFO : 'userinfo',
> >>>     INFO : 'info',
> >>>     DEBUG : 'debug',
> >>>     }
> >>> # associate names with our levels.
> >>> for lvl in log_levels.keys():
> >>>     logging.addLevelName(lvl, log_levels[lvl])
> >>> # setup a log instance
> >>> logger = logging.getLogger('myLog')
> >>> logger.setLevel(CRITICAL)
> >>> hdlr = logging.StreamHandler()
> >>> hdlr.setLevel(CRITICAL)
> >>> logger.addHandler(hdlr)
> >>> # give it a try
> >>> print 'write logs'
> >>> logger.critical('this a critical log message')
> >>> logger.userinfo('this is a userinfo log message')   #call custom log
> >>> level
> >>> ######################################### Output from my interpreter
> >>> ##################################################
> >>> Python 2.6 (r26:66721, Oct  2 2008, 11:35:03) [MSC v.1500 32 bit
> >>> (Intel)]
> >>> Type "help", "copyright", "credits" or "license" for more information.
> >>> Evaluating log_level_test.py
> >>> write logs
> >>> this a critical log message
> >>> AttributeError: Logger instance has no attribute 'userinfo'
> >>> I would love to know what I am doing wrong. Thanks again for your
> >>> help, it is really appreciated.
> >> I think that custom levels don't get their own method; you have to use:
>
> >>      logger.log(USERINFO, 'this is a userinfo log message')
>
> >> although you could add it yourself with, say:
>
> >>      setattr(logger, 'userinfo', lambda *args: logger.log(USERINFO, *args))
>
> > If it's not asking to much, could you show me how these would be
> > used ?
> > I tried on my own, but I keep getting errors.
>
> Have you read:
>
>      http://www.python.org/doc/current/library/logging.html

Problem solved. Thanks for the link to the updated docs :-).



More information about the Python-list mailing list