【问题标题】:Python logging: disable output to stdoutPython 日志记录:禁用输出到标准输出
【发布时间】:2016-09-24 10:47:29
【问题描述】:

我正在尝试让程序仅使用 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 运行时,它会继续向标准输出输出行

【问题讨论】:

  • 使用 self.logger.propagate = False 否则也会使用 root logger。
  • try logging_tree,看看哪里去了。

标签: python python-3.x logging syslog syslog-ng


【解决方案1】:

你有不同的方法来做到这一点:

-将 logger.propagate 设置为 false。

然后把它交给你的记录器。

logging.StreamHandler(stream=None)

【讨论】:

    猜你喜欢
    • 2017-04-13
    • 1970-01-01
    • 2020-10-12
    • 1970-01-01
    • 1970-01-01
    • 2018-05-27
    • 2016-04-26
    • 1970-01-01
    • 2019-01-10
    相关资源
    最近更新 更多