Commit 2dc887a3 authored by Fred Drake's avatar Fred Drake

clean up a little: only do the startup log handler dance when we need to;

the signal handler installation is abstracted in the same way
parent 9af1e612
......@@ -33,14 +33,17 @@ def start_zope(cfg):
return
check_python_version()
starter = ZopeStarter(cfg)
if sys.platform[:3].lower() == "win":
starter = WindowsZopeStarter(cfg)
else:
starter = UnixZopeStarter(cfg)
starter.setupLocale()
# we log events to the root logger, which is backed by a
# "StartupHandler" log handler. The "StartupHandler" outputs to
# stderr but also buffers log messages. When the "real" loggers
# are set up, we flush accumulated messages in StartupHandler's
# buffers to the real logger.
starter.setupStartupHandler()
starter.setupInitialLogging()
starter.setupSecurityOptions()
# Start ZServer servers before we drop privileges so we can bind to
# "low" ports:
......@@ -55,10 +58,8 @@ def start_zope(cfg):
# emit a "ready" message in order to prevent the kinds of emails
# to the Zope maillist in which people claim that Zope has "frozen"
# after it has emitted ZServer messages.
starter.info('Ready to handle requests')
starter.removeStartupHandler()
starter.setupConfiguredLoggers()
starter.flushStartupHandlerBuffer()
logger.info('Ready to handle requests')
starter.setupFinalLogging()
started = True
......@@ -82,6 +83,8 @@ class ZopeStarter:
self.cfg = cfg
self.event_logger = logging.getLogger()
# XXX does anyone actually use these three?
def info(self, msg):
logger.info(msg)
......@@ -91,13 +94,6 @@ class ZopeStarter:
def error(self, msg):
logger.error(msg)
def registerSignals(self):
if os.name == 'posix':
from Signals import Signals
Signals.registerZopeSignals([self.cfg.eventlog,
self.cfg.access,
self.cfg.trace])
def setupSecurityOptions(self):
import AccessControl
AccessControl.setImplementation(
......@@ -106,41 +102,6 @@ class ZopeStarter:
not self.cfg.skip_ownership_checking,
not self.cfg.skip_authentication_checking)
def setupStartupHandler(self):
# set up our initial logging environment (log everything to stderr
# if we're not in debug mode).
from ZConfig.components.logger.loghandler import StartupHandler
if self.cfg.eventlog is not None:
# get the lowest handler level. This is the effective level
# level at which which we will spew messages to the console
# during startup.
level = self.cfg.eventlog.getLowestHandlerLevel()
else:
level = logging.INFO
self.startup_handler = StartupHandler(sys.stderr)
self.startup_handler.setLevel(level)
formatter = logging.Formatter(
fmt='------\n%(asctime)s %(levelname)s %(name)s %(message)s',
datefmt='%Y-%m-%dT%H:%M:%S')
self.startup_handler.setFormatter(formatter)
if not self.cfg.debug_mode:
# prevent startup messages from going to stderr if we're not
# in debug mode
if os.path.exists('/dev/null'): # unix
devnull = '/dev/null'
else: # win32
devnull = 'nul:'
self.startup_handler = StartupHandler(open(devnull, 'w'))
# set up our event logger temporarily with a startup handler only
self.event_logger.handlers = []
self.event_logger.addHandler(self.startup_handler)
# set the initial logging level (this will be changed by the
# zconfig settings later)
self.event_logger.level = level
def setupLocale(self):
# set a locale if one has been specified in the config
if not self.cfg.locale:
......@@ -198,10 +159,6 @@ class ZopeStarter:
def dropPrivileges(self):
return dropPrivileges(self.cfg)
def removeStartupHandler(self):
if self.startup_handler in self.event_logger.handlers:
self.event_logger.removeHandler(self.startup_handler)
def setupConfiguredLoggers(self):
if self.cfg.zserver_read_only_mode:
# no log files written in read only mode
......@@ -215,10 +172,6 @@ class ZopeStarter:
if self.cfg.trace is not None:
self.cfg.trace()
def flushStartupHandlerBuffer(self):
logger = logging.getLogger('event')
self.startup_handler.flushBufferTo(logger)
def startZope(self):
# Import Zope
import Zope
......@@ -277,6 +230,65 @@ class ZopeStarter:
pass
class WindowsZopeStarter(ZopeStarter):
def registerSignals(self):
pass
def setupInitialLogging(self):
self.setupConfiguredLoggers()
def setupFinalLogging(self):
pass
class UnixZopeStarter(ZopeStarter):
def registerSignals(self):
from Signals import Signals
Signals.registerZopeSignals([self.cfg.eventlog,
self.cfg.access,
self.cfg.trace])
def setupInitialLogging(self):
# set up our initial logging environment (log everything to stderr
# if we're not in debug mode).
from ZConfig.components.logger.loghandler import StartupHandler
if self.cfg.eventlog is not None:
# get the lowest handler level. This is the effective level
# level at which which we will spew messages to the console
# during startup.
level = self.cfg.eventlog.getLowestHandlerLevel()
else:
level = logging.INFO
self.startup_handler = StartupHandler(sys.stderr)
self.startup_handler.setLevel(level)
formatter = logging.Formatter(
fmt='------\n%(asctime)s %(levelname)s %(name)s %(message)s',
datefmt='%Y-%m-%dT%H:%M:%S')
if not self.cfg.debug_mode:
# prevent startup messages from going to stderr if we're not
# in debug mode
self.startup_handler = StartupHandler(open('/dev/null', 'w'))
self.startup_handler.setFormatter(formatter)
# set up our event logger temporarily with a startup handler only
self.event_logger.handlers = []
self.event_logger.addHandler(self.startup_handler)
# set the initial logging level (this will be changed by the
# zconfig settings later)
self.event_logger.level = level
def setupFinalLogging(self):
if self.startup_handler in self.event_logger.handlers:
self.event_logger.removeHandler(self.startup_handler)
self.setupConfiguredLoggers()
logger = logging.getLogger('event')
self.startup_handler.flushBufferTo(logger)
def check_python_version():
# check for Python version
python_version = sys.version.split()[0]
......
......@@ -23,7 +23,7 @@ import unittest
import ZConfig
import Zope.Startup
from Zope.Startup import handlers
from Zope.Startup import ZopeStarter
from Zope.Startup import ZopeStarter, UnixZopeStarter
from App.config import getConfiguration, setConfiguration
......@@ -118,8 +118,8 @@ class ZopeStarterTestCase(unittest.TestCase):
level blather
</logfile>
</eventlog>""")
starter = ZopeStarter(conf)
starter.setupStartupHandler()
starter = UnixZopeStarter(conf)
starter.setupInitialLogging()
# startup handler should take on the level of the event log handler
# with the lowest level
......@@ -139,8 +139,8 @@ class ZopeStarterTestCase(unittest.TestCase):
level info
</logfile>
</eventlog>""")
starter = ZopeStarter(conf)
starter.setupStartupHandler()
starter = UnixZopeStarter(conf)
starter.setupInitialLogging()
self.failIfEqual(starter.startup_handler.stream, sys.stderr)
def testSetupZServerThreads(self):
......@@ -267,14 +267,12 @@ class ZopeStarterTestCase(unittest.TestCase):
</logger>
""")
try:
starter = ZopeStarter(conf)
starter.setupStartupHandler()
starter = UnixZopeStarter(conf)
starter.setupInitialLogging()
starter.info('hello')
starter.removeStartupHandler()
starter.setupConfiguredLoggers()
starter.setupFinalLogging()
logger = logging.getLogger()
self.assertEqual(logger.level, logging.INFO)
starter.flushStartupHandlerBuffer()
l = open(os.path.join(TEMPNAME, 'event.log')).read()
self.failUnless(l.find('hello') > -1)
self.failUnless(os.path.exists(os.path.join(TEMPNAME, 'Z2.log')))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment