2016-05-26 3 views
0

私はプログラムがロギング用にSysLogHandlerインスタンスのみを使用し、他のハンドラは使用しないようにしようとしています。私はそれがどんなファイルや標準出力にも記録しないことを期待しています。Pythonロギング:標準出力への出力を無効にする

-set logger.propagate falseに:

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

+3

use self.logger.propagate = Falseそれ以外の場合は、root loggerも使用されます。 –

+2

[try 'logging_tree'](https://pypi.python.org/pypi/logging_tree)を参照してください。 – jfs

答えて

2

を実行すると標準出力に出て行を吐き出すし続けています。

そして、あなたのロガーにそれを与えます。

logging.StreamHandler(stream=None) 
関連する問題