[Zodb-checkins] CVS: Zope/lib/python/zLOG - MinimalLogger.py:1.20 __init__.py:1.11

Fred L. Drake, Jr. fred@zope.com
Thu, 21 Nov 2002 18:17:58 -0500


Update of /cvs-repository/Zope/lib/python/zLOG
In directory cvs.zope.org:/tmp/cvs-serv31747

Modified Files:
	MinimalLogger.py __init__.py 
Log Message:
- zLOG.severity():  Convenience function that converts a value to a
  severity level.  This allows the common names to be used for
  severities, not just integers.  This can make configuration data
  less obscure.

- Refactor initialize():
  get_environment_info() pulls information from the environment
  variables, and this gets passed to initialize_log(), which does
  nothing to determine the source of the configuration data.

- New method initialize_with_config():  Load information not provided
  by the environment from a ZConfig section.  This allows the
  environment to override a config file, and avoids having the
  application deal with the specific information needed to configure
  logging.


=== Zope/lib/python/zLOG/MinimalLogger.py 1.19 => 1.20 ===
--- Zope/lib/python/zLOG/MinimalLogger.py:1.19	Mon Nov  4 16:38:43 2002
+++ Zope/lib/python/zLOG/MinimalLogger.py	Thu Nov 21 18:17:57 2002
@@ -8,12 +8,13 @@
 # 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.
 #
 ##############################################################################
 __version__='$Revision$'[11:-2]
 
 import os, sys, time
+import zLOG
 
 try:
     import textwrap
@@ -53,7 +54,39 @@
         self.initialize()
 
     def initialize(self):
+        path, severity = self.get_environment_info()
+        self.initialize_log(path, severity)
+
+    def initialize_with_config(self, config):
+        """Initialize logging with information from ZConfig."""
+        path, severity = self.get_environment_info()
+
+        if config is not None:
+            loginfo = config.getSection("log")
+            if loginfo is not None:
+                if path is None:
+                    path = loginfo.get("path")
+                if severity is None:
+                    severity = loginfo.get("level")
+
+        self.initialize_log(path, severity)
+
+    def initialize_log(self, path, severity):
         global _log_level
+
+        if path is None:
+            _set_log_dest(None)
+        elif path:
+            _set_log_dest(open(path, 'a'))
+        else:
+            _set_log_dest(sys.stderr)
+
+        if severity:
+            _log_level = zLOG.severity(severity)
+        else:
+            _log_level = 0 # INFO
+
+    def get_environment_info(self):
         eget = os.environ.get
 
         # EVENT_LOG_FILE is the preferred envvar, but we accept
@@ -61,21 +94,14 @@
         path = eget('EVENT_LOG_FILE')
         if path is None:
             path = eget('STUPID_LOG_FILE')
-        if path is None:
-            _set_log_dest(None)
-        else:
-            if path:
-                _set_log_dest(open(path, 'a'))
-            else:
-                _set_log_dest(sys.stderr)
 
         # 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:
-            _log_level = int(severity)
-        else:
-            _log_level = 0 # INFO
+        severity = eget('EVENT_LOG_SEVERITY')
+        if severity is None:
+            severity = eget('STUPID_LOG_SEVERITY')
+
+        return path, severity
 
     def log(self, subsystem, severity, summary, detail, error):
         if _log_dest is None or severity < _log_level:
@@ -112,3 +138,4 @@
 _log = stupid_log_write()
 log_write = _log.log
 initialize = _log.initialize
+initialize_with_config = _log.initialize_with_config


=== Zope/lib/python/zLOG/__init__.py 1.10 => 1.11 ===
--- Zope/lib/python/zLOG/__init__.py:1.10	Tue Nov 12 13:17:11 2002
+++ Zope/lib/python/zLOG/__init__.py	Thu Nov 21 18:17:57 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.
 #
 ##############################################################################
 
@@ -89,7 +89,7 @@
 __version__='$Revision$'[11:-2]
 
 from MinimalLogger import log_write, log_time, severity_string, \
-     _set_log_dest, initialize
+     _set_log_dest, initialize, initialize_with_config
 from traceback import format_exception
 
 # Standard severities
@@ -101,6 +101,30 @@
 WARNING =  100
 ERROR   =  200
 PANIC   =  300
+
+_severity_names = {"trace":   -300,
+                   "debug":   -200,
+                   "blather": -100,
+                   "info":       0,
+                   "problem":  100,
+                   "warning":  100,
+                   "error":    200,
+                   "panic":    300,
+                   }
+
+def severity(value):
+    """Return the severity level associated with a value.
+
+    The value may be an integer, the repr of an integer, or the name
+    of a well-known severity value (case-insensitive).
+    """
+    try:
+        return int(value)
+    except ValueError:
+        try:
+            return _severity_names[value.lower()]
+        except KeyError:
+            raise ValueError("unknown severity value: " + repr(value))
 
 def LOG(subsystem, severity, summary, detail='', error=None, reraise=None):
     """Log some information