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.
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
Post a Comment