Ritesh Raj Sarraf wrote: > Vinay Sajip wrote:
>> It's usual to rely on logger levels and to set handler levels for >> additional refinement of what goes to a particular handler's >> destination. > The problem is that for StreamHandler, logging module logs to > sys.stderr. > I want to use the logging feature for most of the messages my program > displays. > > So some messages would be going to sys.stdout and some to sys.stderr. > > So, I'm ended up creating two handlers, one for sys.stdout and the > other for sys.stderr. > I'd then make INFO level messages go to sys.stdout and DEBUG level > messages go to sys.stderr. > > What do you suggest ?? > Is it good doing this way ? You can achieve the desired behaviour by adding a custom Filter: import sys import logging logger = logging.getLogger("my_app") logger.setLevel(logging.DEBUG) class LevelFilter(logging.Filter): def __init__(self, level): self.level = level def filter(self, record): return self.level == record.levelno def make_handler(outstream, format, level): handler = logging.StreamHandler(outstream) formatter = logging.Formatter(format) handler.setFormatter(formatter) handler.addFilter(LevelFilter(level)) return handler logger.addHandler(make_handler(sys.stderr, 'STDERR %(levelname)s %(message)s', logging.WARN)) logger.addHandler(make_handler(sys.stdout, 'STDOUT %(levelname)s %(message)s', logging.INFO)) logger.info("the world is flat") logger.warning("take care not to fall off its rim") Not sure whether this is a good idea. Another way might be to use distinct loggers. Peter -- http://mail.python.org/mailman/listinfo/python-list