python/dist/src/Lib/logging handlers.py,1.8,1.9
Update of /cvsroot/python/python/dist/src/Lib/logging In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11829 Modified Files: handlers.py Log Message: Copyright year change. Corrections to comments. Tracebacks can now be sent via SocketHandler. SocketHandler now uses exponential backoff strategy. Handlers now chain to Handler.close() from their close() methods. Index: handlers.py =================================================================== RCS file: /cvsroot/python/python/dist/src/Lib/logging/handlers.py,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** handlers.py 8 Nov 2003 11:40:03 -0000 1.8 --- handlers.py 20 Feb 2004 13:17:27 -0000 1.9 *************** *** 20,26 **** Should work under Python versions >= 1.5.2, except that source line ! information is not available unless 'inspect' is. ! Copyright (C) 2001-2002 Vinay Sajip. All Rights Reserved. To use, simply 'import logging' and log away! --- 20,26 ---- Should work under Python versions >= 1.5.2, except that source line ! information is not available unless 'sys._getframe()' is. ! Copyright (C) 2001-2004 Vinay Sajip. All Rights Reserved. To use, simply 'import logging' and log away! *************** *** 133,136 **** --- 133,143 ---- self.sock = None self.closeOnError = 0 + self.retryTime = None + # + # Exponential backoff parameters. + # + self.retryStart = 1.0 + self.retryMax = 30.0 + self.retryFactor = 2.0 def makeSocket(self): *************** *** 143,146 **** --- 150,181 ---- return s + def createSocket(self): + """ + Try to create a socket, using an exponential backoff with + a max retry time. Thanks to Robert Olson for the original patch + (SF #815911) which has been slightly refactored. + """ + now = time.time() + # Either retryTime is None, in which case this + # is the first time back after a disconnect, or + # we've waited long enough. + if self.retryTime is None: + attempt = 1 + else: + attempt = (now >= self.retryTime) + if attempt: + try: + self.sock = self.makeSocket() + self.retryTime = None # next time, no delay before trying + except: + #Creation failed, so set the retry time and return. + if self.retryTime is None: + self.retryPeriod = self.retryStart + else: + self.retryPeriod = self.retryPeriod * self.retryFactor + if self.retryPeriod > self.retryMax: + self.retryPeriod = self.retryMax + self.retryTime = now + self.retryPeriod + def send(self, s): """ *************** *** 150,162 **** network is busy. """ ! if hasattr(self.sock, "sendall"): ! self.sock.sendall(s) ! else: ! sentsofar = 0 ! left = len(s) ! while left > 0: ! sent = self.sock.send(s[sentsofar:]) ! sentsofar = sentsofar + sent ! left = left - sent def makePickle(self, record): --- 185,207 ---- network is busy. """ ! if self.sock is None: ! self.createSocket() ! #self.sock can be None either because we haven't reached the retry ! #time yet, or because we have reached the retry time and retried, ! #but are still unable to connect. ! if self.sock: ! try: ! if hasattr(self.sock, "sendall"): ! self.sock.sendall(s) ! else: ! sentsofar = 0 ! left = len(s) ! while left > 0: ! sent = self.sock.send(s[sentsofar:]) ! sentsofar = sentsofar + sent ! left = left - sent ! except socket.error: ! self.sock.close() ! self.sock = None # so we can call createSocket next time def makePickle(self, record): *************** *** 165,171 **** returns it ready for transmission across the socket. """ s = cPickle.dumps(record.__dict__, 1) ! #n = len(s) ! #slen = "%c%c" % ((n >> 8) & 0xFF, n & 0xFF) slen = struct.pack(">L", len(s)) return slen + s --- 210,220 ---- returns it ready for transmission across the socket. """ + ei = record.exc_info + if ei: + dummy = self.format(record) # just to get traceback text into record.exc_text + record.exc_info = None # to avoid Unpickleable error s = cPickle.dumps(record.__dict__, 1) ! if ei: ! record.exc_info = ei # for next handler slen = struct.pack(">L", len(s)) return slen + s *************** *** 196,201 **** try: s = self.makePickle(record) - if not self.sock: - self.sock = self.makeSocket() self.send(s) except: --- 245,248 ---- *************** *** 209,212 **** --- 256,260 ---- self.sock.close() self.sock = None + logging.Handler.close(self) class DatagramHandler(SocketHandler): *************** *** 387,390 **** --- 435,439 ---- if self.unixsocket: self.socket.close() + logging.Handler.close(self) def emit(self, record): *************** *** 581,585 **** """ #self._welu.RemoveSourceFromRegistry(self.appname, self.logtype) ! pass class HTTPHandler(logging.Handler): --- 630,634 ---- """ #self._welu.RemoveSourceFromRegistry(self.appname, self.logtype) ! logging.Handler.close(self) class HTTPHandler(logging.Handler): *************** *** 604,608 **** """ Default implementation of mapping the log record into a dict ! that is send as the CGI data. Overwrite in your class. Contributed by Franz Glasner. """ --- 653,657 ---- """ Default implementation of mapping the log record into a dict ! that is sent as the CGI data. Overwrite in your class. Contributed by Franz Glasner. """ *************** *** 727,728 **** --- 776,778 ---- self.target = None self.buffer = [] + BufferingHandler.close(self)
participants (1)
-
vsajip@users.sourceforge.net