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

Fred L. Drake, Jr. fred@zope.com
Tue, 17 Dec 2002 18:53:05 -0500


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

Modified Files:
      Tag: ZODB3-fast-restart-branch
	EventLogger.py 
Log Message:
Add a way to call on the stock configurability of the logging package.

=== ZODB3/zLOG/EventLogger.py 1.4.2.1 => 1.4.2.2 ===
--- ZODB3/zLOG/EventLogger.py:1.4.2.1	Tue Dec 17 14:32:40 2002
+++ ZODB3/zLOG/EventLogger.py	Tue Dec 17 18:53:05 2002
@@ -19,15 +19,18 @@
 
 __version__='$Revision$'[11:-2]
 
-import os, time
+import logging
+import os
+import time
+
 try:
     import textwrap
 except ImportError:
     textwrap = None
-import logging
+
 from BaseLogger import BaseLogger
 from LogHandlers import FileHandler, NullHandler, SysLogHandler
-from logging import StreamHandler, Formatter
+
 
 class EventLogger(BaseLogger):
     logger = logging.getLogger('event')
@@ -146,45 +149,51 @@
         dest = path
     return dest
 
+def get_env_logging_config():
+    # If ZLOG_CONFIG_FILE is set, use that as a configuration file.
+    return os.environ.get("ZLOG_CONFIG_FILE")
+
 formatters = {
-    'file':    Formatter(fmt='------\n%(asctime)s %(message)s',
-                         datefmt='%Y-%m-%dT%H:%M:%S'),
-    'syslog':  Formatter(fmt='%(message)s'),
+    'file':    logging.Formatter(fmt='------\n%(asctime)s %(message)s',
+                                 datefmt='%Y-%m-%dT%H:%M:%S'),
+    'syslog':  logging.Formatter(fmt='%(message)s'),
     }
 
 def initialize_from_environment():
     """ Reinitialize the event logger from the environment """
     # clear the current handlers from the event logger
-    event_logger.logger.handlers = []
+    del event_logger.logger.handlers[:]
 
-    handlers = []
+    # set up from logging config file
+    configfile = get_env_logging_config()
+    if configfile:
+        import logging.config
+        logging.config.fileConfig(configfile)
 
     # 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)
+        event_logger.logger.addHandler(handler)
 
     # set up file handler if necessary
     filedest = get_env_file_info()
     if filedest:
         handler = FileHandler(filedest)
         handler.setFormatter(formatters['file'])
-        handlers.append(handler)
+        event_logger.logger.addHandler(handler)
     elif filedest == '':
         # if dest is an empty string, log to standard error
-        handler = StreamHandler()
+        handler = logging.StreamHandler()
         handler.setFormatter(formatters['file'])
-        handlers.append(handler)
-    else:
+        event_logger.logger.addHandler(handler)
+
+    if not event_logger.logger.handlers:
         # log to nowhere, but install a 'null' handler in order to
         # prevent error messages from emanating due to a missing handler
-        handlers.append(NullHandler())
+        event_logger.logger.addHandler(NullHandler())
 
     severity = get_env_severity_info()
     severity = zlog_to_pep282_severity(severity)
     event_logger.logger.setLevel(severity)
-
-    for handler in handlers:
-        event_logger.logger.addHandler(handler)