1

RotatingFileHandlerPython ロギング モジュールを使用して、プログラム用のを作成しようとしています。私のログハンドラーは出力をファイルに記録します。/var/log/pdmd.log基本的な機能は機能しているようで、必要に応じて出力を記録します。

ただし、ログ文字列を次の形式でフォーマットしようとしています。

"%(levelname)s %(asctime)s %(funcName)s %(lineno)d %(message)s"

ただしmessage、例外の一部のみがログに記録されます。ロガーをセットアップするための私のコードは次のとおりです。

#class variable declared at the beginning of the class declaration
log = logging.getLogger("PdmImportDaemon")

def logSetup(self):
    FORMAT = "%(levelname)s %(asctime)s %(funcName)s %(lineno)d %(message)s"
    logging.basicConfig(format=FORMAT)

    #logging.basicConfig(level=logging.DEBUG)
    self.log.setLevel(logging.DEBUG) #by setting our logger to the DEBUG level (lowest level) we will include all other levels by default
    #setup the rotating file handler to automatically increment the log file name when the max size is reached
    self.log.addHandler( logging.handlers.RotatingFileHandler('/var/log/pdmd.log', mode='a', maxBytes=50000, backupCount=5) )

ここで、メソッドを実行し、プログラムが次のコードでログに出力されるようにします。

 def dirIterate( self ):
    try:
        raise Exception( "this is my exception, trying some cool output stuff here!")               
    except Exception, e:
        self.log.error( e )
        raise e

ファイル内の出力はpdmd.log、例外テキストのみであり、他には何もありません。何らかの理由で、フォーマットが尊重されていません。私は期待しました:

 ERROR 2013-09-03 06:53:18,416 dirIterate 89 this is my exception, trying some cool output stuff here!

自分で設定したフォーマットlogging.basicConfigが尊重されない理由について何か考えはありますか?

4

1 に答える 1