標準出力のロギングには、次のような stdout ラッパーを使用できます。
from __future__ import with_statement
class OutWrapper(object):
def __init__(self, realOutput, logFileName):
self._realOutput = realOutput
self._logFileName = logFileName
def _log(self, text):
with open(self._logFileName, 'a') as logFile:
logFile.write(text)
def write(self, text):
self._log(text)
self._realOutput.write(text)
次に、メインの Python ファイル (すべてを実行するファイル) で初期化する必要があります。
import sys
sys.stdout = OutWrapper(sys.stdout, r'c:\temp\log.txt')
例外のログ記録に関してはMainLoop
、wx.App のメソッドを try..except でラップし、例外情報を抽出して何らかの方法で保存し、次にraise
例を介して例外を再発生させるのが最も簡単な方法です。
try:
app.MainLoop()
except:
exc_info = sys.exc_info()
saveExcInfo(exc_info) # this method you have to write yourself
raise