[Zope3-checkins] CVS: Zope3/src/logging - handlers.py:1.3 config.py:1.3 __init__.py:1.3

Jeremy Hylton jeremy@zope.com
Mon, 10 Mar 2003 17:46:16 -0500


Update of /cvs-repository/Zope3/src/logging
In directory cvs.zope.org:/tmp/cvs-serv19494

Modified Files:
	handlers.py config.py __init__.py 
Log Message:
Sync with Python CVS.


=== Zope3/src/logging/handlers.py 1.2 => 1.3 ===
--- Zope3/src/logging/handlers.py:1.2	Wed Dec 25 09:12:12 2002
+++ Zope3/src/logging/handlers.py	Mon Mar 10 17:46:14 2003
@@ -1,5 +1,3 @@
-#! /usr/bin/env python
-#
 # Copyright 2001-2002 by Vinay Sajip. All Rights Reserved.
 #
 # Permission to use, copy, modify, and distribute this software and its
@@ -15,12 +13,6 @@
 # ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
 # IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
 # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-#
-# For the change history, see README.txt in the distribution.
-#
-# This file is part of the Python logging distribution. See
-# http://www.red-dove.com/python_logging.html
-#
 
 """
 Logging package for Python. Based on PEP 282 and comments thereto in
@@ -108,7 +100,7 @@
         """
         if self.maxBytes > 0:                   # are we rolling over?
             msg = "%s\n" % self.format(record)
-            #print msg
+            self.stream.seek(0, 2)  #due to non-posix-compliant Windows feature
             if self.stream.tell() + len(msg) >= self.maxBytes:
                 self.doRollover()
         logging.FileHandler.emit(self, record)
@@ -153,10 +145,7 @@
         This function allows for partial sends which can happen when the
         network is busy.
         """
-        try:
-            v = sys.version_info
-        except:
-            v = None
+        v = logging._verinfo
         if v and (v[0] >= 2) and (v[1] >= 2):
             self.sock.sendall(s)
         else:
@@ -178,7 +167,7 @@
         slen = struct.pack(">L", len(s))
         return slen + s
 
-    def handleError(self):
+    def handleError(self, record):
         """
         Handle an error during logging.
 
@@ -190,7 +179,7 @@
             self.sock.close()
             self.sock = None        #try to reconnect next time
         else:
-            logging.Handler.handleError(self)
+            logging.Handler.handleError(self, record)
 
     def emit(self, record):
         """
@@ -207,7 +196,7 @@
                 self.sock = self.makeSocket()
             self.send(s)
         except:
-            self.handleError()
+            self.handleError(record)
 
     def close(self):
         """
@@ -360,6 +349,12 @@
         self.facility = facility
         if type(address) == types.StringType:
             self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
+            # syslog may require either DGRAM or STREAM sockets
+            try:
+                self.socket.connect(address)
+            except socket.error:
+                self.socket.close()
+                self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
             self.socket.connect(address)
             self.unixsocket = 1
         else:
@@ -416,7 +411,7 @@
             else:
                 self.socket.sendto(msg, self.address)
         except:
-            self.handleError()
+            self.handleError(record)
 
 class SMTPHandler(logging.Handler):
     """
@@ -439,6 +434,8 @@
             self.mailhost = mailhost
             self.mailport = None
         self.fromaddr = fromaddr
+        if type(toaddrs) == types.StringType:
+            toaddrs = [toaddrs]
         self.toaddrs = toaddrs
         self.subject = subject
 
@@ -472,7 +469,7 @@
             smtp.sendmail(self.fromaddr, self.toaddrs, msg)
             smtp.quit()
         except:
-            self.handleError()
+            self.handleError(record)
 
 class NTEventLogHandler(logging.Handler):
     """
