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

Posted by tuergeist on Stack Overflow See other posts from Stack Overflow or by tuergeist
Published on 2010-02-02T10:16:03Z Indexed on 2010/05/12 13:14 UTC
Read the original article Hit count: 208

Filed under:
|

Hi, I'd like to have loglevel TRACE (5) for my application as I don't think that debug() is enought. Additionally log(5, msg) isn't what I want.

The question is, how can I add a custom log level to a Python logger?

Actually I've a mylogger.py with the following content:

import logging

@property
def log(obj):
    myLogger = logging.getLogger(obj.__class__.__name__)
    return myLogger

In my code I use it in the following way:

class ExampleClass(object):
    from mylogger import log

    def __init__(self):
        '''The constructor with the logger'''
        self.log.debug("Init runs")

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

Thanks in advance for your help.

© Stack Overflow or respective owner

Related posts about python

Related posts about logging