Python Log Level with an additional verbosity depth
<p>I would like to extend the existing <code>logging.LEVEL</code> mechanics so that I have the option of switching between different logging levels such as <code>DEBUG</code>, <code>INFO</code>, <code>ERROR</code> etc. but also define a <code>depth</code> for each of the levels.</p>

<p>For example, let's assume that the logging level is set to <code>logging.DEBUG</code>. All of the <code>log.DEBUG()</code> calls will be visible.</p>

<pre><code>log.debug('Limit event has occurred.')
</code></pre>

<p>So I get:</p>

<pre><code>[2016-10-08 10:07:29,807] &lt;__main__&gt; {myApp:test_condition_info:93} (DEBUG) Limit event has occurred.
</code></pre>

<p>What I am after, is passing an extra depth level to the <code>log.debug()</code> call so that I can control how much of detail is printed in the <code>DEBUG</code> message, not entirely <em>enabling</em> or <em>disabling</em> the DEBUG level but controlling how much information the <strong>debug</strong> message will carry. So in all cases we see the debug message but in some instances, it is less detailed and on some occasions more information is included.</p>

<p>For example:</p>

<pre><code>log.debug('Limit event has occurred.', verbosity=1)
log.debug('The following user has caused the limit event: %s' % (user), verbosity=3)
log.debug('The following files have been affected: %s' % [list_of_files], verbosity=7)
</code></pre>

<p>So when the logging level is set to <code>DEBUG</code> and the global verbosity is set to <code>GLOBAL_VERBOSITY=1</code> we will get this:</p>

<pre><code>[2016-10-08 10:07:29,807] &lt;__main__&gt; {myApp:test_condition_info:93} (DEBUG) Limit event has occurred.
</code></pre>

<p>And if the global verbosity is set to <code>GLOBAL_VERBOSITY=4</code> we will get this:</p>

<pre><code>[2016-10-08 10:07:29,807] &lt;__main__&gt; {myApp:test_condition_info:93} (DEBUG) Limit event has occurred.
[2016-10-08 10:07:29,807] &lt;__main__&gt; {myApp:test_condition_info:93} (DEBUG) The following user has caused the limit event: xpr
</code></pre>

<p>And if the global verbosity is set to <code>GLOBAL_VERBOSITY=9</code> we will get all of the details:</p>

<pre><code>[2016-10-08 10:07:29,807] &lt;__main__&gt; {myApp:test_condition_info:93} (DEBUG) Limit event has occurred.
[2016-10-08 10:07:29,807] &lt;__main__&gt; {myApp:test_condition_info:93} (DEBUG) The following user has caused the limit event: xpr
[2016-10-08 10:07:29,807] &lt;__main__&gt; {myApp:test_condition_info:93} (DEBUG) The following files have been affected: ['inside.ini', 'render.so']
</code></pre>

<p>How should I approach this problem?</p>

<p>Can't you just use the more fine grained logging levels? <code>DEBUG</code> is just a wrapper for level 10. You can use</p>

<pre><code>Logger.log(10, "message")
</code></pre>

<p>to log at debug level and then</p>

<pre><code>Logger.log(9, "message")
</code></pre>

<p>which won't show up at debug level, but will if you do</p>

<pre><code>Logger.setLevel(9)
</code></pre>

<p>If you're dead set on doing it the other way, you should look at 'filters'.</p>

<p><a href="http://stackoverflow.com/questions/879732/logging-with-filters">logging with filters</a></p>

<pre><code>#!/usr/bin/env python
import logging

GLOBAL_VERBOSITY = 1

class LoggingErrorFilter(logging.Filter):
  def filter(self, record):
    if record.__dict__.get("verbosity", 0) &gt; GLOBAL_VERBOSITY:
      print "Log message verbosity is greater than threshold, logging line:{0}".format(record)
      return True
    print "Log message verbosity is lower than threshold, not logging line:{0}".format(record)
    return False

logging.basicConfig(level=logging.DEBUG, filename="test.log")

logger = logging.getLogger()

filter = LoggingErrorFilter()

logger.addFilter(filter)


def main():
    logger.info("Message 1", extra={"verbosity":3})
    logger.info("Message 2", extra={"verbosity":1})


if __name__ == "__main__":
    main()
</code></pre>

python logging