Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The following script, <code>log1.py</code>:</p> <pre><code>import logging, sys class SingleLevelFilter(logging.Filter): def __init__(self, passlevel, reject): self.passlevel = passlevel self.reject = reject def filter(self, record): if self.reject: return (record.levelno != self.passlevel) else: return (record.levelno == self.passlevel) h1 = logging.StreamHandler(sys.stdout) f1 = SingleLevelFilter(logging.INFO, False) h1.addFilter(f1) rootLogger = logging.getLogger() rootLogger.addHandler(h1) h2 = logging.StreamHandler(sys.stderr) f2 = SingleLevelFilter(logging.INFO, True) h2.addFilter(f2) rootLogger.addHandler(h2) logger = logging.getLogger("my.logger") logger.setLevel(logging.DEBUG) logger.debug("A DEBUG message") logger.info("An INFO message") logger.warning("A WARNING message") logger.error("An ERROR message") logger.critical("A CRITICAL message") </code></pre> <p>when run, produces the following results.</p> <pre> C:\temp>log1.py A DEBUG message An INFO message A WARNING message An ERROR message A CRITICAL message </pre> <p>As you'd expect, since on a terminal <code>sys.stdout</code> and <code>sys.stderr</code> are the same. Now, let's redirect stdout to a file, <code>tmp</code>:</p> <pre> C:\temp>log1.py >tmp A DEBUG message A WARNING message An ERROR message A CRITICAL message </pre> <p>So the INFO message has not been printed to the terminal - but the messages directed to <code>sys.stderr</code> <em>have</em> been printed. Let's look at what's in <code>tmp</code>:</p> <pre> C:\temp>type tmp An INFO message </pre> <p>So that approach appears to do what you want.</p>
 

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