title: How can I strip Python logging calls without commenting them out?
body: <p>Today I was thinking about a Python project I wrote about a year back where I used <code>logging</code> pretty extensively. I remember having to comment out a lot of logging calls in inner-loop-like scenarios (the 90% code) because of the overhead (<code>hotshot</code> indicated it was one of my biggest bottlenecks).</p>
  
  <p>I wonder now if there's some canonical way to programmatically strip out logging calls in Python applications without commenting and uncommenting all the time. I'd think you could use inspection/recompilation or bytecode manipulation to do something like this and <strong>target only the code objects that are causing bottlenecks</strong>. This way, you could add a manipulator as a post-compilation step and use a centralized configuration file, like so:</p>
  
  <pre><code>[Leave ERROR and above]
  my_module.SomeClass.method_with_lots_of_warn_calls
  
  [Leave WARN and above]
  my_module.SomeOtherClass.method_with_lots_of_info_calls
  
  [Leave INFO and above]
  my_module.SomeWeirdClass.method_with_lots_of_debug_calls
  </code></pre>
  
  <p>Of course, <strong>you'd want to use it sparingly and probably with per-function granularity</strong> -- only for code objects that have shown <code>logging</code> to be a bottleneck. Anybody know of anything like this?</p>
  
  <p><strong>Note:</strong> There are a few things that make this more difficult to do in a performant manner because of dynamic typing and late binding. For example, any calls to a method named <code>debug</code> may have to be wrapped with an <code>if not isinstance(log, Logger)</code>. In any case, I'm assuming all of the minor details can be overcome, either by a gentleman's agreement or some run-time checking. :-)</p>