Python日志记录在stdout和stderr之间分割[复制]

参见英文答案 > How can INFO and DEBUG logging message be sent to stdout and higher level message to stderr                                    6个
是否有可能将INFO或DEBUG的python日志消息转到stdout,使用WARNING或更高版本转到stderr?

解决方法:

这似乎做我想要的:

    #!/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)
上一篇:subprocess模块


下一篇:javascript – node.js:抑制require()回显stderr的解析错误