Commit c526d348 authored by Guido van Rossum's avatar Guido van Rossum

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).
parent 67e5c181
...@@ -19,46 +19,36 @@ This uses Vinay Sajip's PEP 282 logging module. ...@@ -19,46 +19,36 @@ This uses Vinay Sajip's PEP 282 logging module.
__version__='$Revision$'[11:-2] __version__='$Revision$'[11:-2]
import os, time import os, sys, time
try:
import textwrap
except ImportError:
textwrap = None
import logging import logging
from BaseLogger import BaseLogger from BaseLogger import BaseLogger
from LogHandlers import FileHandler, NullHandler, SysLogHandler from LogHandlers import FileHandler, NullHandler, SysLogHandler
from logging import StreamHandler, Formatter from logging import StreamHandler, Formatter
class EventLogger(BaseLogger): class EventLogger(BaseLogger):
logger = logging.getLogger('event') 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): def log(self, subsystem, severity, summary, detail, error):
if error:
kw = {'exc_info':1}
else:
kw = {}
if detail: level = (zlog_to_pep282_severity_cache_get(severity) or
detail = '\n' + detail zlog_to_pep282_severity(severity))
else:
detail = ''
msg = self.log_format % { # Try an early exit if the logger is disabled for this level.
'sev' : severity_string(severity), # (XXX This inlines logger.isEnabledFor(level).)
'subsys' : subsystem, if (self.logger.manager.disable >= level or
'summary': summary, level < self.logger.getEffectiveLevel()):
'detail' : detail, return
}
if textwrap and len(msg) > 80: msg = "%s %s %s" % (
msg = '\n'.join(textwrap.wrap( severity_string_cache_get(severity) or severity_string(severity),
msg, width=79, subsequent_indent=" "*20, subsystem,
break_long_words=0)) summary)
severity = zlog_to_pep282_severity(severity) if detail:
self.logger.log(severity, msg, **kw) msg = "%s\n%s" % (msg, detail)
self.logger.log(level, msg, exc_info=(error is not None))
event_logger = EventLogger() event_logger = EventLogger()
...@@ -77,6 +67,11 @@ def severity_string(severity, mapping={ ...@@ -77,6 +67,11 @@ def severity_string(severity, mapping={
s = mapping.get(int(severity), '') s = mapping.get(int(severity), '')
return "%s(%s)" % (s, 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): def zlog_to_pep282_severity(zlog_severity):
""" """
We map zLOG severities to PEP282 severities here. We map zLOG severities to PEP282 severities here.
...@@ -102,6 +97,11 @@ def zlog_to_pep282_severity(zlog_severity): ...@@ -102,6 +97,11 @@ def zlog_to_pep282_severity(zlog_severity):
else: else:
return logging.DEBUG 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(): def log_time():
"""Return a simple time string without spaces suitable for logging.""" """Return a simple time string without spaces suitable for logging."""
return ("%4.4d-%2.2d-%2.2dT%2.2d:%2.2d:%2.2d" return ("%4.4d-%2.2d-%2.2dT%2.2d:%2.2d:%2.2d"
......
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