How to add a custom loglevel to Python's logging facility -


I have to TRACE (5) to loglevel for my application because I do not think that debug < / Code> Enough is log (5, msg) is not what I need to do is how can I add a custom loglevel to a Python logger?

I have a mylogger.py with the following content:

  import logging @ property def log (obz): myLogger = logging GetLogger (obj .__ class __.__ name__) return myLogger  

In my code I use it in the following way:

  class ExampleClass (object) ): Mylogger import log def __init __ (self): '' constructor with logger '' self.log.debug ("init run")  

Now I self I would like to call .log.trace ("foo bar")

Thank you in advance for your help.

edit (December 8, 2016): I have changed the accepted answer, an excellent solution based on a very good resolution from IMHO, Eric S.

Eric S.'s answer is excellent, but I have learned from experiment that it will always be the reason for logging messages. New debug level is to be printed - despite being set at the log level. So if you make a new level number of 9, if you call set level (50), lower level messages will be erroneously printed. To prevent this from occurring, you need another line inside the "debugv" function to check that the logging level in question is actually enabled or not.

Fixed the example that the logging level is enabled:

DEBUG_LEVELV_NUM = 9 logging.addLevelName (DEBUG_LEVELV_NUM, "DEBUGV") Def debug (self, messages, * args, ** KJ): # Yes, the logger takes his '* Argos' as 'Argus'. . If self.isEnabledFor (DEBUG_LEVELV_NUM): self._log (DEBUG_LEVELV_NUM, messages, args, ** kws) logging. Logger.debugv = debugv

If you code class logger for Python 2.7 logging in .__ init __.py , this This is what all standard log functions (.critical, debug, etc.) do.

I apparently can not answer the answers of others because of lack of reputation ... It is expected that if Eric sees him, then he will update his post. =)


Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -