Re: create a log level for python logging module

Discussion in 'Python' started by dj, Mar 31, 2009.

  1. dj

    dj Guest

    On Mar 30, 4:18 pm, Vinay Sajip <> wrote:
    > On Mar 30, 4:13 pm, dj <> 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.
     
    dj, Mar 31, 2009
    #1
    1. Advertising

  2. dj

    MRAB Guest

    dj wrote:
    > On Mar 30, 4:18 pm, Vinay Sajip <> wrote:
    >> On Mar 30, 4:13 pm, dj <> 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))
     
    MRAB, Mar 31, 2009
    #2
    1. Advertising

  3. dj

    dj Guest

    On Mar 31, 12:58 pm, MRAB <> wrote:
    > dj wrote:
    > > On Mar 30, 4:18 pm, Vinay Sajip <> wrote:
    > >> On Mar 30, 4:13 pm, dj <> 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.
     
    dj, Mar 31, 2009
    #3
  4. dj

    MRAB Guest

    dj wrote:
    > On Mar 31, 12:58 pm, MRAB <> wrote:
    >> dj wrote:
    >>> On Mar 30, 4:18 pm, Vinay Sajip <> wrote:
    >>>> On Mar 30, 4:13 pm, dj <> 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
     
    MRAB, Mar 31, 2009
    #4
  5. dj

    dj Guest

    On Mar 31, 4:01 pm, MRAB <> wrote:
    > dj wrote:
    > > On Mar 31, 12:58 pm, MRAB <> wrote:
    > >> dj wrote:
    > >>> On Mar 30, 4:18 pm, Vinay Sajip <> wrote:
    > >>>> On Mar 30, 4:13 pm, dj <> 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 :).
     
    dj, Mar 31, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rh0dium
    Replies:
    2
    Views:
    345
    rh0dium
    Aug 4, 2005
  2. fuzzylollipop
    Replies:
    1
    Views:
    379
  3. Royan
    Replies:
    1
    Views:
    1,460
    Andrea Francia
    Apr 1, 2008
  4. dj
    Replies:
    0
    Views:
    242
  5. Santha
    Replies:
    0
    Views:
    1,087
    Santha
    Jan 14, 2010
Loading...

Share This Page