Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python logging split between stdout and stderr [duplicate]

Is it possible to have python logging messages which are INFO or DEBUG to go to stdout and WARNING or greater to go to stderr?

like image 527
crosswired Avatar asked Apr 17 '13 13:04

crosswired


1 Answers

This seems to do what I want:

#!/usr/bin/python import sys import logging   class InfoFilter(logging.Filter):     def filter(self, rec):         return rec.levelno in (logging.DEBUG, logging.INFO)   logger = logging.getLogger("__name__") logger.setLevel(logging.DEBUG)  h1 = logging.StreamHandler(sys.stdout) h1.setLevel(logging.DEBUG) h1.addFilter(InfoFilter()) h2 = logging.StreamHandler() h2.setLevel(logging.WARNING)  logger.addHandler(h1) logger.addHandler(h2) 
like image 196
crosswired Avatar answered Sep 23 '22 17:09

crosswired