[Zodb-checkins] CVS: ZODB3/zLOG - EventLogger.py:1.5

Guido van Rossum guido@python.org
Tue, 7 Jan 2003 13:35:15 -0500


Update of /cvs-repository/ZODB3/zLOG
In directory cvs.zope.org:/tmp/cvs-serv11955

Modified Files:
	EventLogger.py 
Log Message:
Several changes, mostly to speed up logging when :

- Cache the translations from severity to level and string in dicts,
  so these are normally done with a single dict.get() call rather than
  a Python function call.

- Extract the PEP 282 level first, and check it against the effective
  level of the logger, so we can take an early exit if the logger
  won't handle the event.

- Got rid of textwrap.  It slows things down, can frustrate grepping
  programs, and wasn't effective unless you were using Python 2.3
  anyway.

- Got rid of the addHandler(NullHandler()) (seems like superstition).

- Got rid of the log_format class variable (YAGNI).


=== ZODB3/zLOG/EventLogger.py 1.4 => 1.5 ===
--- ZODB3/zLOG/EventLogger.py:1.4	Tue Dec 17 13:02:30 2002
+++ ZODB3/zLOG/EventLogger.py	Tue Jan  7 13:35:12 2003
@@ -19,46 +19,36 @@
 
 __version__='$Revision$'[11:-2]
 
-import os, time
-try:
-    import textwrap
-except ImportError:
-    textwrap = None
+import os, sys, time
 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):
-        if error:
-            kw = {'exc_info':1}
-        else:
-            kw = {}
+
+        level = (zlog_to_pep282_severity_cache_get(severity) or
+                 zlog_to_pep282_severity(severity))
+
+        # Try an early exit if the logger is disabled for this level.
+        # (XXX This inlines logger.isEnabledFor(level).)
+        if (self.logger.manager.disable >= level or
+            level < self.logger.getEffectiveLevel()):
+            return
+
+        msg = "%s %s %s" % (
+            severity_string_cache_get(severity) or severity_string(severity),
+            subsystem,
+            summary)
 
         if detail:
-            detail = '\n' + detail
-        else:
-            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))
+            msg = "%s\n%s" % (msg, detail)
 
-        severity = zlog_to_pep282_severity(severity)
-        self.logger.log(severity, msg, **kw)
+        self.logger.log(level, msg, exc_info=(error is not None))
 
 event_logger = EventLogger()
 
@@ -77,6 +67,11 @@
     s = mapping.get(int(severity), '')
     return "%s(%s)" % (s, severity)
 
+severity_string_cache = {}
+for _sev in range(-300, 301, 100):
+    severity_string_cache[_sev] = severity_string(_sev)
+severity_string_cache_get = severity_string_cache.get
+
 def zlog_to_pep282_severity(zlog_severity):
     """
     We map zLOG severities to PEP282 severities here.
@@ -101,6 +96,11 @@
         return logging.INFO
     else:
         return logging.DEBUG
+
+zlog_to_pep282_severity_cache = {}
+for _sev in range(-300, 301, 100):
+    zlog_to_pep282_severity_cache[_sev] = zlog_to_pep282_severity(_sev)
+zlog_to_pep282_severity_cache_get = zlog_to_pep282_severity_cache.get
 
 def log_time():
     """Return a simple time string without spaces suitable for logging."""