Skip to content

Commit

Permalink
Merge tlogger-twistd-8235: Port twisted.application.app (and twistd) …
Browse files Browse the repository at this point in the history
…to use the new logging system.

Author: hawkowl
Reviewer: wsanchez
Fixes: #8235

git-svn-id: svn://svn.twistedmatrix.com/svn/Twisted/trunk@47084 bbbe8e31-12d6-0310-92fd-ac37d47ddeeb
  • Loading branch information
hawkowl committed Mar 26, 2016
1 parent b665a10 commit c575f1d
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 74 deletions.
62 changes: 44 additions & 18 deletions twisted/application/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
import getpass
import traceback
import signal
import warnings

from operator import attrgetter

from twisted import copyright, plugin
from twisted import copyright, plugin, logger
from twisted.application import service, reactors
from twisted.internet import defer
from twisted.persisted import sob
from twisted.python import runtime, log, usage, failure, util, logfile
from twisted.python.log import ILogObserver
from twisted.python.reflect import qual, namedAny

# Expose the new implementation of installReactor at the old location.
Expand Down Expand Up @@ -141,7 +141,8 @@ def __init__(self, options):
class AppLogger(object):
"""
An L{AppLogger} attaches the configured log observer specified on the
commandline to a L{ServerOptions} object or the custom L{ILogObserver}.
commandline to a L{ServerOptions} object, a custom L{logger.ILogObserver},
or a legacy custom {log.ILogObserver}.
@ivar _logfilename: The name of the file to which to log, if other than the
default.
Expand All @@ -151,7 +152,8 @@ class AppLogger(object):
None.
@ivar _observer: log observer added at C{start} and removed at C{stop}.
@type _observer: C{callable}
@type _observer: a callable that implements L{logger.ILogObserver} or
L{log.ILogObserver}.
"""
_observer = None

Expand All @@ -168,24 +170,46 @@ def start(self, application):
Initialize the global logging system for the given application.
If a custom logger was specified on the command line it will be used.
If not, and an L{ILogObserver} component has been set on
C{application}, then it will be used as the log observer. Otherwise a
log observer will be created based on the command-line options for
built-in loggers (e.g. C{--logfile}).
If not, and an L{logger.ILogObserver} or legacy L{log.ILogObserver}
component has been set on C{application}, then it will be used as the
log observer. Otherwise a log observer will be created based on the
command line options for built-in loggers (e.g. C{--logfile}).
@param application: The application on which to check for an
L{ILogObserver}.
L{logger.ILogObserver} or legacy L{log.ILogObserver}.
@type application: L{twisted.python.components.Componentized}
"""
if self._observerFactory is not None:
observer = self._observerFactory()
else:
observer = application.getComponent(ILogObserver, None)
observer = application.getComponent(logger.ILogObserver, None)
if observer is None:
# If there's no new ILogObserver, try the legacy one
observer = application.getComponent(log.ILogObserver, None)

if observer is None:
observer = self._getLogObserver()
self._observer = observer
log.startLoggingWithObserver(self._observer)

if logger.ILogObserver.providedBy(self._observer):
observers = [self._observer]
elif log.ILogObserver.providedBy(self._observer):
observers = [logger.LegacyLogObserverWrapper(self._observer)]
else:
warnings.warn(
("Passing a logger factory which makes log observers which do "
"not implement twisted.logger.ILogObserver or "
"twisted.python.log.ILogObserver to "
"twisted.application.app.AppLogger was deprecated in "
"Twisted 16.2. Please use a factory that produces "
"twisted.logger.ILogObserver (or the legacy "
"twisted.python.log.ILogObserver) implementing objects "
"instead."),
DeprecationWarning,
stacklevel=2)
observers = [logger.LegacyLogObserverWrapper(self._observer)]

logger.globalLogBeginner.beginLoggingTo(observers)
self._initialLog()


Expand All @@ -194,10 +218,12 @@ def _initialLog(self):
Print twistd start log message.
"""
from twisted.internet import reactor
log.msg("twistd %s (%s %s) starting up." % (
copyright.version, sys.executable, runtime.shortPythonVersion())
)
log.msg('reactor class: %s.' % (qual(reactor.__class__),))
logger._loggerFor(self).info(
"twistd {version} ({exe} {pyVersion}) starting up.",
version=copyright.version, exe=sys.executable,
pyVersion=runtime.shortPythonVersion())
logger._loggerFor(self).info('reactor class: {reactor}.',
reactor=qual(reactor.__class__))


