Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_log.py 35KB

1 year ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Tests for L{twisted.python.log}.
  5. """
  6. import calendar
  7. import logging
  8. import os
  9. import sys
  10. import time
  11. import warnings
  12. from io import IOBase, StringIO
  13. from twisted.logger import (
  14. LogBeginner,
  15. LoggingFile,
  16. LogLevel as NewLogLevel,
  17. LogPublisher as NewLogPublisher,
  18. )
  19. from twisted.logger.test.test_stdlib import handlerAndBytesIO
  20. from twisted.python import failure, log
  21. from twisted.python.log import LogPublisher
  22. from twisted.trial import unittest
  23. class FakeWarning(Warning):
  24. """
  25. A unique L{Warning} subclass used by tests for interactions of
  26. L{twisted.python.log} with the L{warnings} module.
  27. """
  28. class TextFromEventDictTests(unittest.SynchronousTestCase):
  29. """
  30. Tests for L{textFromEventDict}.
  31. """
  32. def test_message(self):
  33. """
  34. The C{"message"} value, when specified, is concatenated to generate the
  35. message.
  36. """
  37. eventDict = dict(message=("a", "b", "c"))
  38. text = log.textFromEventDict(eventDict)
  39. self.assertEqual(text, "a b c")
  40. def test_format(self):
  41. """
  42. The C{"format"} value, when specified, is used to format the message.
  43. """
  44. eventDict = dict(message=(), isError=0, format="Hello, %(foo)s!", foo="dude")
  45. text = log.textFromEventDict(eventDict)
  46. self.assertEqual(text, "Hello, dude!")
  47. def test_noMessageNoFormat(self):
  48. """
  49. If C{"format"} is unspecified and C{"message"} is empty, return
  50. L{None}.
  51. """
  52. eventDict = dict(message=(), isError=0)
  53. text = log.textFromEventDict(eventDict)
  54. self.assertIsNone(text)
  55. def test_whySpecified(self):
  56. """
  57. The C{"why"} value, when specified, is first part of message.
  58. """
  59. try:
  60. raise RuntimeError()
  61. except BaseException:
  62. eventDict = dict(
  63. message=(), isError=1, failure=failure.Failure(), why="foo"
  64. )
  65. text = log.textFromEventDict(eventDict)
  66. self.assertTrue(text.startswith("foo\n"))
  67. def test_whyDefault(self):
  68. """
  69. The C{"why"} value, when unspecified, defaults to C{"Unhandled Error"}.
  70. """
  71. try:
  72. raise RuntimeError()
  73. except BaseException:
  74. eventDict = dict(message=(), isError=1, failure=failure.Failure())
  75. text = log.textFromEventDict(eventDict)
  76. self.assertTrue(text.startswith("Unhandled Error\n"))
  77. def test_noTracebackForYou(self):
  78. """
  79. If unable to obtain a traceback due to an exception, catch it and note
  80. the error.
  81. """
  82. # Invalid failure object doesn't implement .getTraceback()
  83. eventDict = dict(message=(), isError=1, failure=object())
  84. text = log.textFromEventDict(eventDict)
  85. self.assertIn("\n(unable to obtain traceback)", text)
  86. class LogTests(unittest.SynchronousTestCase):
  87. def setUp(self):
  88. self.catcher = []
  89. self.observer = self.catcher.append
  90. log.addObserver(self.observer)
  91. self.addCleanup(log.removeObserver, self.observer)
  92. def testObservation(self):
  93. catcher = self.catcher
  94. log.msg("test", testShouldCatch=True)
  95. i = catcher.pop()
  96. self.assertEqual(i["message"][0], "test")
  97. self.assertTrue(i["testShouldCatch"])
  98. self.assertIn("time", i)
  99. self.assertEqual(len(catcher), 0)
  100. def testContext(self):
  101. catcher = self.catcher
  102. log.callWithContext(
  103. {"subsystem": "not the default", "subsubsystem": "a", "other": "c"},
  104. log.callWithContext,
  105. {"subsubsystem": "b"},
  106. log.msg,
  107. "foo",
  108. other="d",
  109. )
  110. i = catcher.pop()
  111. self.assertEqual(i["subsubsystem"], "b")
  112. self.assertEqual(i["subsystem"], "not the default")
  113. self.assertEqual(i["other"], "d")
  114. self.assertEqual(i["message"][0], "foo")
  115. def testErrors(self):
  116. for e, ig in [
  117. ("hello world", "hello world"),
  118. (KeyError(), KeyError),
  119. (failure.Failure(RuntimeError()), RuntimeError),
  120. ]:
  121. log.err(e)
  122. i = self.catcher.pop()
  123. self.assertEqual(i["isError"], 1)
  124. self.flushLoggedErrors(ig)
  125. def testErrorsWithWhy(self):
  126. for e, ig in [
  127. ("hello world", "hello world"),
  128. (KeyError(), KeyError),
  129. (failure.Failure(RuntimeError()), RuntimeError),
  130. ]:
  131. log.err(e, "foobar")
  132. i = self.catcher.pop()
  133. self.assertEqual(i["isError"], 1)
  134. self.assertEqual(i["why"], "foobar")
  135. self.flushLoggedErrors(ig)
  136. def test_erroneousErrors(self):
  137. """
  138. Exceptions raised by log observers are logged but the observer which
  139. raised the exception remains registered with the publisher. These
  140. exceptions do not prevent the event from being sent to other observers
  141. registered with the publisher.
  142. """
  143. L1 = []
  144. L2 = []
  145. def broken(event):
  146. 1 // 0
  147. for observer in [L1.append, broken, L2.append]:
  148. log.addObserver(observer)
  149. self.addCleanup(log.removeObserver, observer)
  150. for i in range(3):
  151. # Reset the lists for simpler comparison.
  152. L1[:] = []
  153. L2[:] = []
  154. # Send out the event which will break one of the observers.
  155. log.msg("Howdy, y'all.", log_trace=[])
  156. # The broken observer should have caused this to be logged.
  157. excs = self.flushLoggedErrors(ZeroDivisionError)
  158. del self.catcher[:]
  159. self.assertEqual(len(excs), 1)
  160. # Both other observers should have seen the message.
  161. self.assertEqual(len(L1), 2)
  162. self.assertEqual(len(L2), 2)
  163. # The first event is delivered to all observers; then, errors
  164. # are delivered.
  165. self.assertEqual(L1[0]["message"], ("Howdy, y'all.",))
  166. self.assertEqual(L2[0]["message"], ("Howdy, y'all.",))
  167. def test_showwarning(self):
  168. """
  169. L{twisted.python.log.showwarning} emits the warning as a message
  170. to the Twisted logging system.
  171. """
  172. publisher = log.LogPublisher()
  173. publisher.addObserver(self.observer)
  174. publisher.showwarning(
  175. FakeWarning("unique warning message"),
  176. FakeWarning,
  177. "warning-filename.py",
  178. 27,
  179. )
  180. event = self.catcher.pop()
  181. self.assertEqual(
  182. event["format"] % event,
  183. "warning-filename.py:27: twisted.test.test_log.FakeWarning: "
  184. "unique warning message",
  185. )
  186. self.assertEqual(self.catcher, [])
  187. # Python 2.6 requires that any function used to override the
  188. # warnings.showwarning API accept a "line" parameter or a
  189. # deprecation warning is emitted.
  190. publisher.showwarning(
  191. FakeWarning("unique warning message"),
  192. FakeWarning,
  193. "warning-filename.py",
  194. 27,
  195. line=object(),
  196. )
  197. event = self.catcher.pop()
  198. self.assertEqual(
  199. event["format"] % event,
  200. "warning-filename.py:27: twisted.test.test_log.FakeWarning: "
  201. "unique warning message",
  202. )
  203. self.assertEqual(self.catcher, [])
  204. def test_warningToFile(self):
  205. """
  206. L{twisted.python.log.showwarning} passes warnings with an explicit file
  207. target on to the underlying Python warning system.
  208. """
  209. message = "another unique message"
  210. category = FakeWarning
  211. filename = "warning-filename.py"
  212. lineno = 31
  213. output = StringIO()
  214. log.showwarning(message, category, filename, lineno, file=output)
  215. self.assertEqual(
  216. output.getvalue(),
  217. warnings.formatwarning(message, category, filename, lineno),
  218. )
  219. # In Python 2.6 and higher, warnings.showwarning accepts
  220. # a "line" argument which gives the source line the warning
  221. # message is to include.
  222. line = "hello world"
  223. output = StringIO()
  224. log.showwarning(message, category, filename, lineno, file=output, line=line)
  225. self.assertEqual(
  226. output.getvalue(),
  227. warnings.formatwarning(message, category, filename, lineno, line),
  228. )
  229. def test_publisherReportsBrokenObserversPrivately(self):
  230. """
  231. Log publisher does not use the global L{log.err} when reporting broken
  232. observers.
  233. """
  234. errors = []
  235. def logError(eventDict):
  236. if eventDict.get("isError"):
  237. errors.append(eventDict["failure"].value)
  238. def fail(eventDict):
  239. raise RuntimeError("test_publisherLocalyReportsBrokenObservers")
  240. publisher = log.LogPublisher()
  241. publisher.addObserver(logError)
  242. publisher.addObserver(fail)
  243. publisher.msg("Hello!")
  244. self.assertEqual(set(publisher.observers), {logError, fail})
  245. self.assertEqual(len(errors), 1)
  246. self.assertIsInstance(errors[0], RuntimeError)
  247. class FakeFile(list):
  248. def write(self, bytes):
  249. self.append(bytes)
  250. def flush(self):
  251. pass
  252. IOBase.register(FakeFile) # type: ignore[attr-defined]
  253. class EvilStr:
  254. def __str__(self) -> str:
  255. return str(1 // 0)
  256. class EvilRepr:
  257. def __str__(self) -> str:
  258. return "Happy Evil Repr"
  259. def __repr__(self) -> str:
  260. return str(1 // 0)
  261. class EvilReprStr(EvilStr, EvilRepr):
  262. pass
  263. class LogPublisherTestCaseMixin:
  264. def setUp(self):
  265. """
  266. Add a log observer which records log events in C{self.out}. Also,
  267. make sure the default string encoding is ASCII so that
  268. L{testSingleUnicode} can test the behavior of logging unencodable
  269. unicode messages.
  270. """
  271. self.out = FakeFile()
  272. self.lp = log.LogPublisher()
  273. self.flo = log.FileLogObserver(self.out)
  274. self.lp.addObserver(self.flo.emit)
  275. try:
  276. "\N{VULGAR FRACTION ONE HALF}"
  277. except UnicodeEncodeError:
  278. pass
  279. # This is the behavior we want - don't change anything.
  280. self._origEncoding = None
  281. def tearDown(self):
  282. """
  283. Verify that everything written to the fake file C{self.out} was a
  284. C{str}. Also, restore the default string encoding to its previous
  285. setting, if it was modified by L{setUp}.
  286. """
  287. for chunk in self.out:
  288. self.assertIsInstance(chunk, str, f"{chunk!r} was not a string")
  289. if self._origEncoding is not None:
  290. sys.setdefaultencoding(self._origEncoding)
  291. del sys.setdefaultencoding
  292. class LogPublisherTests(LogPublisherTestCaseMixin, unittest.SynchronousTestCase):
  293. def testSingleString(self):
  294. self.lp.msg("Hello, world.")
  295. self.assertEqual(len(self.out), 1)
  296. def testMultipleString(self):
  297. # Test some stupid behavior that will be deprecated real soon.
  298. # If you are reading this and trying to learn how the logging
  299. # system works, *do not use this feature*.
  300. self.lp.msg("Hello, ", "world.")
  301. self.assertEqual(len(self.out), 1)
  302. def test_singleUnicode(self):
  303. """
  304. L{log.LogPublisher.msg} does not accept non-ASCII Unicode on Python 2,
  305. logging an error instead.
  306. On Python 3, where Unicode is default message type, the message is
  307. logged normally.
  308. """
  309. message = "Hello, \N{VULGAR FRACTION ONE HALF} world."
  310. self.lp.msg(message)
  311. self.assertEqual(len(self.out), 1)
  312. self.assertIn(message, self.out[0])
  313. class FileObserverTests(LogPublisherTestCaseMixin, unittest.SynchronousTestCase):
  314. """
  315. Tests for L{log.FileObserver}.
  316. """
  317. ERROR_INVALID_FORMAT = "Invalid format string"
  318. ERROR_UNFORMATTABLE_OBJECT = "UNFORMATTABLE OBJECT"
  319. ERROR_FORMAT = "Invalid format string or unformattable object in log message"
  320. ERROR_PATHOLOGICAL = "PATHOLOGICAL ERROR"
  321. ERROR_NO_FORMAT = "Unable to format event"
  322. ERROR_UNFORMATTABLE_SYSTEM = "[UNFORMATTABLE]"
  323. ERROR_MESSAGE_LOST = "MESSAGE LOST: unformattable object logged"
  324. def _getTimezoneOffsetTest(self, tzname, daylightOffset, standardOffset):
  325. """
  326. Verify that L{getTimezoneOffset} produces the expected offset for a
  327. certain timezone both when daylight saving time is in effect and when
  328. it is not.
  329. @param tzname: The name of a timezone to exercise.
  330. @type tzname: L{bytes}
  331. @param daylightOffset: The number of seconds west of UTC the timezone
  332. should be when daylight saving time is in effect.
  333. @type daylightOffset: L{int}
  334. @param standardOffset: The number of seconds west of UTC the timezone
  335. should be when daylight saving time is not in effect.
  336. @type standardOffset: L{int}
  337. """
  338. if getattr(time, "tzset", None) is None:
  339. raise unittest.SkipTest(
  340. "Platform cannot change timezone, cannot verify correct "
  341. "offsets in well-known timezones."
  342. )
  343. originalTimezone = os.environ.get("TZ", None)
  344. try:
  345. os.environ["TZ"] = tzname
  346. time.tzset()
  347. # The behavior of mktime depends on the current timezone setting.
  348. # So only do this after changing the timezone.
  349. # Compute a POSIX timestamp for a certain date and time that is
  350. # known to occur at a time when daylight saving time is not in
  351. # effect.
  352. localStandardTuple = (2007, 1, 31, 0, 0, 0, 2, 31, 0)
  353. standard = time.mktime(localStandardTuple)
  354. # Compute a POSIX timestamp for a certain date and time that is
  355. # known to occur at a time when daylight saving time is in effect.
  356. localDaylightTuple = (2006, 6, 30, 0, 0, 0, 4, 181, 1)
  357. try:
  358. daylight = time.mktime(localDaylightTuple)
  359. except OverflowError:
  360. # mktime() may raise OverflowError if its tuple is
  361. # inconsistent, although many implementations don't
  362. # care. The implementation in glibc>=2.28 will raise
  363. # if DST is indicated for a zone that doesn't have DST.
  364. # We accept either behavior: ignoring the DST flag for those
  365. # zones, or raising EOVERFLOW.
  366. if daylightOffset == standardOffset: # DST-less zone?
  367. daylight = standard
  368. else:
  369. raise
  370. self.assertEqual(
  371. (
  372. self.flo.getTimezoneOffset(daylight),
  373. self.flo.getTimezoneOffset(standard),
  374. ),
  375. (daylightOffset, standardOffset),
  376. )
  377. finally:
  378. if originalTimezone is None:
  379. del os.environ["TZ"]
  380. else:
  381. os.environ["TZ"] = originalTimezone
  382. time.tzset()
  383. def test_getTimezoneOffsetWestOfUTC(self):
  384. """
  385. Attempt to verify that L{FileLogObserver.getTimezoneOffset} returns
  386. correct values for the current C{TZ} environment setting for at least
  387. some cases. This test method exercises a timezone that is west of UTC
  388. (and should produce positive results).
  389. """
  390. self._getTimezoneOffsetTest("America/New_York", 14400, 18000)
  391. def test_getTimezoneOffsetEastOfUTC(self):
  392. """
  393. Attempt to verify that L{FileLogObserver.getTimezoneOffset} returns
  394. correct values for the current C{TZ} environment setting for at least
  395. some cases. This test method exercises a timezone that is east of UTC
  396. (and should produce negative results).
  397. """
  398. self._getTimezoneOffsetTest("Europe/Berlin", -7200, -3600)
  399. def test_getTimezoneOffsetWithoutDaylightSavingTime(self):
  400. """
  401. Attempt to verify that L{FileLogObserver.getTimezoneOffset} returns
  402. correct values for the current C{TZ} environment setting for at least
  403. some cases. This test method exercises a timezone that does not use
  404. daylight saving time at all (so both summer and winter time test values
  405. should have the same offset).
  406. """
  407. # Test a timezone that doesn't have DST. Some mktime()
  408. # implementations available for testing seem happy to produce
  409. # results for this even though it's not entirely valid. Others
  410. # such as glibc>=2.28 return EOVERFLOW.
  411. self._getTimezoneOffsetTest("Africa/Johannesburg", -7200, -7200)
  412. def test_timeFormatting(self):
  413. """
  414. Test the method of L{FileLogObserver} which turns a timestamp into a
  415. human-readable string.
  416. """
  417. when = calendar.timegm((2001, 2, 3, 4, 5, 6, 7, 8, 0))
  418. # Pretend to be in US/Eastern for a moment
  419. self.flo.getTimezoneOffset = lambda when: 18000
  420. self.assertEqual(self.flo.formatTime(when), "2001-02-02 23:05:06-0500")
  421. # Okay now we're in Eastern Europe somewhere
  422. self.flo.getTimezoneOffset = lambda when: -3600
  423. self.assertEqual(self.flo.formatTime(when), "2001-02-03 05:05:06+0100")
  424. # And off in the Pacific or someplace like that
  425. self.flo.getTimezoneOffset = lambda when: -39600
  426. self.assertEqual(self.flo.formatTime(when), "2001-02-03 15:05:06+1100")
  427. # One of those weird places with a half-hour offset timezone
  428. self.flo.getTimezoneOffset = lambda when: 5400
  429. self.assertEqual(self.flo.formatTime(when), "2001-02-03 02:35:06-0130")
  430. # Half-hour offset in the other direction
  431. self.flo.getTimezoneOffset = lambda when: -5400
  432. self.assertEqual(self.flo.formatTime(when), "2001-02-03 05:35:06+0130")
  433. # Test an offset which is between 0 and 60 minutes to make sure the
  434. # sign comes out properly in that case.
  435. self.flo.getTimezoneOffset = lambda when: 1800
  436. self.assertEqual(self.flo.formatTime(when), "2001-02-03 03:35:06-0030")
  437. # Test an offset between 0 and 60 minutes in the other direction.
  438. self.flo.getTimezoneOffset = lambda when: -1800
  439. self.assertEqual(self.flo.formatTime(when), "2001-02-03 04:35:06+0030")
  440. # If a strftime-format string is present on the logger, it should
  441. # use that instead. Note we don't assert anything about day, hour
  442. # or minute because we cannot easily control what time.strftime()
  443. # thinks the local timezone is.
  444. self.flo.timeFormat = "%Y %m"
  445. self.assertEqual(self.flo.formatTime(when), "2001 02")
  446. def test_microsecondTimestampFormatting(self):
  447. """
  448. L{FileLogObserver.formatTime} supports a value of C{timeFormat} which
  449. includes C{"%f"}, a L{datetime}-only format specifier for microseconds.
  450. """
  451. self.flo.timeFormat = "%f"
  452. self.assertEqual("600000", self.flo.formatTime(112345.6))
  453. def test_loggingAnObjectWithBroken__str__(self):
  454. # HELLO, MCFLY
  455. self.lp.msg(EvilStr())
  456. self.assertEqual(len(self.out), 1)
  457. # Logging system shouldn't need to crap itself for this trivial case
  458. self.assertNotIn(self.ERROR_UNFORMATTABLE_OBJECT, self.out[0])
  459. def test_formattingAnObjectWithBroken__str__(self):
  460. self.lp.msg(format="%(blat)s", blat=EvilStr())
  461. self.assertEqual(len(self.out), 1)
  462. self.assertIn(self.ERROR_INVALID_FORMAT, self.out[0])
  463. def test_brokenSystem__str__(self):
  464. self.lp.msg("huh", system=EvilStr())
  465. self.assertEqual(len(self.out), 1)
  466. self.assertIn(self.ERROR_FORMAT, self.out[0])
  467. def test_formattingAnObjectWithBroken__repr__Indirect(self):
  468. self.lp.msg(format="%(blat)s", blat=[EvilRepr()])
  469. self.assertEqual(len(self.out), 1)
  470. self.assertIn(self.ERROR_UNFORMATTABLE_OBJECT, self.out[0])
  471. def test_systemWithBroker__repr__Indirect(self):
  472. self.lp.msg("huh", system=[EvilRepr()])
  473. self.assertEqual(len(self.out), 1)
  474. self.assertIn(self.ERROR_UNFORMATTABLE_OBJECT, self.out[0])
  475. def test_simpleBrokenFormat(self):
  476. self.lp.msg(format="hooj %s %s", blat=1)
  477. self.assertEqual(len(self.out), 1)
  478. self.assertIn(self.ERROR_INVALID_FORMAT, self.out[0])
  479. def test_ridiculousFormat(self):
  480. self.lp.msg(format=42, blat=1)
  481. self.assertEqual(len(self.out), 1)
  482. self.assertIn(self.ERROR_INVALID_FORMAT, self.out[0])
  483. def test_evilFormat__repr__And__str__(self):
  484. self.lp.msg(format=EvilReprStr(), blat=1)
  485. self.assertEqual(len(self.out), 1)
  486. self.assertIn(self.ERROR_PATHOLOGICAL, self.out[0])
  487. def test_strangeEventDict(self):
  488. """
  489. This kind of eventDict used to fail silently, so test it does.
  490. """
  491. self.lp.msg(message="", isError=False)
  492. self.assertEqual(len(self.out), 0)
  493. def _startLoggingCleanup(self):
  494. """
  495. Cleanup after a startLogging() call that mutates the hell out of some
  496. global state.
  497. """
  498. self.addCleanup(log.theLogPublisher._stopLogging)
  499. self.addCleanup(setattr, sys, "stdout", sys.stdout)
  500. self.addCleanup(setattr, sys, "stderr", sys.stderr)
  501. def test_printToStderrSetsIsError(self):
  502. """
  503. startLogging()'s overridden sys.stderr should consider everything
  504. written to it an error.
  505. """
  506. self._startLoggingCleanup()
  507. fakeFile = StringIO()
  508. log.startLogging(fakeFile)
  509. def observe(event):
  510. observed.append(event)
  511. observed = []
  512. log.addObserver(observe)
  513. print("Hello, world.", file=sys.stderr)
  514. self.assertEqual(observed[0]["isError"], 1)
  515. def test_startLogging(self):
  516. """
  517. startLogging() installs FileLogObserver and overrides sys.stdout and
  518. sys.stderr.
  519. """
  520. origStdout, origStderr = sys.stdout, sys.stderr
  521. self._startLoggingCleanup()
  522. # When done with test, reset stdout and stderr to current values:
  523. fakeFile = StringIO()
  524. observer = log.startLogging(fakeFile)
  525. self.addCleanup(observer.stop)
  526. log.msg("Hello!")
  527. self.assertIn("Hello!", fakeFile.getvalue())
  528. self.assertIsInstance(sys.stdout, LoggingFile)
  529. self.assertEqual(sys.stdout.level, NewLogLevel.info)
  530. encoding = getattr(origStdout, "encoding", None)
  531. if not encoding:
  532. encoding = sys.getdefaultencoding()
  533. self.assertEqual(sys.stdout.encoding.upper(), encoding.upper())
  534. self.assertIsInstance(sys.stderr, LoggingFile)
  535. self.assertEqual(sys.stderr.level, NewLogLevel.error)
  536. encoding = getattr(origStderr, "encoding", None)
  537. if not encoding:
  538. encoding = sys.getdefaultencoding()
  539. self.assertEqual(sys.stderr.encoding.upper(), encoding.upper())
  540. def test_startLoggingTwice(self):
  541. """
  542. There are some obscure error conditions that can occur when logging is
  543. started twice. See http://twistedmatrix.com/trac/ticket/3289 for more
  544. information.
  545. """
  546. self._startLoggingCleanup()
  547. # The bug is particular to the way that the t.p.log 'global' function
  548. # handle stdout. If we use our own stream, the error doesn't occur. If
  549. # we use our own LogPublisher, the error doesn't occur.
  550. sys.stdout = StringIO()
  551. def showError(eventDict):
  552. if eventDict["isError"]:
  553. sys.__stdout__.write(eventDict["failure"].getTraceback())
  554. log.addObserver(showError)
  555. self.addCleanup(log.removeObserver, showError)
  556. observer = log.startLogging(sys.stdout)
  557. self.addCleanup(observer.stop)
  558. # At this point, we expect that sys.stdout is a StdioOnnaStick object.
  559. self.assertIsInstance(sys.stdout, LoggingFile)
  560. fakeStdout = sys.stdout
  561. observer = log.startLogging(sys.stdout)
  562. self.assertIs(sys.stdout, fakeStdout)
  563. def test_startLoggingOverridesWarning(self):
  564. """
  565. startLogging() overrides global C{warnings.showwarning} such that
  566. warnings go to Twisted log observers.
  567. """
  568. self._startLoggingCleanup()
  569. newPublisher = NewLogPublisher()
  570. class SysModule:
  571. stdout = object()
  572. stderr = object()
  573. tempLogPublisher = LogPublisher(
  574. newPublisher,
  575. newPublisher,
  576. logBeginner=LogBeginner(newPublisher, StringIO(), SysModule, warnings),
  577. )
  578. # Trial reports warnings in two ways. First, it intercepts the global
  579. # 'showwarning' function *itself*, after starting logging (by way of
  580. # the '_collectWarnings' function which collects all warnings as a
  581. # around the test's 'run' method). Second, it has a log observer which
  582. # immediately reports warnings when they're propagated into the log
  583. # system (which, in normal operation, happens only at the end of the
  584. # test case). In order to avoid printing a spurious warning in this
  585. # test, we first replace the global log publisher's 'showwarning' in
  586. # the module with our own.
  587. self.patch(log, "theLogPublisher", tempLogPublisher)
  588. # And, one last thing, pretend we're starting from a fresh import, or
  589. # warnings.warn won't be patched at all.
  590. log._oldshowwarning = None
  591. # Global mutable state is bad, kids. Stay in school.
  592. fakeFile = StringIO()
  593. # We didn't previously save log messages, so let's make sure we don't
  594. # save them any more.
  595. evt = {"pre-start": "event"}
  596. received = []
  597. def preStartObserver(x):
  598. if "pre-start" in x.keys():
  599. received.append(x)
  600. newPublisher(evt)
  601. newPublisher.addObserver(preStartObserver)
  602. log.startLogging(fakeFile, setStdout=False)
  603. self.addCleanup(tempLogPublisher._stopLogging)
  604. self.assertEqual(received, [])
  605. warnings.warn("hello!")
  606. output = fakeFile.getvalue()
  607. self.assertIn("UserWarning: hello!", output)
  608. def test_emitPrefix(self):
  609. """
  610. FileLogObserver.emit() will add a timestamp and system prefix to its
  611. file output.
  612. """
  613. output = StringIO()
  614. flo = log.FileLogObserver(output)
  615. events = []
  616. def observer(event):
  617. # Capture the event for reference and pass it along to flo
  618. events.append(event)
  619. flo.emit(event)
  620. publisher = log.LogPublisher()
  621. publisher.addObserver(observer)
  622. publisher.msg("Hello!")
  623. self.assertEqual(len(events), 1)
  624. event = events[0]
  625. result = output.getvalue()
  626. prefix = "{time} [{system}] ".format(
  627. time=flo.formatTime(event["time"]),
  628. system=event["system"],
  629. )
  630. self.assertTrue(
  631. result.startswith(prefix),
  632. f"{result!r} does not start with {prefix!r}",
  633. )
  634. def test_emitNewline(self):
  635. """
  636. FileLogObserver.emit() will append a newline to its file output.
  637. """
  638. output = StringIO()
  639. flo = log.FileLogObserver(output)
  640. publisher = log.LogPublisher()
  641. publisher.addObserver(flo.emit)
  642. publisher.msg("Hello!")
  643. result = output.getvalue()
  644. suffix = "Hello!\n"
  645. self.assertTrue(
  646. result.endswith(suffix),
  647. f"{result!r} does not end with {suffix!r}",
  648. )
  649. class PythonLoggingObserverTests(unittest.SynchronousTestCase):
  650. """
  651. Test the bridge with python logging module.
  652. """
  653. def setUp(self):
  654. rootLogger = logging.getLogger("")
  655. originalLevel = rootLogger.getEffectiveLevel()
  656. rootLogger.setLevel(logging.DEBUG)
  657. @self.addCleanup
  658. def restoreLevel():
  659. rootLogger.setLevel(originalLevel)
  660. self.hdlr, self.out = handlerAndBytesIO()
  661. rootLogger.addHandler(self.hdlr)
  662. @self.addCleanup
  663. def removeLogger():
  664. rootLogger.removeHandler(self.hdlr)
  665. self.hdlr.close()
  666. self.lp = log.LogPublisher()
  667. self.obs = log.PythonLoggingObserver()
  668. self.lp.addObserver(self.obs.emit)
  669. def test_singleString(self):
  670. """
  671. Test simple output, and default log level.
  672. """
  673. self.lp.msg("Hello, world.")
  674. self.assertIn(b"Hello, world.", self.out.getvalue())
  675. self.assertIn(b"INFO", self.out.getvalue())
  676. def test_errorString(self):
  677. """
  678. Test error output.
  679. """
  680. f = failure.Failure(ValueError("That is bad."))
  681. self.lp.msg(failure=f, isError=True)
  682. prefix = b"CRITICAL:"
  683. output = self.out.getvalue()
  684. self.assertTrue(
  685. output.startswith(prefix),
  686. f"Does not start with {prefix!r}: {output!r}",
  687. )
  688. def test_formatString(self):
  689. """
  690. Test logging with a format.
  691. """
  692. self.lp.msg(format="%(bar)s oo %(foo)s", bar="Hello", foo="world")
  693. self.assertIn(b"Hello oo world", self.out.getvalue())
  694. def test_customLevel(self):
  695. """
  696. Test the logLevel keyword for customizing level used.
  697. """
  698. self.lp.msg("Spam egg.", logLevel=logging.ERROR)
  699. self.assertIn(b"Spam egg.", self.out.getvalue())
  700. self.assertIn(b"ERROR", self.out.getvalue())
  701. self.out.seek(0, 0)
  702. self.out.truncate()
  703. self.lp.msg("Foo bar.", logLevel=logging.WARNING)
  704. self.assertIn(b"Foo bar.", self.out.getvalue())
  705. self.assertIn(b"WARNING", self.out.getvalue())
  706. def test_strangeEventDict(self):
  707. """
  708. Verify that an event dictionary which is not an error and has an empty
  709. message isn't recorded.
  710. """
  711. self.lp.msg(message="", isError=False)
  712. self.assertEqual(self.out.getvalue(), b"")
  713. class PythonLoggingIntegrationTests(unittest.SynchronousTestCase):
  714. """
  715. Test integration of python logging bridge.
  716. """
  717. def test_startStopObserver(self):
  718. """
  719. Test that start and stop methods of the observer actually register
  720. and unregister to the log system.
  721. """
  722. oldAddObserver = log.addObserver
  723. oldRemoveObserver = log.removeObserver
  724. l = []
  725. try:
  726. log.addObserver = l.append
  727. log.removeObserver = l.remove
  728. obs = log.PythonLoggingObserver()
  729. obs.start()
  730. self.assertEqual(l[0], obs.emit)
  731. obs.stop()
  732. self.assertEqual(len(l), 0)
  733. finally:
  734. log.addObserver = oldAddObserver
  735. log.removeObserver = oldRemoveObserver
  736. def test_inheritance(self):
  737. """
  738. Test that we can inherit L{log.PythonLoggingObserver} and use super:
  739. that's basically a validation that L{log.PythonLoggingObserver} is
  740. new-style class.
  741. """
  742. class MyObserver(log.PythonLoggingObserver):
  743. def emit(self, eventDict):
  744. super().emit(eventDict)
  745. obs = MyObserver()
  746. l = []
  747. oldEmit = log.PythonLoggingObserver.emit
  748. try:
  749. log.PythonLoggingObserver.emit = l.append
  750. obs.emit("foo")
  751. self.assertEqual(len(l), 1)
  752. finally:
  753. log.PythonLoggingObserver.emit = oldEmit
  754. class DefaultObserverTests(unittest.SynchronousTestCase):
  755. """
  756. Test the default observer.
  757. """
  758. def test_failureLogger(self):
  759. """
  760. The reason argument passed to log.err() appears in the report
  761. generated by DefaultObserver.
  762. """
  763. self.catcher = []
  764. self.observer = self.catcher.append
  765. log.addObserver(self.observer)
  766. self.addCleanup(log.removeObserver, self.observer)
  767. obs = log.DefaultObserver()
  768. obs.stderr = StringIO()
  769. obs.start()
  770. self.addCleanup(obs.stop)
  771. reason = "The reason."
  772. log.err(Exception(), reason)
  773. errors = self.flushLoggedErrors()
  774. self.assertIn(reason, obs.stderr.getvalue())
  775. self.assertEqual(len(errors), 1)
  776. def test_emitEventWithBrokenRepr(self):
  777. """
  778. DefaultObserver.emit() does not raise when it observes an error event
  779. with a message that causes L{repr} to raise.
  780. """
  781. class Ouch:
  782. def __repr__(self) -> str:
  783. return str(1 / 0)
  784. message = ("foo", Ouch())
  785. event = dict(message=message, isError=1)
  786. observer = log.DefaultObserver()
  787. with StringIO() as output:
  788. observer.stderr = output
  789. observer.emit(event)
  790. self.assertTrue(output.getvalue().startswith("foo <Ouch instance"))
  791. class StdioOnnaStickTests(unittest.SynchronousTestCase):
  792. """
  793. StdioOnnaStick should act like the normal sys.stdout object.
  794. """
  795. def setUp(self):
  796. self.resultLogs = []
  797. log.addObserver(self.resultLogs.append)
  798. def tearDown(self):
  799. log.removeObserver(self.resultLogs.append)
  800. def getLogMessages(self):
  801. return ["".join(d["message"]) for d in self.resultLogs]
  802. def test_write(self):
  803. """
  804. Writing to a StdioOnnaStick instance results in Twisted log messages.
  805. Log messages are generated every time a '\\n' is encountered.
  806. """
  807. stdio = log.StdioOnnaStick()
  808. stdio.write("Hello there\nThis is a test")
  809. self.assertEqual(self.getLogMessages(), ["Hello there"])
  810. stdio.write("!\n")
  811. self.assertEqual(self.getLogMessages(), ["Hello there", "This is a test!"])
  812. def test_metadata(self):
  813. """
  814. The log messages written by StdioOnnaStick have printed=1 keyword, and
  815. by default are not errors.
  816. """
  817. stdio = log.StdioOnnaStick()
  818. stdio.write("hello\n")
  819. self.assertFalse(self.resultLogs[0]["isError"])
  820. self.assertTrue(self.resultLogs[0]["printed"])
  821. def test_writeLines(self):
  822. """
  823. Writing lines to a StdioOnnaStick results in Twisted log messages.
  824. """
  825. stdio = log.StdioOnnaStick()
  826. stdio.writelines(["log 1", "log 2"])
  827. self.assertEqual(self.getLogMessages(), ["log 1", "log 2"])
  828. def test_print(self):
  829. """
  830. When StdioOnnaStick is set as sys.stdout, prints become log messages.
  831. """
  832. oldStdout = sys.stdout
  833. sys.stdout = log.StdioOnnaStick()
  834. self.addCleanup(setattr, sys, "stdout", oldStdout)
  835. print("This", end=" ")
  836. print("is a test")
  837. self.assertEqual(self.getLogMessages(), ["This is a test"])
  838. def test_error(self):
  839. """
  840. StdioOnnaStick created with isError=True log messages as errors.
  841. """
  842. stdio = log.StdioOnnaStick(isError=True)
  843. stdio.write("log 1\n")
  844. self.assertTrue(self.resultLogs[0]["isError"])
  845. def test_unicode(self):
  846. """
  847. StdioOnnaStick converts unicode prints to byte strings on Python 2, in
  848. order to be compatible with the normal stdout/stderr objects.
  849. On Python 3, the prints are left unmodified.
  850. """
  851. unicodeString = "Hello, \N{VULGAR FRACTION ONE HALF} world."
  852. stdio = log.StdioOnnaStick(encoding="utf-8")
  853. self.assertEqual(stdio.encoding, "utf-8")
  854. stdio.write(unicodeString + "\n")
  855. stdio.writelines(["Also, " + unicodeString])
  856. oldStdout = sys.stdout
  857. sys.stdout = stdio
  858. self.addCleanup(setattr, sys, "stdout", oldStdout)
  859. # This should go to the log, utf-8 encoded too:
  860. print(unicodeString)
  861. self.assertEqual(
  862. self.getLogMessages(),
  863. [unicodeString, "Also, " + unicodeString, unicodeString],
  864. )