[Zope3-checkins] CVS: Zope/lib/python/zLOG - EventLogger.py:1.13
datatypes.py:1.17
Fred L. Drake, Jr.
fred at zope.com
Fri Apr 9 17:22:28 EDT 2004
Update of /cvs-repository/Zope/lib/python/zLOG
In directory cvs.zope.org:/tmp/cvs-serv28631/zLOG
Modified Files:
EventLogger.py datatypes.py
Log Message:
Change the mapping from zLOG to the logging package:
- don't dump everything into the "event" logger;
use the subsystem argument to zLOG.LOG to get a specific logger
- move some comments into docstrings
=== Zope/lib/python/zLOG/EventLogger.py 1.12 => 1.13 ===
--- Zope/lib/python/zLOG/EventLogger.py:1.12 Thu Apr 8 16:28:55 2004
+++ Zope/lib/python/zLOG/EventLogger.py Fri Apr 9 17:21:57 2004
@@ -19,8 +19,10 @@
__version__='$Revision$'[11:-2]
-import os, time
import logging
+import os
+import time
+
from BaseLogger import BaseLogger
from ZConfig.components.logger import loghandler
from logging import StreamHandler, Formatter
@@ -31,33 +33,18 @@
logging.addLevelName("BLATHER", CUSTOM_BLATHER)
logging.addLevelName("TRACE", CUSTOM_TRACE)
-class EventLogger(BaseLogger):
-
- # Get our logger object:
- logger = logging.getLogger('event')
- if not logger.handlers:
- # Add a null handler to prevent warnings about loggers with no
- # handlers:
- logger.addHandler(loghandler.NullHandler())
-
- def log(self, subsystem, severity, summary, detail, error):
-
- level = (zlog_to_pep282_severity_cache_get(severity) or
- zlog_to_pep282_severity(severity))
-
- msg = "%s %s %s" % (
- severity_string_cache_get(severity) or severity_string(severity),
- subsystem,
- summary)
- if detail:
- msg = "%s\n%s" % (msg, detail)
+def log_write(subsystem, severity, summary, detail, error):
+ level = (zlog_to_pep282_severity_cache_get(severity) or
+ zlog_to_pep282_severity(severity))
+
+ msg = summary
+ if detail:
+ msg = "%s\n%s" % (msg, detail)
- self.logger.log(level, msg, exc_info=(error is not None))
+ logger = logging.getLogger(subsystem)
+ logger.log(level, msg, exc_info=(error is not None))
-event_logger = EventLogger()
-
-log_write = event_logger.log
def severity_string(severity, mapping={
-300: 'TRACE',
@@ -72,10 +59,6 @@
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):
"""
@@ -114,14 +97,21 @@
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."""
return ("%4.4d-%2.2d-%2.2dT%2.2d:%2.2d:%2.2d"
% time.localtime()[:6])
+
def get_env_severity_info():
- # EVENT_LOG_SEVERITY is the preferred envvar, but we accept
- # STUPID_LOG_SEVERITY also
+ """Return the severity setting based on the environment.
+
+ The value returned is a zLOG severity, not a logging package severity.
+
+ 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:
@@ -130,6 +120,7 @@
severity = 0 # INFO
return severity
+
def get_env_syslog_info():
eget = os.environ.get
addr = None
@@ -145,30 +136,31 @@
else:
return (facility, path)
+
def get_env_file_info():
+ """Return the path of the log file to write to based on the
+ environment.
+
+ EVENT_LOG_FILE is the preferred envvar, but we accept
+ STUPID_LOG_FILE also.
+ """
eget = os.environ.get
- # EVENT_LOG_FILE is the preferred envvar, but we accept
- # STUPID_LOG_FILE also
- path = eget('EVENT_LOG_FILE')
- if path is None:
- path = eget('STUPID_LOG_FILE')
- if path is None:
- dest = None
- else:
- dest = path
- return dest
+ return eget('EVENT_LOG_FILE') or eget('STUPID_LOG_FILE')
+
formatters = {
- 'file': Formatter(fmt='------\n%(asctime)s %(message)s',
- datefmt='%Y-%m-%dT%H:%M:%S'),
- 'syslog': Formatter(fmt='%(message)s'),
+ 'file': Formatter(fmt=('------\n%(asctime)s %(levelname)s %(name)s'
+ ' %(message)s'),
+ datefmt='%Y-%m-%dT%H:%M:%S'),
+ 'syslog': Formatter(fmt='%(levelname)s %(name)s %(message)s'),
}
def initialize_from_environment():
""" Reinitialize the event logger from the environment """
# clear the current handlers from the event logger
- for h in event_logger.logger.handlers[:]:
- event_logger.logger.removeHandler(h)
+ logger = logging.getLogger()
+ for h in logger.handlers[:]:
+ logger.removeHandler(h)
handlers = []
@@ -197,9 +189,7 @@
severity = get_env_severity_info()
severity = zlog_to_pep282_severity(severity)
- event_logger.logger.setLevel(severity)
+ logger.setLevel(severity)
for handler in handlers:
- event_logger.logger.addHandler(handler)
-
- event_logger.logger.propagate = 0
+ logger.addHandler(handler)
=== Zope/lib/python/zLOG/datatypes.py 1.16 => 1.17 ===
--- Zope/lib/python/zLOG/datatypes.py:1.16 Tue Jan 6 11:41:04 2004
+++ Zope/lib/python/zLOG/datatypes.py Fri Apr 9 17:21:57 2004
@@ -44,7 +44,6 @@
for handler in logger.handlers:
if hasattr(handler, "reopen"):
handler.reopen()
- EventLogger.event_logger.logger = self()
def startup(self):
zLOG.set_initializer(self.initialize)
More information about the Zope3-Checkins
mailing list