def _getLogObserver(self):
Expand All @@ -209,16 +235,16 @@ def _getLogObserver(self):
logFile = sys.stdout
else:
logFile = logfile.LogFile.fromFullPath(self._logfilename)
return log.FileLogObserver(logFile).emit
return logger.textFileLogObserver(logFile)


def stop(self):
"""
Remove all log observers previously set up by L{AppLogger.start}.
"""
log.msg("Server Shut Down.")
logger._loggerFor(self).info("Server Shut Down.")
if self._observer is not None:
log.removeObserver(self._observer)
logger.globalLogPublisher.removeObserver(self._observer)
self._observer = None


Expand Down
14 changes: 5 additions & 9 deletions twisted/internet/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@

from twisted.python import log, failure, components
from twisted.internet import interfaces, error, defer
from twisted.logger import Logger

_log = Logger()
_logFor = lambda _:_log.__get__(_, _.__class__)

from twisted.logger import _loggerFor


@implementer(interfaces.IProtocolFactory, interfaces.ILoggingContext)
Expand Down Expand Up @@ -73,8 +69,8 @@ def doStart(self):
"""
if not self.numPorts:
if self.noisy:
_logFor(self).info("Starting factory {factory!r}",
factory=self)
_loggerFor(self).info("Starting factory {factory!r}",
factory=self)
self.startFactory()
self.numPorts = self.numPorts + 1

Expand All @@ -90,8 +86,8 @@ def doStop(self):
self.numPorts = self.numPorts - 1
if not self.numPorts:
if self.noisy:
_logFor(self).info("Stopping factory {factory!r}",
factory=self)
_loggerFor(self).info("Stopping factory {factory!r}",
factory=self)
self.stopFactory()

def startFactory(self):
Expand Down
4 changes: 2 additions & 2 deletions twisted/logger/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def oops(self, data):
"extractField",

# From ._logger
"Logger",
"Logger", "_loggerFor",

# From ._observer
"ILogObserver", "LogPublisher",
Expand Down Expand Up @@ -94,7 +94,7 @@ def oops(self, data):
formatEvent, formatEventAsClassicLogText, formatTime, timeFormatRFC3339,
)

from ._logger import Logger
from ._logger import Logger, _loggerFor

from ._observer import ILogObserver, LogPublisher

Expand Down
5 changes: 5 additions & 0 deletions twisted/logger/_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,8 @@ def critical(self, format=None, **kwargs):
later execution.
"""
self.emit(LogLevel.critical, format, **kwargs)



_log = Logger()
_loggerFor = lambda obj:_log.__get__(obj, obj.__class__)
4 changes: 2 additions & 2 deletions twisted/scripts/_twistd_unix.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
switchUID, uidFromString, gidFromString, untilConcludes)
from twisted.application import app, service
from twisted.internet.interfaces import IReactorDaemonize
from twisted import copyright
from twisted import copyright, logger
from twisted.python.runtime import platformType


Expand Down Expand Up @@ -167,7 +167,7 @@ def rotateLog(signal, frame):
from twisted.internet import reactor
reactor.callFromThread(logFile.rotate)
signal.signal(signal.SIGUSR1, rotateLog)
return log.FileLogObserver(logFile).emit
return logger.textFileLogObserver(logFile)



Expand Down
Loading

0 comments on commit c575f1d

Please sign in to comment.