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

Discussão em 'Python' iniciado por Stack, Dezembro 17, 2024.

  1. Stack

    Stack Membro Participativo

    I'd like to have loglevel TRACE (5) for my application, as I don't think that debug() is sufficient. Additionally log(5, msg) isn't what I want. How can I add a custom loglevel to a Python logger?

    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")

    Edit (Dec 8th 2016): I changed the accepted answer to pfa's which is, IMHO, an excellent solution based on the very good proposal from Eric S.

    Continue reading...

Compartilhe esta Página