Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python日志:禁用输出到标准输出_Python_Python 3.x_Logging_Syslog_Syslog Ng - Fatal编程技术网

Python日志:禁用输出到标准输出

Python日志:禁用输出到标准输出,python,python-3.x,logging,syslog,syslog-ng,Python,Python 3.x,Logging,Syslog,Syslog Ng,我试图使程序只使用SysLogHandler实例进行日志记录,而不使用其他处理程序。我希望它不会登录到任何文件或标准输出 self.logger = logging.getLogger(self.name) syslog_handler = logging.handlers.SysLogHandler( socktype=socket.AF_UNIX, address='/dev/log', facility=logging.ha

我试图使程序只使用SysLogHandler实例进行日志记录,而不使用其他处理程序。我希望它不会登录到任何文件或标准输出

    self.logger = logging.getLogger(self.name)

    syslog_handler = logging.handlers.SysLogHandler(
        socktype=socket.AF_UNIX,
        address='/dev/log',
        facility=logging.handlers.SysLogHandler.LOG_LOCAL4,
    )

    # Check if there is a handler attached
    if len(self.logger.handlers) > 0:

        # If there is a handler attached
        # ensure it is a SysLogHandler instance

        handler = self.logger.handlers[0]
        if not (handler.__class__ == logging.handlers.SysLogHandler):
            # If is was something else but a SysLogHandler instance,
            # remove it and attach the syslog_handler

            self.logger.handlers = []
            self.logger.addHandler(syslog_handler)
    else:
        # If no handlers attached,
        # attach syslog_handler

        self.logger.handlers = []
        self.logger.addHandler(syslog_handler)

但是,在使用
python srcipt.py

运行时,它会继续向stdout吐出行:

-将logger.propagate设置为false

  • 您可以创建自己的巡更流,如下所示:

把它交给你的记录器

logging.StreamHandler(stream=None)

使用self.logger.propagate=False,否则也将使用根记录器来查看发生了什么。