Reputation: 11746
I'm using the following code to separate the errors/warnings from the info/debug messages:
import sys, logging
class StdErrFilter(logging.Filter):
def filter(self, rec):
return rec.levelno in (logging.ERROR, logging.WARNING)
class StdOutFilter(logging.Filter):
def filter(self, rec):
return rec.levelno in (logging.DEBUG, logging.INFO)
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(process)s - %(asctime)s - %(name)s - %(levelname)s - %(message)s')
h1 = logging.StreamHandler(sys.stdout)
h1.setLevel(logging.DEBUG)
h1.setFormatter(formatter)
h1.addFilter(StdOutFilter())
logger.addHandler(h1)
h2 = logging.StreamHandler(sys.stderr)
h2.setLevel(logging.WARNING)
h2.setFormatter(formatter)
h2.addFilter(StdErrFilter())
logger.addHandler(h2)
unfortunately it looks like the filters are getting ignored. Both files contain INFO/DEBUG messages... Any ideas?
Upvotes: 4
Views: 2006
Reputation: 11477
In UNIX terminal sys.stdout
and sys.stderr
are the same, which means if you run this script, you will get this:
(venv) 🍀 python a.py
5798 - 2017-05-10 - __main__ - DEBUG - A DEBUG message
5798 - 2017-05-10 - __main__ - INFO - An INFO message
5798 - 2017-05-10 - __main__ - WARNING - A WARNING message
5798 - 2017-05-10 - __main__ - ERROR - An ERROR message
As a matter of fact, maybe you need to redirect sys.stderr
and sys.stdout
to different files by running this command, you will find that they are actually separated:
(venv) ➜ python a.py 2>> error 1>> info
(venv) 🍀 cat error
5895 - 2017-05-10 - __main__ - WARNING - A WARNING message
5895 - 2017-05-10 - __main__ - ERROR - An ERROR message
(venv) 🍀 cat info
5895 - 2017-05-10 - __main__ - DEBUG - A DEBUG message
5895 - 2017-05-10 - __main__ - INFO - An INFO message
Upvotes: 3