3

I'm trying to make a program use only the SysLogHandler instance for logging and no other handlers. I expect it to not log to any files or stdout.

    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)

But with this setup it continues to spit lines out to stdout when run with python srcipt.py

Igor
  • 2,834
  • 2
  • 26
  • 44

1 Answers1

8

You have different ways to do so:

-set logger.propagate to false.

And give it to your logger.

logging.StreamHandler(stream=None)
Destrif
  • 2,104
  • 1
  • 14
  • 22