Reputation: 2909
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
Upvotes: 3
Views: 10788
Reputation: 2134
You have different ways to do so:
-set logger.propagate to false.
you can create tour own stream, like this:
And give it to your logger.
logging.StreamHandler(stream=None)
Upvotes: 8