3

プログラムで、ログに 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)

しかし、このセットアップでは、実行時に stdout に行を吐き出し続けますpython srcipt.py

4

1 に答える 1