@@ -501,7 +498,7 @@
             self.typemap = {
                 logging.DEBUG   : win32evtlog.EVENTLOG_INFORMATION_TYPE,
                 logging.INFO    : win32evtlog.EVENTLOG_INFORMATION_TYPE,
-                logging.WARN    : win32evtlog.EVENTLOG_WARNING_TYPE,
+                logging.WARNING : win32evtlog.EVENTLOG_WARNING_TYPE,
                 logging.ERROR   : win32evtlog.EVENTLOG_ERROR_TYPE,
                 logging.CRITICAL: win32evtlog.EVENTLOG_ERROR_TYPE,
          }
@@ -536,7 +533,7 @@
         Override this if you want to specify your own types. This version does
         a mapping using the handler's typemap attribute, which is set up in
         __init__() to a dictionary which contains mappings for DEBUG, INFO,
-        WARN, ERROR and CRITICAL. If you are using your own levels you will
+        WARNING, ERROR and CRITICAL. If you are using your own levels you will
         either need to override this method or place a suitable dictionary in
         the handler's typemap attribute.
         """
@@ -557,7 +554,7 @@
                 msg = self.format(record)
                 self._welu.ReportEvent(self.appname, id, cat, type, [msg])
             except:
-                self.handleError()
+                self.handleError(record)
 
     def close(self):
         """
@@ -615,7 +612,7 @@
                 h.send(data)
             h.getreply()    #can't do anything with the result
         except:
-            self.handleError()
+            self.handleError(record)
 
 class BufferingHandler(logging.Handler):
     """


=== Zope3/src/logging/config.py 1.2 => 1.3 ===
--- Zope3/src/logging/config.py:1.2	Wed Dec 25 09:12:12 2002
+++ Zope3/src/logging/config.py	Mon Mar 10 17:46:14 2003
@@ -1,5 +1,3 @@
-#! /usr/bin/env python
-#
 # Copyright 2001-2002 by Vinay Sajip. All Rights Reserved.
 #
 # Permission to use, copy, modify, and distribute this software and its
@@ -15,12 +13,6 @@
 # ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
 # IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
 # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-#
-# For the change history, see README.txt in the distribution.
-#
-# This file is part of the Python logging distribution. See
-# http://www.red-dove.com/python_logging.html
-#
 
 """
 Logging package for Python. Based on PEP 282 and comments thereto in
@@ -48,7 +40,7 @@
 #   _listener holds the server object doing the listening
 _listener = None
 
-def fileConfig(fname,defaults=None):
+def fileConfig(fname, defaults=None):
     """
     Read the logging configuration from a ConfigParser-format file.
 
@@ -259,7 +251,7 @@
         allow_reuse_address = 1
 
         def __init__(self, host='localhost', port=DEFAULT_LOGGING_CONFIG_PORT,
-                handler=None):
+                     handler=None):
             ThreadingTCPServer.__init__(self, (host, port), handler)
             logging._acquireLock()
             self.abort = 0
@@ -279,20 +271,23 @@
                 abort = self.abort
                 logging._releaseLock()
 
-    def serve(rcvr, hdlr):
-        server = rcvr(handler=hdlr)
+    def serve(rcvr, hdlr, port):
+        server = rcvr(port=port, handler=hdlr)
         global _listener
         logging._acquireLock()
         _listener = server
         logging._releaseLock()
         server.serve_until_stopped()
 
-    return threading.Thread(target=serve, args=(ConfigSocketReceiver, ConfigStreamHandler))
+    return threading.Thread(target=serve,
+                            args=(ConfigSocketReceiver,
+                                  ConfigStreamHandler, port))
 
 def stopListening():
     """
     Stop the listening server which was created with a call to listen().
     """
+    global _listener
     if _listener:
         logging._acquireLock()
         _listener.abort = 1


=== Zope3/src/logging/__init__.py 1.2 => 1.3 ===
--- Zope3/src/logging/__init__.py:1.2	Wed Dec 25 09:12:12 2002
+++ Zope3/src/logging/__init__.py	Mon Mar 10 17:46:14 2003
@@ -1,5 +1,3 @@
-#! /usr/bin/env python
-#
 # Copyright 2001-2002 by Vinay Sajip. All Rights Reserved.
 #
 # Permission to use, copy, modify, and distribute this software and its
@@ -15,59 +13,59 @@
 # ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
 # IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
 # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-#
-# For the change history, see README.txt in the distribution.
-#
-# This file is part of the Python logging distribution. See
-# http://www.red-dove.com/python_logging.html
-#
 
 """
 Logging package for Python. Based on PEP 282 and comments thereto in
 comp.lang.python, and influenced by Apache's log4j system.
 
 Should work under Python versions >= 1.5.2, except that source line
