_global.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. # -*- test-case-name: twisted.logger.test.test_global -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. This module includes process-global state associated with the logging system,
  6. and implementation of logic for managing that global state.
  7. """
  8. import sys
  9. import warnings
  10. from typing import IO, Any, Iterable, Optional, Type
  11. from twisted.python.compat import currentframe
  12. from twisted.python.reflect import qual
  13. from ._buffer import LimitedHistoryLogObserver
  14. from ._file import FileLogObserver
  15. from ._filter import FilteringLogObserver, LogLevelFilterPredicate
  16. from ._format import eventAsText
  17. from ._interfaces import ILogObserver
  18. from ._io import LoggingFile
  19. from ._levels import LogLevel
  20. from ._logger import Logger
  21. from ._observer import LogPublisher
  22. MORE_THAN_ONCE_WARNING = (
  23. "Warning: primary log target selected twice at <{fileNow}:{lineNow}> - "
  24. "previously selected at <{fileThen}:{lineThen}>. Remove one of the calls "
  25. "to beginLoggingTo."
  26. )
  27. class LogBeginner:
  28. """
  29. A L{LogBeginner} holds state related to logging before logging has begun,
  30. and begins logging when told to do so. Logging "begins" when someone has
  31. selected a set of observers, like, for example, a L{FileLogObserver} that
  32. writes to a file on disk, or to standard output.
  33. Applications will not typically need to instantiate this class, except
  34. those which intend to initialize the global logging system themselves,
  35. which may wish to instantiate this for testing. The global instance for
  36. the current process is exposed as
  37. L{twisted.logger.globalLogBeginner}.
  38. Before logging has begun, a L{LogBeginner} will:
  39. 1. Log any critical messages (e.g.: unhandled exceptions) to the given
  40. file-like object.
  41. 2. Save (a limited number of) log events in a
  42. L{LimitedHistoryLogObserver}.
  43. @cvar _DEFAULT_BUFFER_SIZE: The default size for the initial log events
  44. buffer.
  45. @ivar _initialBuffer: A buffer of messages logged before logging began.
  46. @ivar _publisher: The log publisher passed in to L{LogBeginner}'s
  47. constructor.
  48. @ivar _log: The logger used to log messages about the operation of the
  49. L{LogBeginner} itself.
  50. @ivar _stdio: An object with C{stderr} and C{stdout} attributes (like the
  51. L{sys} module) which will be replaced when redirecting standard I/O.
  52. @ivar _temporaryObserver: If not L{None}, an L{ILogObserver} that observes
  53. events on C{_publisher} for this L{LogBeginner}.
  54. """
  55. _DEFAULT_BUFFER_SIZE = 200
  56. def __init__(
  57. self,
  58. publisher: LogPublisher,
  59. errorStream: IO[Any],
  60. stdio: object,
  61. warningsModule: Any,
  62. initialBufferSize: Optional[int] = None,
  63. ) -> None:
  64. """
  65. Initialize this L{LogBeginner}.
  66. @param initialBufferSize: The size of the event buffer into which
  67. events are collected until C{beginLoggingTo} is called. Or
  68. C{None} to use the default size.
  69. """
  70. if initialBufferSize is None:
  71. initialBufferSize = self._DEFAULT_BUFFER_SIZE
  72. self._initialBuffer = LimitedHistoryLogObserver(size=initialBufferSize)
  73. self._publisher = publisher
  74. self._log = Logger(observer=publisher)
  75. self._stdio = stdio
  76. self._warningsModule = warningsModule
  77. self._temporaryObserver: Optional[ILogObserver] = LogPublisher(
  78. self._initialBuffer,
  79. FilteringLogObserver(
  80. FileLogObserver(
  81. errorStream,
  82. lambda event: eventAsText(
  83. event,
  84. includeTimestamp=False,
  85. includeSystem=False,
  86. )
  87. + "\n",
  88. ),
  89. [LogLevelFilterPredicate(defaultLogLevel=LogLevel.critical)],
  90. ),
  91. )
  92. self._previousBegin = ("", 0)
  93. publisher.addObserver(self._temporaryObserver)
  94. self._oldshowwarning = warningsModule.showwarning
  95. def beginLoggingTo(
  96. self,
  97. observers: Iterable[ILogObserver],
  98. discardBuffer: bool = False,
  99. redirectStandardIO: bool = True,
  100. ) -> None:
  101. """
  102. Begin logging to the given set of observers. This will:
  103. 1. Add all the observers given in C{observers} to the
  104. L{LogPublisher} associated with this L{LogBeginner}.
  105. 2. Optionally re-direct standard output and standard error streams
  106. to the logging system.
  107. 3. Re-play any messages that were previously logged to that
  108. publisher to the new observers, if C{discardBuffer} is not set.
  109. 4. Stop logging critical errors from the L{LogPublisher} as strings
  110. to the C{errorStream} associated with this L{LogBeginner}, and
  111. allow them to be logged normally.
  112. 5. Re-direct warnings from the L{warnings} module associated with
  113. this L{LogBeginner} to log messages.
  114. @note: Since a L{LogBeginner} is designed to encapsulate the transition
  115. between process-startup and log-system-configuration, this method
  116. is intended to be invoked I{once}.
  117. @param observers: The observers to register.
  118. @param discardBuffer: Whether to discard the buffer and not re-play it
  119. to the added observers. (This argument is provided mainly for
  120. compatibility with legacy concerns.)
  121. @param redirectStandardIO: If true, redirect standard output and
  122. standard error to the observers.
  123. """
  124. caller = currentframe(1)
  125. filename = caller.f_code.co_filename
  126. lineno = caller.f_lineno
  127. for observer in observers:
  128. self._publisher.addObserver(observer)
  129. if self._temporaryObserver is not None:
  130. self._publisher.removeObserver(self._temporaryObserver)
  131. if not discardBuffer:
  132. self._initialBuffer.replayTo(self._publisher)
  133. self._temporaryObserver = None
  134. self._warningsModule.showwarning = self.showwarning
  135. else:
  136. previousFile, previousLine = self._previousBegin
  137. self._log.warn(
  138. MORE_THAN_ONCE_WARNING,
  139. fileNow=filename,
  140. lineNow=lineno,
  141. fileThen=previousFile,
  142. lineThen=previousLine,
  143. )
  144. self._previousBegin = (filename, lineno)
  145. if redirectStandardIO:
  146. streams = [("stdout", LogLevel.info), ("stderr", LogLevel.error)]
  147. else:
  148. streams = []
  149. for stream, level in streams:
  150. oldStream = getattr(self._stdio, stream)
  151. loggingFile = LoggingFile(
  152. logger=Logger(namespace=stream, observer=self._publisher),
  153. level=level,
  154. encoding=getattr(oldStream, "encoding", None),
  155. )
  156. setattr(self._stdio, stream, loggingFile)
  157. def showwarning(
  158. self,
  159. message: str,
  160. category: Type[Warning],
  161. filename: str,
  162. lineno: int,
  163. file: Optional[IO[Any]] = None,
  164. line: Optional[str] = None,
  165. ) -> None:
  166. """
  167. Twisted-enabled wrapper around L{warnings.showwarning}.
  168. If C{file} is L{None}, the default behaviour is to emit the warning to
  169. the log system, otherwise the original L{warnings.showwarning} Python
  170. function is called.
  171. @param message: A warning message to emit.
  172. @param category: A warning category to associate with C{message}.
  173. @param filename: A file name for the source code file issuing the
  174. warning.
  175. @param lineno: A line number in the source file where the warning was
  176. issued.
  177. @param file: A file to write the warning message to. If L{None},
  178. write to L{sys.stderr}.
  179. @param line: A line of source code to include with the warning message.
  180. If L{None}, attempt to read the line from C{filename} and
  181. C{lineno}.
  182. """
  183. if file is None:
  184. self._log.warn(
  185. "{filename}:{lineno}: {category}: {warning}",
  186. warning=message,
  187. category=qual(category),
  188. filename=filename,
  189. lineno=lineno,
  190. )
  191. else:
  192. self._oldshowwarning(message, category, filename, lineno, file, line)
  193. globalLogPublisher = LogPublisher()
  194. globalLogBeginner = LogBeginner(globalLogPublisher, sys.stderr, sys, warnings)