[Zodb-checkins] CVS: Zope/lib/python/zLOG - AccessLogger.py:1.1.2.3 BaseLogger.py:1.1.2.2 DebugLogger.py:1.1.2.4 EventLogger.py:1.1.2.6 LogHandlers.py:1.1.2.4 __init__.py:1.8.4.5
Chris McDonough
chrism@zope.com
Fri, 3 Jan 2003 01:47:05 -0500
Update of /cvs-repository/Zope/lib/python/zLOG
In directory cvs.zope.org:/tmp/cvs-serv30122
Modified Files:
Tag: chrism-install-branch
AccessLogger.py BaseLogger.py DebugLogger.py EventLogger.py
LogHandlers.py __init__.py
Log Message:
Putting log handlers in zLOG for lack of a better place.
=== Zope/lib/python/zLOG/AccessLogger.py 1.1.2.2 => 1.1.2.3 ===
--- Zope/lib/python/zLOG/AccessLogger.py:1.1.2.2 Sun Nov 3 18:31:28 2002
+++ Zope/lib/python/zLOG/AccessLogger.py Fri Jan 3 01:47:00 2003
@@ -22,8 +22,10 @@
class AccessLogger(BaseLogger):
logger = logging.getLogger('access')
def log(self, message):
+ if not self.logger.handlers: # dont log if we have no handlers
+ return
if message.endswith('\n'):
message = message[:-1]
self.logger.warn(message)
-AccessLogger = AccessLogger()
+access_logger = AccessLogger()
=== Zope/lib/python/zLOG/BaseLogger.py 1.1.2.1 => 1.1.2.2 ===
--- Zope/lib/python/zLOG/BaseLogger.py:1.1.2.1 Sun Nov 3 18:31:28 2002
+++ Zope/lib/python/zLOG/BaseLogger.py Fri Jan 3 01:47:00 2003
@@ -21,4 +21,4 @@
for handler in self.logger.handlers:
if hasattr(handler, 'reopen') and callable(handler.reopen):
handler.reopen()
-
+
=== Zope/lib/python/zLOG/DebugLogger.py 1.1.2.3 => 1.1.2.4 ===
--- Zope/lib/python/zLOG/DebugLogger.py:1.1.2.3 Sun Nov 3 19:26:26 2002
+++ Zope/lib/python/zLOG/DebugLogger.py Fri Jan 3 01:47:00 2003
@@ -40,13 +40,15 @@
class DebugLogger(BaseLogger):
logger = logging.getLogger('trace')
def log(self, code, request_id, data=''):
+ if not self.logger.handlers:
+ return
t=time.strftime('%Y-%m-%dT%H:%M:%S', time.localtime(time.time()))
message = '%s %s %s %s' % (code, request_id, t, data)
self.logger.warn(message)
#DebugLogger.log('U', '000000000', 'System startup')
-DebugLogger = DebugLogger()
-log = DebugLogger.log
-reopen = DebugLogger.reopen
+debug_logger = DebugLogger()
+log = debug_logger.log
+reopen = debug_logger.reopen
=== Zope/lib/python/zLOG/EventLogger.py 1.1.2.5 => 1.1.2.6 ===
--- Zope/lib/python/zLOG/EventLogger.py:1.1.2.5 Mon Nov 25 02:41:56 2002
+++ Zope/lib/python/zLOG/EventLogger.py Fri Jan 3 01:47:00 2003
@@ -19,7 +19,7 @@
__version__='$Revision$'[11:-2]
-import os, sys, time
+import os, time
try:
import textwrap
except ImportError:
@@ -29,23 +29,13 @@
from LogHandlers import FileHandler, NullHandler, SysLogHandler
from logging import StreamHandler, Formatter
-def get_formatter(type):
- formatters = {
- 'file': Formatter(fmt='------\n%(asctime)s %(message)s',
- datefmt='%Y-%m-%dT%H:%M:%S'),
- 'syslog': Formatter(fmt='%(message)s'),
- }
- return formatters[type]
-
class EventLogger(BaseLogger):
logger = logging.getLogger('event')
- handler = NullHandler()
- handler.setFormatter(get_formatter('file'))
- logger.addHandler(handler)
-
log_format = '%(sev)s %(subsys)s %(summary)s%(detail)s'
-
+
def log(self, subsystem, severity, summary, detail, error):
+ if not self.logger.handlers: # dont log if we have no handlers
+ return
if error:
kw = {'exc_info':1}
else:
@@ -71,9 +61,9 @@
severity = zlog_to_pep282_severity(severity)
self.logger.log(severity, msg, **kw)
-EventLogger = EventLogger()
+event_logger = EventLogger()
-log_write = EventLogger.log
+log_write = event_logger.log
def severity_string(severity, mapping={
-300: 'TRACE',
@@ -112,7 +102,7 @@
return logging.INFO
else:
return logging.DEBUG
-
+
def log_time():
"""Return a simple time string without spaces suitable for logging."""
return ("%4.4d-%2.2d-%2.2dT%2.2d:%2.2d:%2.2d"
@@ -157,10 +147,16 @@
dest = path
return dest
+formatters = {
+ 'file': Formatter(fmt='------\n%(asctime)s %(message)s',
+ datefmt='%Y-%m-%dT%H:%M:%S'),
+ 'syslog': Formatter(fmt='%(message)s'),
+ }
+
def initialize_from_environment():
""" Reinitialize the event logger from the environment """
# clear the current handlers from the event logger
- EventLogger.logger.handlers = []
+ event_logger.logger.handlers = []
handlers = []
@@ -168,19 +164,19 @@
facility, syslogdest = get_env_syslog_info()
if syslogdest:
handler = SysLogHandler(syslogdest, facility)
- handler.setFormatter(get_formatter('syslog'))
+ handler.setFormatter(formatters['syslog'])
handlers.append(handler)
# set up file handler if necessary
filedest = get_env_file_info()
if filedest:
handler = FileHandler(filedest)
- handler.setFormatter(get_formatter('file'))
+ handler.setFormatter(formatters['file'])
handlers.append(handler)
elif filedest == '':
# if dest is an empty string, log to standard error
handler = StreamHandler()
- handler.setFormatter(get_formatter('file'))
+ handler.setFormatter(formatters['file'])
handlers.append(handler)
else:
# log to nowhere, but install a 'null' handler in order to
@@ -189,8 +185,7 @@
severity = get_env_severity_info()
severity = zlog_to_pep282_severity(severity)
- EventLogger.logger.setLevel(severity)
+ event_logger.logger.setLevel(severity)
for handler in handlers:
- EventLogger.logger.addHandler(handler)
-
+ event_logger.logger.addHandler(handler)
=== Zope/lib/python/zLOG/LogHandlers.py 1.1.2.3 => 1.1.2.4 ===
--- Zope/lib/python/zLOG/LogHandlers.py:1.1.2.3 Sun Nov 24 19:32:43 2002
+++ Zope/lib/python/zLOG/LogHandlers.py Fri Jan 3 01:47:00 2003
@@ -10,12 +10,23 @@
# FOR A PARTICULAR PURPOSE
#
##############################################################################
-""" Handlers which can plug into a PEP 282 logger """
+"""Handlers which can plug into a PEP 282 logger."""
+
+import sys
from logging import Handler, StreamHandler
-from logging.handlers import SysLogHandler
+from logging.handlers import SysLogHandler, HTTPHandler, NTEventLogHandler, \
+ SMTPHandler
+
+class BaseHandler(Handler):
+ def handleError(self):
+ import traceback
+ ei = sys.exc_info()
+ traceback.print_exception(ei[0], ei[1], ei[2], None, sys.stderr)
+ del ei
+
-class FileHandler(StreamHandler):
+class FileHandler(StreamHandler, BaseHandler):
"""
A file handler which allows for reopening of logs in favor of the
'rollover' features of the standard PEP282 FileHandler.
@@ -32,7 +43,7 @@
self.close()
self.stream = open(self.baseFilename, self.mode)
-class NullHandler(Handler):
+class NullHandler(Handler, BaseHandler):
"""
A null handler. Does nothing.
"""
@@ -42,6 +53,18 @@
def handle(self, record):
pass
+class SysLogHandler(SysLogHandler, BaseHandler):
+ pass
+
+class HTTPHandler(HTTPHandler, BaseHandler):
+ pass
+
+class NTEventLogHandler(NTEventLogHandler, BaseHandler):
+ pass
+
+class SMTPHandler(SMTPHandler, BaseHandler):
+ pass
+
class StartupHandler(Handler):
"""
A handler which outputs messages to a stream but also buffers them until
@@ -71,8 +94,9 @@
for record in self.buffer:
target.handle(record)
self.buffer = []
-
-
-
-
-
+
+# make sure folks know not to remove the imported handlers (they're themselves
+# imported elsewhere)
+
+__all__ = [ FileHandler, NullHandler, StartupHandler, SysLogHandler,
+ HTTPHandler, NTEventLogHandler, SMTPHandler ]
=== Zope/lib/python/zLOG/__init__.py 1.8.4.4 => 1.8.4.5 ===
--- Zope/lib/python/zLOG/__init__.py:1.8.4.4 Sun Nov 24 19:19:52 2002
+++ Zope/lib/python/zLOG/__init__.py Fri Jan 3 01:47:00 2003
@@ -89,7 +89,7 @@
__version__='$Revision$'[11:-2]
from EventLogger import log_write, log_time, severity_string, \
- initialize_from_environment as initialize
+ initialize_from_environment
from traceback import format_exception
# Standard severities
@@ -102,6 +102,14 @@
ERROR = 200
PANIC = 300
+# Flag indicating whether LOG() should call initialize()
+_call_initialize = 1
+
+def initialize():
+ global _call_initialize
+ _call_initialize = 0
+ initialize_from_environment()
+
def LOG(subsystem, severity, summary, detail='', error=None, reraise=None):
"""Log some information
@@ -127,6 +135,8 @@
error is reraised.
"""
+ if _call_initialize:
+ initialize()
log_write(subsystem, severity, summary, detail, error)
if reraise and error:
raise error[0], error[1], error[2]