-information is not available unless 'inspect' is.
+information is not available unless 'sys._getframe()' is.
 
 Copyright (C) 2001-2002 Vinay Sajip. All Rights Reserved.
 
 To use, simply 'import logging' and log away!
 """
 
-import sys, os, types, time, string, struct, cPickle, cStringIO
+import sys, os, types, time, string, cStringIO
 
 try:
     import thread
     import threading
 except ImportError:
     thread = None
-try:
-    import inspect
-except ImportError:
-    inspect = None
 
 __author__  = "Vinay Sajip <vinay_sajip@red-dove.com>"
 __status__  = "alpha"
-__version__ = "0.4.7"
-__date__    = "15 November 2002"
+__version__ = "0.4.8"
+__date__    = "16 February 2003"
 
 #---------------------------------------------------------------------------
 #   Miscellaneous module data
 #---------------------------------------------------------------------------
 
 #
+# _verinfo is used for when behaviour needs to be adjusted to the version
+# of Python
+#
+
+_verinfo = getattr(sys, "version_info", None)
+
+#
 #_srcfile is used when walking the stack to check when we've got the first
 # caller stack frame.
-#If run as a script, __file__ is not bound.
 #
-if __name__ == "__main__":
-    _srcfile = None
+if string.lower(__file__[-4:]) in ['.pyc', '.pyo']:
+    _srcfile = __file__[:-4] + '.py'
 else:
-    if string.lower(__file__[-4:]) in ['.pyc', '.pyo']:
-        _srcfile = __file__[:-4] + '.py'
-    else:
-        _srcfile = __file__
-    _srcfile = os.path.normcase(_srcfile)
+    _srcfile = __file__
+_srcfile = os.path.normcase(_srcfile)
+
+# _srcfile is only used in conjunction with sys._getframe().
+# To provide compatibility with older versions of Python, set _srcfile
+# to None if _getframe() is not available; this value will prevent
+# findCaller() from being called.
+if not hasattr(sys, "_getframe"):
+    _srcfile = None
 
 #
 #_startTime is used as the base when calculating the relative time of events
@@ -80,7 +78,6 @@
 #
 raiseExceptions = 1
 
-
 #---------------------------------------------------------------------------
 #   Level related stuff
 #---------------------------------------------------------------------------
@@ -94,7 +91,8 @@
 CRITICAL = 50
 FATAL = CRITICAL
 ERROR = 40
-WARN = 30
+WARNING = 30
+WARN = WARNING
 INFO = 20
 DEBUG = 10
 NOTSET = 0
@@ -102,13 +100,14 @@
 _levelNames = {
     CRITICAL : 'CRITICAL',
     ERROR : 'ERROR',
-    WARN : 'WARN',
+    WARNING : 'WARNING',
     INFO : 'INFO',
     DEBUG : 'DEBUG',
     NOTSET : 'NOTSET',
     'CRITICAL' : CRITICAL,
     'ERROR' : ERROR,
-    'WARN' : WARN,
+    'WARN' : WARNING,
+    'WARNING' : WARNING,
     'INFO' : INFO,
     'DEBUG' : DEBUG,
     'NOTSET' : NOTSET,
@@ -118,7 +117,7 @@
     """
     Return the textual representation of logging level 'level'.
 
