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): ...@@ -33,14 +33,17 @@ def start_zope(cfg):
return return
check_python_version() check_python_version()
starter = ZopeStarter(cfg) if sys.platform[:3].lower() == "win":
starter = WindowsZopeStarter(cfg)
else:
starter = UnixZopeStarter(cfg)
starter.setupLocale() starter.setupLocale()
# we log events to the root logger, which is backed by a # we log events to the root logger, which is backed by a
# "StartupHandler" log handler. The "StartupHandler" outputs to # "StartupHandler" log handler. The "StartupHandler" outputs to
# stderr but also buffers log messages. When the "real" loggers # stderr but also buffers log messages. When the "real" loggers
# are set up, we flush accumulated messages in StartupHandler's # are set up, we flush accumulated messages in StartupHandler's
# buffers to the real logger. # buffers to the real logger.
starter.setupStartupHandler() starter.setupInitialLogging()
starter.setupSecurityOptions() starter.setupSecurityOptions()
# Start ZServer servers before we drop privileges so we can bind to # Start ZServer servers before we drop privileges so we can bind to
# "low" ports: # "low" ports:
...@@ -55,10 +58,8 @@ def start_zope(cfg): ...@@ -55,10 +58,8 @@ def start_zope(cfg):
# emit a "ready" message in order to prevent the kinds of emails # emit a "ready" message in order to prevent the kinds of emails
# to the Zope maillist in which people claim that Zope has "frozen" # to the Zope maillist in which people claim that Zope has "frozen"
# after it has emitted ZServer messages. # after it has emitted ZServer messages.
starter.info('Ready to handle requests') logger.info('Ready to handle requests')
starter.removeStartupHandler() starter.setupFinalLogging()
starter.setupConfiguredLoggers()
starter.flushStartupHandlerBuffer()
started = True started = True
...@@ -82,6 +83,8 @@ class ZopeStarter: ...@@ -82,6 +83,8 @@ class ZopeStarter:
self.cfg = cfg self.cfg = cfg
self.event_logger = logging.getLogger() self.event_logger = logging.getLogger()
# XXX does anyone actually use these three?
def info(self, msg): def info(self, msg):
logger.info(msg) logger.info(msg)
...@@ -91,13 +94,6 @@ class ZopeStarter: ...@@ -91,13 +94,6 @@ class ZopeStarter:
def error(self, msg): def error(self, msg):
logger.error(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): def setupSecurityOptions(self):
import AccessControl import AccessControl
AccessControl.setImplementation( AccessControl.setImplementation(
...@@ -106,41 +102,6 @@ class ZopeStarter: ...@@ -106,41 +102,6 @@ class ZopeStarter:
not self.cfg.skip_ownership_checking, not self.cfg.skip_ownership_checking,
not self.cfg.skip_authentication_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): def setupLocale(self):
# set a locale if one has been specified in the config # set a locale if one has been specified in the config
if not self.cfg.locale: if not self.cfg.locale:
...@@ -198,10 +159,6 @@ class ZopeStarter: ...@@ -198,10 +159,6 @@ class ZopeStarter:
def dropPrivileges(self): def dropPrivileges(self):
return dropPrivileges(self.cfg) 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): def setupConfiguredLoggers(self):
if self.cfg.zserver_read_only_mode: if self.cfg.zserver_read_only_mode:
# no log files written in read only mode # no log files written in read only mode
...@@ -215,10 +172,6 @@ class ZopeStarter: ...@@ -215,10 +172,6 @@ class ZopeStarter:
if self.cfg.trace is not None: if self.cfg.trace is not None:
self.cfg.trace() self.cfg.trace()
def flushStartupHandlerBuffer(self):
logger = logging.getLogger('event')
self.startup_handler.flushBufferTo(logger)
def startZope(self): def startZope(self):
# Import Zope # Import Zope
import Zope import Zope
...@@ -277,6 +230,65 @@ class ZopeStarter: ...@@ -277,6 +230,65 @@ class ZopeStarter:
pass 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(): def check_python_version():
# check for Python version # check for Python version
python_version = sys.version.split()[0] python_version = sys.version.split()[0]
......
...@@ -23,7 +23,7 @@ import unittest ...@@ -23,7 +23,7 @@ import unittest
import ZConfig import ZConfig
import Zope.Startup import Zope.Startup
from Zope.Startup import handlers from Zope.Startup import handlers
from Zope.Startup import ZopeStarter from Zope.Startup import ZopeStarter, UnixZopeStarter
from App.config import getConfiguration, setConfiguration from App.config import getConfiguration, setConfiguration
...@@ -118,8 +118,8 @@ class ZopeStarterTestCase(unittest.TestCase): ...@@ -118,8 +118,8 @@ class ZopeStarterTestCase(unittest.TestCase):
level blather level blather
</logfile> </logfile>
</eventlog>""") </eventlog>""")
starter = ZopeStarter(conf) starter = UnixZopeStarter(conf)
starter.setupStartupHandler() starter.setupInitialLogging()
# startup handler should take on the level of the event log handler # startup handler should take on the level of the event log handler
# with the lowest level # with the lowest level
...@@ -139,8 +139,8 @@ class ZopeStarterTestCase(unittest.TestCase): ...@@ -139,8 +139,8 @@ class ZopeStarterTestCase(unittest.TestCase):
level info level info
</logfile> </logfile>
</eventlog>""") </eventlog>""")
starter = ZopeStarter(conf) starter = UnixZopeStarter(conf)
starter.setupStartupHandler() starter.setupInitialLogging()
self.failIfEqual(starter.startup_handler.stream, sys.stderr) self.failIfEqual(starter.startup_handler.stream, sys.stderr)
def testSetupZServerThreads(self): def testSetupZServerThreads(self):
...@@ -267,14 +267,12 @@ class ZopeStarterTestCase(unittest.TestCase): ...@@ -267,14 +267,12 @@ class ZopeStarterTestCase(unittest.TestCase):
</logger> </logger>
""") """)
try: try:
starter = ZopeStarter(conf) starter = UnixZopeStarter(conf)
starter.setupStartupHandler() starter.setupInitialLogging()
starter.info('hello') starter.info('hello')
starter.removeStartupHandler() starter.setupFinalLogging()
starter.setupConfiguredLoggers()
logger = logging.getLogger() logger = logging.getLogger()
self.assertEqual(logger.level, logging.INFO) self.assertEqual(logger.level, logging.INFO)
starter.flushStartupHandlerBuffer()
l = open(os.path.join(TEMPNAME, 'event.log')).read() l = open(os.path.join(TEMPNAME, 'event.log')).read()
self.failUnless(l.find('hello') > -1) self.failUnless(l.find('hello') > -1)
self.failUnless(os.path.exists(os.path.join(TEMPNAME, 'Z2.log'))) 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