diff -r 719c11b6b6ff Doc/library/logging.rst --- a/Doc/library/logging.rst Wed Apr 06 09:50:03 2016 +0300 +++ b/Doc/library/logging.rst Wed Apr 06 22:39:05 2016 +0300 @@ -447,10 +447,14 @@ have been added to the handler. Wraps the actual emission of the record with acquisition/release of the I/O thread lock. + .. versionchanged:: 3.6 + When an exception is encountere during an :meth:`emit` call + :meth:`handleError` will be called. + .. method:: Handler.handleError(record) - This method should be called from handlers when an exception is encountered + This method is called from handlers when an exception is encountered during an :meth:`emit` call. If the module-level attribute ``raiseExceptions`` is ``False``, exceptions get silently ignored. This is what is mostly wanted for a logging system - most users will not care about @@ -460,6 +464,10 @@ occurred. (The default value of ``raiseExceptions`` is ``True``, as that is more useful during development). + .. versionchanged:: 3.6 + This method is called from the :meth:`handle` method on an exception + during an :meth:`emit` call. + .. method:: Handler.format(record) diff -r 719c11b6b6ff Lib/logging/__init__.py --- a/Lib/logging/__init__.py Wed Apr 06 09:50:03 2016 +0300 +++ b/Lib/logging/__init__.py Wed Apr 06 22:39:05 2016 +0300 @@ -854,6 +854,8 @@ self.acquire() try: self.emit(record) + except Exception: + self.handleError(record) finally: self.release() return rv @@ -977,14 +979,11 @@ has an 'encoding' attribute, it is used to determine how to do the output to the stream. """ - try: - msg = self.format(record) - stream = self.stream - stream.write(msg) - stream.write(self.terminator) - self.flush() - except Exception: - self.handleError(record) + msg = self.format(record) + stream = self.stream + stream.write(msg) + stream.write(self.terminator) + self.flush() class FileHandler(StreamHandler): """ diff -r 719c11b6b6ff Lib/logging/handlers.py --- a/Lib/logging/handlers.py Wed Apr 06 09:50:03 2016 +0300 +++ b/Lib/logging/handlers.py Wed Apr 06 22:39:05 2016 +0300 @@ -67,12 +67,9 @@ Output the record to the file, catering for rollover as described in doRollover(). """ - try: - if self.shouldRollover(record): - self.doRollover() - logging.FileHandler.emit(self, record) - except Exception: - self.handleError(record) + if self.shouldRollover(record): + self.doRollover() + logging.FileHandler.emit(self, record) def rotation_filename(self, default_name): """ @@ -626,11 +623,8 @@ If there was a problem with the socket, re-establishes the socket. """ - try: - s = self.makePickle(record) - self.send(s) - except Exception: - self.handleError(record) + s = self.makePickle(record) + self.send(s) def close(self): """ @@ -891,34 +885,31 @@ The record is formatted, and then sent to the syslog server. If exception information is present, it is NOT sent to the server. """ - try: - msg = self.format(record) - if self.ident: - msg = self.ident + msg - if self.append_nul: - msg += '\000' + msg = self.format(record) + if self.ident: + msg = self.ident + msg + if self.append_nul: + msg += '\000' - # We need to convert record level to lowercase, maybe this will - # change in the future. - prio = '<%d>' % self.encodePriority(self.facility, - self.mapPriority(record.levelname)) - prio = prio.encode('utf-8') - # Message is a string. Convert to bytes as required by RFC 5424 - msg = msg.encode('utf-8') - msg = prio + msg - if self.unixsocket: - try: - self.socket.send(msg) - except OSError: - self.socket.close() - self._connect_unixsocket(self.address) - self.socket.send(msg) - elif self.socktype == socket.SOCK_DGRAM: - self.socket.sendto(msg, self.address) - else: - self.socket.sendall(msg) - except Exception: - self.handleError(record) + # We need to convert record level to lowercase, maybe this will + # change in the future. + prio = '<%d>' % self.encodePriority(self.facility, + self.mapPriority(record.levelname)) + prio = prio.encode('utf-8') + # Message is a string. Convert to bytes as required by RFC 5424 + msg = msg.encode('utf-8') + msg = prio + msg + if self.unixsocket: + try: + self.socket.send(msg) + except OSError: + self.socket.close() + self._connect_unixsocket(self.address) + self.socket.send(msg) + elif self.socktype == socket.SOCK_DGRAM: + self.socket.sendto(msg, self.address) + else: + self.socket.sendall(msg) class SMTPHandler(logging.Handler): """ @@ -974,31 +965,28 @@ Format the record and send it to the specified addressees. """ - try: - import smtplib - from email.message import EmailMessage - import email.utils + import smtplib + from email.message import EmailMessage + import email.utils - port = self.mailport - if not port: - port = smtplib.SMTP_PORT - smtp = smtplib.SMTP(self.mailhost, port, timeout=self.timeout) - msg = EmailMessage() - msg['From'] = self.fromaddr - msg['To'] = ','.join(self.toaddrs) - msg['Subject'] = self.getSubject(record) - msg['Date'] = email.utils.localtime() - msg.set_content(self.format(record)) - if self.username: - if self.secure is not None: - smtp.ehlo() - smtp.starttls(*self.secure) - smtp.ehlo() - smtp.login(self.username, self.password) - smtp.send_message(msg) - smtp.quit() - except Exception: - self.handleError(record) + port = self.mailport + if not port: + port = smtplib.SMTP_PORT + smtp = smtplib.SMTP(self.mailhost, port, timeout=self.timeout) + msg = EmailMessage() + msg['From'] = self.fromaddr + msg['To'] = ','.join(self.toaddrs) + msg['Subject'] = self.getSubject(record) + msg['Date'] = email.utils.localtime() + msg.set_content(self.format(record)) + if self.username: + if self.secure is not None: + smtp.ehlo() + smtp.starttls(*self.secure) + smtp.ehlo() + smtp.login(self.username, self.password) + smtp.send_message(msg) + smtp.quit() class NTEventLogHandler(logging.Handler): """ @@ -1076,14 +1064,11 @@ log the message in the NT event log. """ if self._welu: - try: - id = self.getMessageID(record) - cat = self.getEventCategory(record) - type = self.getEventType(record) - msg = self.format(record) - self._welu.ReportEvent(self.appname, id, cat, type, [msg]) - except Exception: - self.handleError(record) + id = self.getMessageID(record) + cat = self.getEventCategory(record) + type = self.getEventType(record) + msg = self.format(record) + self._welu.ReportEvent(self.appname, id, cat, type, [msg]) def close(self): """ @@ -1137,43 +1122,40 @@ Send the record to the Web server as a percent-encoded dictionary """ - try: - import http.client, urllib.parse - host = self.host - if self.secure: - h = http.client.HTTPSConnection(host, context=self.context) + import http.client, urllib.parse + host = self.host + if self.secure: + h = http.client.HTTPSConnection(host, context=self.context) + else: + h = http.client.HTTPConnection(host) + url = self.url + data = urllib.parse.urlencode(self.mapLogRecord(record)) + if self.method == "GET": + if (url.find('?') >= 0): + sep = '&' else: - h = http.client.HTTPConnection(host) - url = self.url - data = urllib.parse.urlencode(self.mapLogRecord(record)) - if self.method == "GET": - if (url.find('?') >= 0): - sep = '&' - else: - sep = '?' - url = url + "%c%s" % (sep, data) - h.putrequest(self.method, url) - # support multiple hosts on one IP address... - # need to strip optional :port from host, if present - i = host.find(":") - if i >= 0: - host = host[:i] - h.putheader("Host", host) - if self.method == "POST": - h.putheader("Content-type", - "application/x-www-form-urlencoded") - h.putheader("Content-length", str(len(data))) - if self.credentials: - import base64 - s = ('u%s:%s' % self.credentials).encode('utf-8') - s = 'Basic ' + base64.b64encode(s).strip() - h.putheader('Authorization', s) - h.endheaders() - if self.method == "POST": - h.send(data.encode('utf-8')) - h.getresponse() #can't do anything with the result - except Exception: - self.handleError(record) + sep = '?' + url = url + "%c%s" % (sep, data) + h.putrequest(self.method, url) + # support multiple hosts on one IP address... + # need to strip optional :port from host, if present + i = host.find(":") + if i >= 0: + host = host[:i] + h.putheader("Host", host) + if self.method == "POST": + h.putheader("Content-type", + "application/x-www-form-urlencoded") + h.putheader("Content-length", str(len(data))) + if self.credentials: + import base64 + s = ('u%s:%s' % self.credentials).encode('utf-8') + s = 'Basic ' + base64.b64encode(s).strip() + h.putheader('Authorization', s) + h.endheaders() + if self.method == "POST": + h.send(data.encode('utf-8')) + h.getresponse() #can't do anything with the result class BufferingHandler(logging.Handler): """ @@ -1354,10 +1336,7 @@ Writes the LogRecord to the queue, preparing it for pickling first. """ - try: - self.enqueue(self.prepare(record)) - except Exception: - self.handleError(record) + self.enqueue(self.prepare(record)) if threading: class QueueListener(object):