-    If the level is one of the predefined levels (CRITICAL, ERROR, WARN,
+    If the level is one of the predefined levels (CRITICAL, ERROR, WARNING,
     INFO, DEBUG) then you get the corresponding string. If you have
     associated levels with names using addLevelName then the name you have
     associated with 'level' is returned. Otherwise, the string
@@ -214,6 +213,10 @@
             self.thread = thread.get_ident()
         else:
             self.thread = None
+        if hasattr(os, 'getpid'):
+            self.process = os.getpid()
+        else:
+            self.process = None
 
     def __str__(self):
         return '<LogRecord: %s, %s, %s, %s, "%s">'%(self.name, self.levelno,
@@ -226,7 +229,13 @@
         Return the message for this LogRecord after merging any user-supplied
         arguments with the message.
         """
-        msg = str(self.msg)
+        if not hasattr(types, "UnicodeType"): #if no unicode support...
+            msg = str(self.msg)
+        else:
+            try:
+                msg = str(self.msg)
+            except UnicodeError:
+                msg = self.msg      #Defer encoding till later
         if self.args:
             msg = msg % self.args
         return msg
@@ -253,9 +262,9 @@
 
     %(name)s            Name of the logger (logging channel)
     %(levelno)s         Numeric logging level for the message (DEBUG, INFO,
-                        WARN, ERROR, CRITICAL)
+                        WARNING, ERROR, CRITICAL)
     %(levelname)s       Text logging level for the message ("DEBUG", "INFO",
-                        "WARN", "ERROR", "CRITICAL")
+                        "WARNING", "ERROR", "CRITICAL")
     %(pathname)s        Full pathname of the source file where the logging
                         call was issued (if available)
     %(filename)s        Filename portion of pathname
@@ -270,6 +279,7 @@
                         relative to the time the logging module was loaded
                         (typically at application startup time)
     %(thread)d          Thread ID (if available)
+    %(process)d         Process ID (if available)
     %(message)s         The result of record.getMessage(), computed just as
                         the record is emitted
     """
@@ -568,14 +578,17 @@
 
         Emission depends on filters which may have been added to the handler.
         Wrap the actual emission of the record with acquisition/release of
-        the I/O thread lock.
+        the I/O thread lock. Returns whether the filter passed the record for
+        emission.
         """
-        if self.filter(record):
+        rv = self.filter(record)
+        if rv:
             self.acquire()
             try:
                 self.emit(record)
             finally:
                 self.release()
+        return rv
 
     def setFormatter(self, fmt):
         """
@@ -601,17 +614,17 @@
         """
         pass
 
-    def handleError(self):
+    def handleError(self, record):
         """
         Handle errors which occur during an emit() call.
 
         This method should be called from handlers when an exception is
-        encountered during an emit() call. By default it does nothing,
-        because by default raiseExceptions is false, which means that
+        encountered during an emit() call. If raiseExceptions is false,
         exceptions get silently ignored. This is what is mostly wanted
         for a logging system - most users will not care about errors in
         the logging system, they are more interested in application errors.
         You could, however, replace this with a custom handler if you wish.
+        The record which was being processed is passed in to this method.
         """
         if raiseExceptions:
             import traceback
@@ -655,10 +668,16 @@
         """
         try:
             msg = self.format(record)
-            self.stream.write("%s\n" % msg)
+            if not hasattr(types, "UnicodeType"): #if no unicode support...
+                self.stream.write("%s\n" % msg)
+            else:
+                try:
+                    self.stream.write("%s\n" % msg)
+                except UnicodeError:
+                    self.stream.write("%s\n" % msg.encode("UTF-8"))
             self.flush()
         except:
-            self.handleError()
+            self.handleError(record)
 
 class FileHandler(StreamHandler):
     """
@@ -726,11 +745,11 @@
     There is [under normal circumstances] just one Manager instance, which
     holds the hierarchy of loggers.
     """
-    def __init__(self, root):
+    def __init__(self, rootnode):
         """
         Initialize the manager with the root node of the logger hierarchy.
         """
-        self.root = root
+        self.root = rootnode
         self.disable = 0
         self.emittedNoHandlerWarning = 0
         self.loggerDict = {}
@@ -871,19 +890,21 @@
         if INFO >= self.getEffectiveLevel():
             apply(self._log, (INFO, msg, args), kwargs)
 
-    def warn(self, msg, *args, **kwargs):
+    def warning(self, msg, *args, **kwargs):
         """
-        Log 'msg % args' with severity 'WARN'.
+        Log 'msg % args' with severity 'WARNING'.
 
         To pass exception information, use the keyword argument exc_info with
         a true value, e.g.
 
-        logger.warn("Houston, we have a %s", "bit of a problem", exc_info=1)
+        logger.warning("Houston, we have a %s", "bit of a problem", exc_info=1)
         """
-        if self.manager.disable >= WARN:
+        if self.manager.disable >= WARNING:
             return
-        if self.isEnabledFor(WARN):
-            apply(self._log, (WARN, msg, args), kwargs)
+        if self.isEnabledFor(WARNING):
+            apply(self._log, (WARNING, msg, args), kwargs)
+
+    warn = warning
 
     def error(self, msg, *args, **kwargs):
         """
@@ -940,19 +961,14 @@
         Find the stack frame of the caller so that we can note the source
         file name and line number.
         """
-        rv = (None, None)
-        frame = inspect.currentframe().f_back
-        while frame:
-            sfn = inspect.getsourcefile(frame)
-            if sfn:
-                sfn = os.path.normcase(sfn)
-            if sfn != _srcfile:
-                #print frame.f_code.co_code
-                lineno = inspect.getlineno(frame)
-                rv = (sfn, lineno)
-                break
-            frame = frame.f_back
-        return rv
+        f = sys._getframe(1)
+        while 1:
+            co = f.f_code
+            filename = os.path.normcase(co.co_filename)
+            if filename == _srcfile:
+                f = f.f_back
+                continue
+            return filename, f.f_lineno
 
     def makeRecord(self, name, level, fn, lno, msg, args, exc_info):
         """
@@ -966,12 +982,8 @@
         Low-level logging routine which creates a LogRecord and then calls
         all the handlers of this logger to handle the record.
         """
-        if inspect and _srcfile:
-            _acquireLock()
-            try:
-                fn, lno = self.findCaller()
-            finally:
-                _releaseLock()
+        if _srcfile:
+            fn, lno = self.findCaller()
         else:
             fn, lno = "<unknown file>", 0
         if exc_info:
@@ -1001,7 +1013,7 @@
         Remove the specified handler from this logger.
         """
         if hdlr in self.handlers:
-            hdlr.close()
+            #hdlr.close()
             self.handlers.remove(hdlr)
 
     def callHandlers(self, record):
@@ -1066,7 +1078,7 @@
 
 _loggerClass = Logger
 
-root = RootLogger(WARN)
+root = RootLogger(WARNING)
 Logger.root = root
 Logger.manager = Manager(Logger.root)
 
@@ -1138,13 +1150,15 @@
     """
     apply(error, (msg,)+args, {'exc_info': 1})
 
-def warn(msg, *args, **kwargs):
+def warning(msg, *args, **kwargs):
     """
-    Log a message with severity 'WARN' on the root logger.
+    Log a message with severity 'WARNING' on the root logger.
     """
     if len(root.handlers) == 0:
         basicConfig()
-    apply(root.warn, (msg,)+args, kwargs)
+    apply(root.warning, (msg,)+args, kwargs)
+
+warn = warning
 
 def info(msg, *args, **kwargs):
     """
@@ -1178,6 +1192,3 @@
     for h in _handlers.keys():
         h.flush()
         h.close()
-
-if __name__ == "__main__":
-    print __doc__