[twisted] 使用 logfile

怎么使用 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()

----------------

过一天,则自动生成一个新的文件

相关文章
相关标签/搜索