Commit 2409f710 authored by Fred Drake's avatar Fred Drake

- fix up the last piece that dealt with the old zLOG cruft; this is more

  loosely coupled and re-uses the shared logging configuration more
- replace uses of zLOG with logging
parent a3445b83
...@@ -13,65 +13,73 @@ ...@@ -13,65 +13,73 @@
""" """
Zope signal handlers for clean shutdown, restart and log rotation. Zope signal handlers for clean shutdown, restart and log rotation.
$Id: Signals.py,v 1.2 2003/11/12 20:42:22 chrism Exp $ $Id: Signals.py,v 1.3 2004/04/13 19:02:25 fdrake Exp $
""" """
__version__='$Revision: 1.2 $'[11:-2] __version__='$Revision: 1.3 $'[11:-2]
from SignalHandler import SignalHandler import logging
import zLOG
import sys import sys
import Lifetime import Lifetime
from SignalHandler import SignalHandler
logger = logging.getLogger("Z2")
def shutdownFastHandler(): def shutdownFastHandler():
"""Shutdown cleanly on SIGTERM. This is registered first, """Shutdown cleanly on SIGTERM. This is registered first,
so it should be called after all other handlers.""" so it should be called after all other handlers."""
zLOG.LOG('Z2', zLOG.INFO , "Shutting down fast") logger.info("Shutting down fast")
Lifetime.shutdown(0,fast=1) Lifetime.shutdown(0,fast=1)
def shutdownHandler(): def shutdownHandler():
"""Shutdown cleanly on SIGINT. This is registered first, """Shutdown cleanly on SIGINT. This is registered first,
so it should be called after all other handlers.""" so it should be called after all other handlers."""
zLOG.LOG('Z2', zLOG.INFO , "Shutting down") logger.info("Shutting down")
sys.exit(0) sys.exit(0)
def restartHandler(): def restartHandler():
"""Restart cleanly on SIGHUP. This is registered first, so it """Restart cleanly on SIGHUP. This is registered first, so it
should be called after all other SIGHUP handlers.""" should be called after all other SIGHUP handlers."""
zLOG.LOG('Z2', zLOG.INFO , "Restarting") logger.info("Restarting")
Lifetime.shutdown(1) Lifetime.shutdown(1)
def logfileReopenHandler(): class LogfileReopenHandler:
"""Reopen log files on SIGUSR2. This is registered first, so it """Reopen log files on SIGUSR2.
should be called after all other SIGUSR2 handlers."""
from zLOG.EventLogger import event_logger This is registered first, so it should be called after all other
from ZServer.AccessLogger import access_logger SIGUSR2 handlers.
from ZServer.DebugLogger import debug_logger """
for logger in (event_logger, access_logger, debug_logger): def __init__(self, loggers):
logger.reopen() self.loggers = [log for log in loggers if log is not None]
zLOG.LOG('Z2', zLOG.INFO, "Log files reopened successfully")
def __call__(self):
for log in self.loggers:
log.reopen()
logger.info("Log files reopened successfully")
def packHandler(): def packHandler():
""" Packs the main database. Not safe to call under a signal """ Packs the main database. Not safe to call under a signal
handler, because it blocks the main thread """ handler, because it blocks the main thread """
zLOG.LOG('Z2', zLOG.INFO, 'Packing main ZODB database') logger.info('Packing main ZODB database')
import Globals import Globals
try: try:
db = Globals.opened[0] db = Globals.opened[0]
db.pack() db.pack()
zLOG.LOG('Z2', zLOG.INFO, logger.info('Database packing launched or completed successfully')
'Database packing launched or completed successfully')
except: except:
zLOG.LOG('Z2', zLOG.INFO, logger.exception('Call to pack failed!')
'Call to pack failed!', error=sys.exc_info())
def registerZopeSignals(): def registerZopeSignals(loggers):
import signal import signal
SignalHandler.registerHandler(signal.SIGTERM, shutdownFastHandler) SignalHandler.registerHandler(signal.SIGTERM, shutdownFastHandler)
SignalHandler.registerHandler(signal.SIGINT, shutdownHandler) SignalHandler.registerHandler(signal.SIGINT, shutdownHandler)
SignalHandler.registerHandler(signal.SIGHUP, restartHandler) SignalHandler.registerHandler(signal.SIGHUP, restartHandler)
SignalHandler.registerHandler(signal.SIGUSR2, logfileReopenHandler) SignalHandler.registerHandler(signal.SIGUSR2,
LogfileReopenHandler(loggers))
# SIGUSR1 is nominally reserved for pack, but we dont have an # SIGUSR1 is nominally reserved for pack, but we dont have an
# implementation that is stable yet because if the signal handler # implementation that is stable yet because if the signal handler
# fires it will be caught in the main thread and all network operations # fires it will be caught in the main thread and all network operations
......
...@@ -22,10 +22,11 @@ import socket ...@@ -22,10 +22,11 @@ import socket
import ZConfig import ZConfig
logger = logging.getLogger("Zope")
started = False started = False
def start_zope(cfg): def start_zope(cfg):
""" The function called by run.py which starts a Zope appserver """ """The function called by run.py which starts a Zope appserver."""
global started global started
if started: if started:
# dont allow any code to call start_zope twice. # dont allow any code to call start_zope twice.
...@@ -73,28 +74,29 @@ def start_zope(cfg): ...@@ -73,28 +74,29 @@ def start_zope(cfg):
started = False started = False
class ZopeStarter: class ZopeStarter:
""" This is a class which starts a Zope server. Making it a class """This is a class which starts a Zope server.
makes it easier to unit test. """
Making it a class makes it easier to test.
"""
def __init__(self, cfg): def __init__(self, cfg):
self.cfg = cfg self.cfg = cfg
self.event_logger = logging.getLogger() self.event_logger = logging.getLogger()
def info(self, msg): def info(self, msg):
import zLOG logger.info(msg)
zLOG.LOG('Zope', zLOG.INFO, msg)
def panic(self, msg): def panic(self, msg):
import zLOG logger.critical(msg)
zLOG.LOG('Zope', zLOG.PANIC, msg)
def error(self, msg): def error(self, msg):
import zLOG logger.error(msg)
zLOG.LOG('Zope', zLOG.ERROR, msg)
def registerSignals(self): def registerSignals(self):
if os.name == 'posix': if os.name == 'posix':
from Signals import Signals from Signals import Signals
Signals.registerZopeSignals() Signals.registerZopeSignals([self.cfg.eventlog,
self.cfg.access,
self.cfg.trace])
def setupSecurityOptions(self): def setupSecurityOptions(self):
import AccessControl import AccessControl
...@@ -300,7 +302,6 @@ def dropPrivileges(cfg): ...@@ -300,7 +302,6 @@ def dropPrivileges(cfg):
if os.getuid() != 0: if os.getuid() != 0:
return return
import zLOG
import pwd import pwd
effective_user = cfg.effective_user effective_user = cfg.effective_user
...@@ -308,7 +309,7 @@ def dropPrivileges(cfg): ...@@ -308,7 +309,7 @@ def dropPrivileges(cfg):
msg = ('A user was not specified to setuid to; fix this to ' msg = ('A user was not specified to setuid to; fix this to '
'start as root (change the effective-user directive ' 'start as root (change the effective-user directive '
'in zope.conf)') 'in zope.conf)')
zLOG.LOG('Zope', zLOG.PANIC, msg) logger.critical(msg)
raise ZConfig.ConfigurationError(msg) raise ZConfig.ConfigurationError(msg)
try: try:
...@@ -318,7 +319,7 @@ def dropPrivileges(cfg): ...@@ -318,7 +319,7 @@ def dropPrivileges(cfg):
pwrec = pwd.getpwnam(effective_user) pwrec = pwd.getpwnam(effective_user)
except KeyError: except KeyError:
msg = "Can't find username %r" % effective_user msg = "Can't find username %r" % effective_user
zLOG.LOG("Zope", zLOG.ERROR, msg) logger.error(msg)
raise ZConfig.ConfigurationError(msg) raise ZConfig.ConfigurationError(msg)
uid = pwrec[2] uid = pwrec[2]
else: else:
...@@ -326,13 +327,13 @@ def dropPrivileges(cfg): ...@@ -326,13 +327,13 @@ def dropPrivileges(cfg):
pwrec = pwd.getpwuid(uid) pwrec = pwd.getpwuid(uid)
except KeyError: except KeyError:
msg = "Can't find uid %r" % uid msg = "Can't find uid %r" % uid
zLOG.LOG("Zope", zLOG.ERROR, msg) logger.error(msg)
raise ZConfig.ConfigurationError(msg) raise ZConfig.ConfigurationError(msg)
gid = pwrec[3] gid = pwrec[3]
if uid == 0: if uid == 0:
msg = 'Cannot start Zope with the effective user as the root user' msg = 'Cannot start Zope with the effective user as the root user'
zLOG.LOG('Zope', zLOG.INFO, msg) logger.error(msg)
raise ZConfig.ConfigurationError(msg) raise ZConfig.ConfigurationError(msg)
try: try:
...@@ -340,11 +341,8 @@ def dropPrivileges(cfg): ...@@ -340,11 +341,8 @@ def dropPrivileges(cfg):
initgroups.initgroups(effective_user, gid) initgroups.initgroups(effective_user, gid)
os.setgid(gid) os.setgid(gid)
except OSError: except OSError:
zLOG.LOG("Zope", zLOG.INFO, logger.exception('Could not set group id of effective user')
'Could not set group id of effective user',
error=sys.exc_info())
os.setuid(uid) os.setuid(uid)
zLOG.LOG("Zope", zLOG.INFO, logger.info('Set effective user to "%s"' % effective_user)
'Set effective user to "%s"' % effective_user)
return 1 # for unit testing purposes return 1 # for unit testing purposes
...@@ -15,6 +15,8 @@ ...@@ -15,6 +15,8 @@
"""Datatypes for the Zope schema for use with ZConfig.""" """Datatypes for the Zope schema for use with ZConfig."""
import os import os
from ZConfig.components.logger import logger
from ZODB.config import ZODBDatabase from ZODB.config import ZODBDatabase
# generic datatypes # generic datatypes
...@@ -43,7 +45,7 @@ def cgi_environment(section): ...@@ -43,7 +45,7 @@ def cgi_environment(section):
# Datatype for the access and trace logs # Datatype for the access and trace logs
# (the loghandler datatypes come from the zLOG package) # (the loghandler datatypes come from the zLOG package)
class LoggerFactory: class LoggerFactory(logger.LoggerFactory):
""" """
A factory used to create loggers while delaying actual logger A factory used to create loggers while delaying actual logger
instance construction. We need to do this because we may want to instance construction. We need to do this because we may want to
...@@ -53,24 +55,9 @@ class LoggerFactory: ...@@ -53,24 +55,9 @@ class LoggerFactory:
object. object.
""" """
def __init__(self, section): def __init__(self, section):
self.name = section.getSectionName() section.name = section.getSectionName()
self.level = section.level section.propagate = False
self.handler_factories = section.handlers logger.LoggerFactory.__init__(self, section)
self.resolved = None
def __call__(self):
if self.resolved is None:
# set the logger up
import logging
logger = logging.getLogger(self.name)
logger.handlers = []
logger.propagate = 0
logger.setLevel(self.level)
for handler_factory in self.handler_factories:
handler = handler_factory()
logger.addHandler(handler)
self.resolved = logger
return self.resolved
# DNS resolver # DNS resolver
......
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