4

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

2
  • 6
    use self.logger.propagate = False otherwise root logger will be use also. Commented May 26, 2016 at 12:27
  • 2
    try logging_tree, to see what goes where. Commented May 26, 2016 at 12:46

1 Answer 1

8

You have different ways to do so:

-set logger.propagate to false.

And give it to your logger.

logging.StreamHandler(stream=None)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.