本文整理汇总了Python中twisted.python.log.LogPublisher方法的典型用法代码示例。如果您正苦于以下问题:Python log.LogPublisher方法的具体用法?Python log.LogPublisher怎么用?Python log.LogPublisher使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.python.log
的用法示例。
在下文中一共展示了log.LogPublisher方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_publisherReportsBrokenObserversPrivately
# 需要导入模块: from twisted.python import log [as 别名]
# 或者: from twisted.python.log import LogPublisher [as 别名]
def test_publisherReportsBrokenObserversPrivately(self):
"""
Log publisher does not use the global L{log.err} when reporting broken
observers.
"""
errors = []
def logError(eventDict):
if eventDict.get("isError"):
errors.append(eventDict["failure"].value)
def fail(eventDict):
raise RuntimeError("test_publisherLocalyReportsBrokenObservers")
publisher = log.LogPublisher()
publisher.addObserver(logError)
publisher.addObserver(fail)
publisher.msg("Hello!")
self.assertEqual(set(publisher.observers), set([logError, fail]))
self.assertEqual(len(errors), 1)
self.assertIsInstance(errors[0], RuntimeError)
示例2: test_startLoggingTwice
# 需要导入模块: from twisted.python import log [as 别名]
# 或者: from twisted.python.log import LogPublisher [as 别名]
def test_startLoggingTwice(self):
"""
There are some obscure error conditions that can occur when logging is
started twice. See http://twistedmatrix.com/trac/ticket/3289 for more
information.
"""
self._startLoggingCleanup()
# The bug is particular to the way that the t.p.log 'global' function
# handle stdout. If we use our own stream, the error doesn't occur. If
# we use our own LogPublisher, the error doesn't occur.
sys.stdout = StringIO()
def showError(eventDict):
if eventDict['isError']:
sys.__stdout__.write(eventDict['failure'].getTraceback())
log.addObserver(showError)
self.addCleanup(log.removeObserver, showError)
observer = log.startLogging(sys.stdout)
self.addCleanup(observer.stop)
# At this point, we expect that sys.stdout is a StdioOnnaStick object.
self.assertIsInstance(sys.stdout, LoggingFile)
fakeStdout = sys.stdout
observer = log.startLogging(sys.stdout)
self.assertIs(sys.stdout, fakeStdout)
示例3: test_emitNewline
# 需要导入模块: from twisted.python import log [as 别名]
# 或者: from twisted.python.log import LogPublisher [as 别名]
def test_emitNewline(self):
"""
FileLogObserver.emit() will append a newline to its file output.
"""
output = StringIO()
flo = log.FileLogObserver(output)
publisher = log.LogPublisher()
publisher.addObserver(flo.emit)
publisher.msg("Hello!")
result = output.getvalue()
suffix = "Hello!\n"
self.assertTrue(
result.endswith(suffix),
"{0!r} does not end with {1!r}".format(result, suffix)
)
示例4: test_error
# 需要导入模块: from twisted.python import log [as 别名]
# 或者: from twisted.python.log import LogPublisher [as 别名]
def test_error(self, logger):
"""
An error logged to the given ``LogPublisher`` is converted to an Eliot
log message.
"""
publisher = LogPublisher()
observer = EliotObserver(publisher)
observer.logger = logger
publisher.addObserver(observer)
# No public API for this unfortunately, so emulate error logging:
publisher.msg(failure=Failure(ZeroDivisionError("onoes")),
why=b"A zero division ono",
isError=True)
message = (u'A zero division ono\nTraceback (most recent call '
u'last):\nFailure: exceptions.ZeroDivisionError: onoes\n')
assertHasMessage(self, logger, TWISTED_LOG_MESSAGE,
dict(error=True, message=message))
示例5: test_singleUnicode
# 需要导入模块: from twisted.python import log [as 别名]
# 或者: from twisted.python.log import LogPublisher [as 别名]
def test_singleUnicode(self):
"""
L{log.LogPublisher.msg} does not accept non-ASCII Unicode on Python 2,
logging an error instead.
On Python 3, where Unicode is default message type, the message is
logged normally.
"""
message = u"Hello, \N{VULGAR FRACTION ONE HALF} world."
self.lp.msg(message)
self.assertEqual(len(self.out), 1)
if _PY3:
self.assertIn(message, self.out[0])
else:
self.assertIn('with str error', self.out[0])
self.assertIn('UnicodeEncodeError', self.out[0])
示例6: setUp
# 需要导入模块: from twisted.python import log [as 别名]
# 或者: from twisted.python.log import LogPublisher [as 别名]
def setUp(self):
"""
Add a log observer which records log events in C{self.out}. Also,
make sure the default string encoding is ASCII so that
L{testSingleUnicode} can test the behavior of logging unencodable
unicode messages.
"""
self.out = FakeFile()
self.lp = log.LogPublisher()
self.flo = log.FileLogObserver(self.out)
self.lp.addObserver(self.flo.emit)
try:
str(u'\N{VULGAR FRACTION ONE HALF}')
except UnicodeEncodeError:
# This is the behavior we want - don't change anything.
self._origEncoding = None
else:
reload(sys)
self._origEncoding = sys.getdefaultencoding()
sys.setdefaultencoding('ascii')
示例7: getRunner
# 需要导入模块: from twisted.python import log [as 别名]
# 或者: from twisted.python.log import LogPublisher [as 别名]
def getRunner(self):
r = trial._makeRunner(self.config)
r.stream = NativeStringIO()
# XXX The runner should always take care of cleaning this up itself.
# It's not clear why this is necessary. The runner always tears down
# its log file.
self.addCleanup(r._tearDownLogFile)
# XXX The runner should always take care of cleaning this up itself as
# well. It's necessary because TrialRunner._setUpTestdir might raise
# an exception preventing Reporter.done from being run, leaving the
# observer added by Reporter.__init__ still present in the system.
# Something better needs to happen inside
# TrialRunner._runWithoutDecoration to remove the need for this cludge.
r._log = log.LogPublisher()
return r
示例8: setUp
# 需要导入模块: from twisted.python import log [as 别名]
# 或者: from twisted.python.log import LogPublisher [as 别名]
def setUp(self):
self.test = sample.FooTest('test_foo')
self.stream = NativeStringIO()
self.publisher = log.LogPublisher()
self.result = self.resultFactory(self.stream, publisher=self.publisher)
示例9: test_showwarning
# 需要导入模块: from twisted.python import log [as 别名]
# 或者: from twisted.python.log import LogPublisher [as 别名]
def test_showwarning(self):
"""
L{twisted.python.log.showwarning} emits the warning as a message
to the Twisted logging system.
"""
publisher = log.LogPublisher()
publisher.addObserver(self.observer)
publisher.showwarning(
FakeWarning("unique warning message"), FakeWarning,
"warning-filename.py", 27)
event = self.catcher.pop()
self.assertEqual(
event['format'] % event,
'warning-filename.py:27: twisted.test.test_log.FakeWarning: '
'unique warning message')
self.assertEqual(self.catcher, [])
# Python 2.6 requires that any function used to override the
# warnings.showwarning API accept a "line" parameter or a
# deprecation warning is emitted.
publisher.showwarning(
FakeWarning("unique warning message"), FakeWarning,
"warning-filename.py", 27, line=object())
event = self.catcher.pop()
self.assertEqual(
event['format'] % event,
'warning-filename.py:27: twisted.test.test_log.FakeWarning: '
'unique warning message')
self.assertEqual(self.catcher, [])
示例10: test_singleUnicode
# 需要导入模块: from twisted.python import log [as 别名]
# 或者: from twisted.python.log import LogPublisher [as 别名]
def test_singleUnicode(self):
"""
L{log.LogPublisher.msg} encodes Unicode as ``ascii`` with
``backslashreplace`` error handling on Python 2.
On Python 3, where Unicode is default message type, the
message is logged normally.
"""
message = u"Hello, \N{VULGAR FRACTION ONE HALF} world."
self.lp.msg(message)
self.assertEqual(len(self.out), 1)
if _PY3:
self.assertIn(message, self.out[0])
else:
self.assertIn(r"Hello, \xbd world", self.out[0])
示例11: test_emitPrefix
# 需要导入模块: from twisted.python import log [as 别名]
# 或者: from twisted.python.log import LogPublisher [as 别名]
def test_emitPrefix(self):
"""
FileLogObserver.emit() will add a timestamp and system prefix to its
file output.
"""
output = StringIO()
flo = log.FileLogObserver(output)
events = []
def observer(event):
# Capture the event for reference and pass it along to flo
events.append(event)
flo.emit(event)
publisher = log.LogPublisher()
publisher.addObserver(observer)
publisher.msg("Hello!")
self.assertEqual(len(events), 1)
event = events[0]
result = output.getvalue()
prefix = "{time} [{system}] ".format(
time=flo.formatTime(event["time"]), system=event["system"],
)
self.assertTrue(
result.startswith(prefix),
"{0!r} does not start with {1!r}".format(result, prefix)
)
示例12: test_message
# 需要导入模块: from twisted.python import log [as 别名]
# 或者: from twisted.python.log import LogPublisher [as 别名]
def test_message(self, logger):
"""
A message logged to the given ``LogPublisher`` is converted to an
Eliot log message.
"""
publisher = LogPublisher()
observer = EliotObserver(publisher)
observer.logger = logger
publisher.addObserver(observer)
publisher.msg(b"Hello", b"world")
assertHasMessage(self, logger, TWISTED_LOG_MESSAGE,
dict(error=False, message=u"Hello world"))
示例13: getRunner
# 需要导入模块: from twisted.python import log [as 别名]
# 或者: from twisted.python.log import LogPublisher [as 别名]
def getRunner(self):
r = trial._makeRunner(self.config)
r.stream = StringIO.StringIO()
# XXX The runner should always take care of cleaning this up itself.
# It's not clear why this is necessary. The runner always tears down
# its log file.
self.addCleanup(r._tearDownLogFile)
# XXX The runner should always take care of cleaning this up itself as
# well. It's necessary because TrialRunner._setUpTestdir might raise
# an exception preventing Reporter.done from being run, leaving the
# observer added by Reporter.__init__ still present in the system.
# Something better needs to happen inside
# TrialRunner._runWithoutDecoration to remove the need for this cludge.
r._log = log.LogPublisher()
return r
示例14: setUp
# 需要导入模块: from twisted.python import log [as 别名]
# 或者: from twisted.python.log import LogPublisher [as 别名]
def setUp(self):
self.test = sample.FooTest('test_foo')
self.stream = StringIO.StringIO()
self.publisher = log.LogPublisher()
self.result = self.resultFactory(self.stream, publisher=self.publisher)
示例15: test_erroneousErrors
# 需要导入模块: from twisted.python import log [as 别名]
# 或者: from twisted.python.log import LogPublisher [as 别名]
def test_erroneousErrors(self):
"""
Exceptions raised by log observers are logged but the observer which
raised the exception remains registered with the publisher. These
exceptions do not prevent the event from being sent to other observers
registered with the publisher.
"""
L1 = []
L2 = []
def broken(events):
1 / 0
for observer in [L1.append, broken, L2.append]:
log.addObserver(observer)
self.addCleanup(log.removeObserver, observer)
for i in xrange(3):
# Reset the lists for simpler comparison.
L1[:] = []
L2[:] = []
# Send out the event which will break one of the observers.
log.msg("Howdy, y'all.")
# The broken observer should have caused this to be logged. There
# is a slight bug with LogPublisher - when it logs an error from an
# observer, it uses the global "err", which is not necessarily
# associated with it, but which may be associated with a different
# LogPublisher! See #3307.
excs = self.flushLoggedErrors(ZeroDivisionError)
self.assertEqual(len(excs), 1)
# Both other observers should have seen the message.
self.assertEquals(len(L1), 2)
self.assertEquals(len(L2), 2)
# The order is slightly wrong here. The first event should be
# delivered to all observers; then, errors should be delivered.
self.assertEquals(L1[1]['message'], ("Howdy, y'all.",))
self.assertEquals(L2[0]['message'], ("Howdy, y'all.",))