Note that there are some explanatory texts on larger screens.

plurals
  1. POlogging with filters
    primarykey
    data
    text
    <p>I'm using Logging (<code>import logging</code>) to log messages.</p> <p>Within 1 single module, I am logging messages at the debug level <code>my_logger.debug('msg')</code>; </p> <p>Some of these debug messages come from <code>function_a()</code> and others from <code>function_b()</code>; I'd like to be able to enable/disable logging based on whether they come from a or from b; </p> <p>I'm guessing that I have to use Logging's filtering mechanism. </p> <p>Can someone show me how the code below would need to be instrumented to do what I want?</p> <pre><code>import logging logger= logging.getLogger( "module_name" ) def function_a( ... ): logger.debug( "a message" ) def function_b( ... ): logger.debug( "another message" ) if __name__ == "__main__": logging.basicConfig( stream=sys.stderr, level=logging.DEBUG ) #don't want function_a()'s noise -&gt; .... #somehow filter-out function_a's logging function_a() #don't want function_b()'s noise -&gt; .... #somehow filter-out function_b's logging function_b() </code></pre> <p>If I scaled this simple example to more modules and more funcs per module, I'd be concerned about lots of loggers; </p> <p>Can I keep it down to 1 logger per module? Note that the log messages are "structured", i.e. if the function(s) logging it are doing some parsing work, they all contain a prefix <code>logger.debug("parsing: xxx")</code> - can I somehow with a single line just shut-off all "parsing" messages (regardless of the module/function emitting the message?)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload