[Zodb-checkins] CVS: Zope/lib/python/zLOG - EventLogger.py:1.1.2.4 __init__.py:1.8.4.4 FormatException.py:NONE
Chris McDonough
chrism@zope.com
Sun, 24 Nov 2002 19:19:53 -0500
Update of /cvs-repository/Zope/lib/python/zLOG
In directory cvs.zope.org:/tmp/cvs-serv21586
Modified Files:
Tag: chrism-install-branch
EventLogger.py __init__.py
Removed Files:
Tag: chrism-install-branch
FormatException.py
Log Message:
Merge with HEAD.
=== Zope/lib/python/zLOG/EventLogger.py 1.1.2.3 => 1.1.2.4 ===
--- Zope/lib/python/zLOG/EventLogger.py:1.1.2.3 Sun Nov 3 18:31:28 2002
+++ Zope/lib/python/zLOG/EventLogger.py Sun Nov 24 19:19:52 2002
@@ -24,78 +24,41 @@
import textwrap
except ImportError:
textwrap = None
-from FormatException import format_exception
import logging
from BaseLogger import BaseLogger
+from LogHandlers import FileHandler, NullHandler, SysLogHandler
+from logging import StreamHandler, Formatter
class EventLogger(BaseLogger):
logger = logging.getLogger('event')
+ logger.addHandler(NullHandler())
+ log_format = '%(sev)s %(subsys)s %(summary)s%(detail)s'
def log(self, subsystem, severity, summary, detail, error):
- kw = {}
- sep = '%s\n' % ('-' * 8)
if error:
- kw.update({'exc_info':1})
- if detail:
- fmt = '%(time)s %(sev)s %(subsys)s %(summary)s\n%(detail)s'
- msg = fmt % {
- 'time': log_time(),
- 'sev' : severity_string(severity),
- 'subsys' : subsystem,
- 'summary': summary,
- 'detail' : detail,
- }
- if not textwrap or len(msg) < 80:
- msg = sep + msg
- else:
- msg = sep + textwrap.wrap(
- msg, width=79, subsequent_indent=" "*20,
- break_long_words=0)
+ kw = {'exc_info':1}
else:
- fmt = '%(time)s %(sev)s %(subsys)s %(summary)s'
- msg = fmt % {
- 'time': log_time(),
- 'sev' : severity_string(severity),
- 'subsys' : subsystem,
- 'summary': summary,
- }
- if not textwrap or len(msg) < 80:
- msg = sep + msg
- else:
- msg = sep + textwrap.wrap(msg, width=79,
- subsequent_indent=" "*20,
- break_long_words=0)
- log_method = self.severity_to_method(severity)
- apply(log_method, [msg], kw)
-
- def severity_to_method(self, sev):
- """
- We map zLOG severities to PEP282 severities here.
- This is how they are mapped:
-
- zLOG severity PEP282 severity
- ------------- ---------------
- PANIC (300) critical (50)
- ERROR (200), PROBLEM (100) error (40)
- INFO (0) warn (30)
- BLATHER (-100) info (20)
- DEBUG (-200), TRACE (-300) debug (10)
- """
- # PANIC
- if sev >= 300:
- return self.logger.critical
- # ERROR, PROBLEM
- if sev >= 100:
- return self.logger.error
- # INFO
- if sev >= 0:
- return self.logger.warn
- # BLATHER
- if sev >= -100:
- return self.logger.info
- # TRACE, DEBUG
+ kw = {}
+
+ if detail:
+ detail = '\n' + detail
else:
- return self.logger.debug
+ detail = ''
+
+ msg = self.log_format % {
+ 'sev' : severity_string(severity),
+ 'subsys' : subsystem,
+ 'summary': summary,
+ 'detail' : detail,
+ }
+
+ if textwrap and len(msg) > 80:
+ msg = '\n'.join(textwrap.wrap(
+ msg, width=79, subsequent_indent=" "*20,
+ break_long_words=0))
+
+ severity = zlog_to_pep282_severity(severity)
+ self.logger.log(severity, msg, **kw)
EventLogger = EventLogger()
@@ -114,12 +77,63 @@
s = mapping.get(int(severity), '')
return "%s(%s)" % (s, severity)
+def zlog_to_pep282_severity(zlog_severity):
+ """
+ We map zLOG severities to PEP282 severities here.
+ This is how they are mapped:
+
+ zLOG severity PEP282 severity
+ ------------- ---------------
+ PANIC (300) critical (50)
+ ERROR (200), PROBLEM (100) error (40)
+ INFO (0) warn (30)
+ BLATHER (-100) info (20)
+ DEBUG (-200), TRACE (-300) debug (10)
+ """
+ sev = zlog_severity
+ if sev >= 300:
+ return logging.CRITICAL
+ if sev >= 100:
+ return logging.ERROR
+ if sev >= 0:
+ return logging.WARN
+ if sev >= -100:
+ 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"
% time.localtime()[:6])
-def reinitialize():
+def get_env_severity_info():
+ # EVENT_LOG_SEVERITY is the preferred envvar, but we accept
+ # STUPID_LOG_SEVERITY also
+ eget = os.environ.get
+ severity = eget('EVENT_LOG_SEVERITY') or eget('STUPID_LOG_SEVERITY')
+ if severity:
+ severity = int(severity)
+ else:
+ severity = 0 # INFO
+ return severity
+
+def get_env_syslog_info():
+ eget = os.environ.get
+ addr = None
+ port = None
+ path = eget('ZSYSLOG')
+ facility = eget('ZSYSLOG_FACILITY', 'user')
+ server = eget('ZSYSLOG_SERVER')
+ if server:
+ addr, port = server.split(':')
+ port = int(port)
+ if addr:
+ return (facility, (addr, port))
+ else:
+ return (facility, path)
+
+def get_env_file_info():
eget = os.environ.get
# EVENT_LOG_FILE is the preferred envvar, but we accept
# STUPID_LOG_FILE also
@@ -130,17 +144,48 @@
dest = None
else:
dest = path
- # EVENT_LOG_SEVERITY is the preferred envvar, but we accept
- # STUPID_LOG_SEVERITY also
- severity = eget('EVENT_LOG_SEVERITY') or eget('STUPID_LOG_SEVERITY')
- if severity:
- severity = int(severity)
- else:
- severity = 0 # INFO
- if dest:
- handler = logging.FileHandler(dest)
- else:
- # if dest is an empty string, log to standard error
- handler = logging.StreamHandler()
+ 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 = []
- EventLogger.logger.addHandler(handler, severity)
+
+ handlers = []
+
+ # set up syslog handler if necessary
+ facility, syslogdest = get_env_syslog_info()
+ if syslogdest:
+ handler = SysLogHandler(syslogdest, facility)
+ 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(formatters['file'])
+ handlers.append(handler)
+ elif filedest == '':
+ # if dest is an empty string, log to standard error
+ handler = StreamHandler()
+ handler.setFormatter(formatters['file'])
+ handlers.append(handler)
+ else:
+ # log to nowhere, but install a 'null' handler in order to
+ # prevent error messages from emanating due to a missing handler
+ handlers.append(NullHandler())
+
+ severity = get_env_severity_info()
+ severity = zlog_to_pep282_severity(severity)
+ EventLogger.logger.setLevel(severity)
+
+ for handler in handlers:
+ EventLogger.logger.addHandler(handler)
+
=== Zope/lib/python/zLOG/__init__.py 1.8.4.3 => 1.8.4.4 ===
--- Zope/lib/python/zLOG/__init__.py:1.8.4.3 Sun Nov 3 18:31:28 2002
+++ Zope/lib/python/zLOG/__init__.py Sun Nov 24 19:19:52 2002
@@ -8,7 +8,7 @@
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
-# FOR A PARTICULAR PURPOSE
+# FOR A PARTICULAR PURPOSE.
#
##############################################################################
@@ -46,7 +46,7 @@
PANIC=300 -- We're dead!
-Also, looging facilities will normally ignore negative severities.
+Also, logging facilities will normally ignore negative severities.
To plug in a log handler, simply replace the log_write function
with a callable object that takes 5 arguments:
@@ -88,9 +88,9 @@
"""
__version__='$Revision$'[11:-2]
-from EventLogger import log_time
-from EventLogger import EventLogger
-from FormatException import format_exception
+from EventLogger import log_write, log_time, severity_string, \
+ initialize_from_environment as initialize
+from traceback import format_exception
# Standard severities
TRACE = -300
@@ -127,19 +127,19 @@
error is reraised.
"""
- EventLogger.log(subsystem, severity, summary, detail, error)
+ log_write(subsystem, severity, summary, detail, error)
if reraise and error:
raise error[0], error[1], error[2]
_subsystems=[]
def register_subsystem(subsystem):
- """Register a subsystem name. This is only kept for backwards
- compatibility. It is not actually used.
+ """Register a subsystem name
+
+ A logging facility might replace this function to collect information about
+ subsystems used in an application.
"""
_subsystems.append(subsystem)
# Most apps interested in logging only want the names below.
__all__ = ['LOG', 'TRACE', 'DEBUG', 'BLATHER', 'INFO', 'PROBLEM',
'WARNING', 'ERROR', 'PANIC', 'log_time']
-
-
=== Removed File Zope/lib/python/zLOG/FormatException.py ===