How to modifying the behaviour of the Python logging facility?
<p>I would like to change the way in which messages with DEBUG and INFO level are displayed when using Python's native logging facility. By "change", I do not mean altering the format but adding an extra logical level. For example:</p>

<pre><code># This is a global variable that is set at the time of initializing the logging.
required_verbosity_level = 7

# This variable is passed with each call to the logger.
supplied_verbosity_level = 5
</code></pre>

<p>So when creating the logger we pass the global requirement.</p>

<pre><code>logger = LoggerBridge(required_verbosity_level = 7)
</code></pre>

<p>Then when we call the method, we pass the appropriate level:</p>

<pre><code>logger.debug('This is a debug message.', supplied_verbosity_level = 5)
</code></pre>

<p>So internally, the logic would be (5&lt;7) and this will make the message <strong>visible</strong> due to the fact that the <code>supplied</code> value meets the <code>required</code> one. However, in the following case:</p>

<pre><code>logger.debug('This is a debug message.', supplied_verbosity_level = 11)
</code></pre>

<p>The message <strong>will never be visible</strong> as the <code>supplied</code> value is higher than the <code>required</code> value. The question is: Where would be the best place to implement such a behaviour?</p>

<p>Right now, I tried couple of things based on inheriting the current Logger class and overriding the internal behaviour, something known as the <code>mixin</code> approach:</p>

<pre><code>class LoggerBridge(object):
    def __init__(self, required_verbosity_level):
        self.required_verbosity_level = required_verbosity_level

    def _log_bridge(self, logger):
        logger(message)

    def info(self, message, supplied_verbosity_level):
        if supplied_verbosity_level &lt; self.required_verbosity_level:
            self._log_bridge(logging.info, message)

    def debug(self, message, supplied_verbosity_level):
        if supplied_verbosity_level &lt; self.required_verbosity_level:
            self._log_bridge(logging.debug, message)
</code></pre>

<p>In theory, this seems to be working. However, is that the right way? Is there a way to solve this by using any of the built-in logging bits, such as a <code>custom handler</code> or a <code>custom filter</code>?</p>

<p>If all you want is custom debug levels then it's supported <em>out of the box</em>.
You set custom debug level (<code>required_verbosity_level</code> in your example) on logger using <a href="https://docs.python.org/2/library/logging.html#logging.Logger.setLevel" rel="nofollow">setLevel()</a> and then you use <a href="https://docs.python.org/2/library/logging.html#logging.Logger.log" rel="nofollow">log()</a> method for logging and pass your custom level (<code>supplied_verbosity_level</code> in your example).
If you want to further customize this logic you might want to override <a href="https://docs.python.org/2/library/logging.html#logging.Logger.isEnabledFor" rel="nofollow">isEnabledFor()</a> method.</p>

python logging