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