怎麼使用 Twisted 的 log,看這:html
http://twistedmatrix.com/documents/current/core/howto/logging.htmlpython
Twisted 中還定義了經常使用的 logfile,下面來講說用法。htm
LogFileget
支持 rotate log file,就是超過大小自動生成新的 log 文件。it
--------------------------------------------------------------import
from twisted.python.logfile import LogFilefile
fout = LogFile("a.txt", "/home/kasicass/sandbox/twisted/log", rotateLength=100)im
for i in xrange(1, 20):db
fout.write("myhello = %d\n" % i)文件
fout.close()
--------------------------------------------------------------
當文件超過 rotateLength 大小,則把生成:
a.txt # 當前正在寫的log
a.txt.1
a.txt.2
...
a.txt.n # 最老的log
有個問題,就是每次 a.txt 達到 rotateLength 大小,則會
mv a.txt.n a.txt.n+1
...
mv a.txt.2 a.txt.3
mv a.txt.1 a.txt.2
mv a.txt a.txt.1
這樣,文件多了,mv 的消耗很大。
DailyLogFile
還有一種常見的狀況,就是每日新生成一個 log 文件。
--------------------------------------------------------------
from twisted.python.logfile import DailyLogFile
fout = DailyLogFile("a.txt", "/home/kasicass/sandbox/twisted/log")
fout.write("hello1\n")
fout.write("hello2\n")
fout.rotate()
fout.write("good!\n")
fout.close()
----------------
過一天,則自動生成一個新的文件