install our custom timestamp formats in a less disruptive way

The unit tests on Windows fail because trial is attempting to remove its own
log observer during teardown.  This patch customizes the extant log observer
object by replacing its formatTime method with our own.

I first tried the approach of storing their log observer object and putting it
back during teardown, but it didn't work (perhaps because our node object
doesn't get a chance to do its deferred stopService behavior in time), and
anyway I generally prefer the "fail-safe", or "crash-only" design.
This commit is contained in:
Zooko O'Whielacronx 2007-10-14 20:43:11 -07:00
parent a25ef2f031
commit 240de64598

View File

@ -1,5 +1,5 @@
import os.path, re, time import new, os.path, re, time
from base64 import b32decode, b32encode from base64 import b32decode, b32encode
import twisted import twisted
@ -20,15 +20,14 @@ ADDR_RE=re.compile("^([1-9][0-9]*\.[1-9][0-9]*\.[1-9][0-9]*\.[1-9][0-9]*)(:([1-9
class MyFileLogObserver(log.FileLogObserver): def formatTimeTahoeStyle(self, when):
def formatTime(self, when): # we want UTC timestamps that look like:
# we want UTC timestamps that look like: # 2007-10-12 00:26:28.566Z [Client] rnp752lz: 'client running'
# 2007-10-12 00:26:28.566Z [Client] rnp752lz: 'client running' base = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(when))
base = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(when)) # now add the milliseconds
# now add the milliseconds fraction = when - int(when)
fraction = when - int(when) suffix = ".%03dZ" % (1000*fraction,)
suffix = ".%03dZ" % (1000*fraction,) return base + suffix
return base + suffix
class Node(service.MultiService): class Node(service.MultiService):
@ -189,14 +188,9 @@ class Node(service.MultiService):
if type(o) is type(self.setup_logging): # bound method if type(o) is type(self.setup_logging): # bound method
ob = o.im_self ob = o.im_self
if isinstance(ob, log.FileLogObserver): if isinstance(ob, log.FileLogObserver):
log.removeObserver(o) newmeth = new.instancemethod(formatTimeTahoeStyle, ob, ob.__class__)
logdir = os.path.abspath(os.path.join(self.basedir, "logs")) ob.formatTime = newmeth
fileutil.make_dirs(logdir)
lf = logfile.LogFile("twistd.log", logdir)
# TODO: twisted >2.5.0 offers maxRotatedFiles=50 # TODO: twisted >2.5.0 offers maxRotatedFiles=50
ob = MyFileLogObserver(lf)
log.addObserver(ob.emit)
return
def log(self, msg, src="", args=()): def log(self, msg, src="", args=()):
if src: if src:
@ -208,9 +202,6 @@ class Node(service.MultiService):
msg = msg % tuple(map(humanreadable.hr, args)) msg = msg % tuple(map(humanreadable.hr, args))
except TypeError, e: except TypeError, e:
msg = "ERROR: output string '%s' contained invalid %% expansion, error: %s, args: %s\n" % (`msg`, e, `args`) msg = "ERROR: output string '%s' contained invalid %% expansion, error: %s, args: %s\n" % (`msg`, e, `args`)
# TODO: modify the timestamp to include milliseconds
# TODO: modify it to be in UTC instead of localtime
# (see twisted/python/log.py:FileLogObserver.formatTime line 362)
log.callWithContext({"system":logsrc}, log.callWithContext({"system":logsrc},
log.msg, log.msg,