summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorVinay Sajip <vinay_sajip@yahoo.co.uk>2014-09-14 20:29:11 (GMT)
committerVinay Sajip <vinay_sajip@yahoo.co.uk>2014-09-14 20:29:11 (GMT)
commit02a8f9e9acbe55efcbb7ebc3f821d3d2f9cca368 (patch)
tree842346c95aa2afded7bcf6a5a9171448c62b64c2 /Lib
parent4ff91eb5e38517222ab4e65c1f9d0757c29cebc7 (diff)
downloadcpython-02a8f9e9acbe55efcbb7ebc3f821d3d2f9cca368.zip
cpython-02a8f9e9acbe55efcbb7ebc3f821d3d2f9cca368.tar.gz
cpython-02a8f9e9acbe55efcbb7ebc3f821d3d2f9cca368.tar.bz2
Closes #20537: logging methods now accept an exception instance as well as a Boolean value or exception tuple. Thanks to Yury Selivanov for the patch.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/logging/__init__.py19
-rw-r--r--Lib/test/test_logging.py13
2 files changed, 22 insertions, 10 deletions
diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py
index 7fb3a35..7628d84 100644
--- a/Lib/logging/__init__.py
+++ b/Lib/logging/__init__.py
@@ -1302,12 +1302,11 @@ class Logger(Filterer):
if self.isEnabledFor(ERROR):
self._log(ERROR, msg, args, **kwargs)
- def exception(self, msg, *args, **kwargs):
+ def exception(self, msg, *args, exc_info=True, **kwargs):
"""
Convenience method for logging an ERROR with exception information.
"""
- kwargs['exc_info'] = True
- self.error(msg, *args, **kwargs)
+ self.error(msg, *args, exc_info=exc_info, **kwargs)
def critical(self, msg, *args, **kwargs):
"""
@@ -1402,7 +1401,9 @@ class Logger(Filterer):
else: # pragma: no cover
fn, lno, func = "(unknown file)", 0, "(unknown function)"
if exc_info:
- if not isinstance(exc_info, tuple):
+ if isinstance(exc_info, BaseException):
+ exc_info = (type(exc_info), exc_info, exc_info.__traceback__)
+ elif not isinstance(exc_info, tuple):
exc_info = sys.exc_info()
record = self.makeRecord(self.name, level, fn, lno, msg, args,
exc_info, func, extra, sinfo)
@@ -1612,12 +1613,11 @@ class LoggerAdapter(object):
"""
self.log(ERROR, msg, *args, **kwargs)
- def exception(self, msg, *args, **kwargs):
+ def exception(self, msg, *args, exc_info=True, **kwargs):
"""
Delegate an exception call to the underlying logger.
"""
- kwargs["exc_info"] = True
- self.log(ERROR, msg, *args, **kwargs)
+ self.log(ERROR, msg, *args, exc_info=exc_info, **kwargs)
def critical(self, msg, *args, **kwargs):
"""
@@ -1796,14 +1796,13 @@ def error(msg, *args, **kwargs):
basicConfig()
root.error(msg, *args, **kwargs)
-def exception(msg, *args, **kwargs):
+def exception(msg, *args, exc_info=True, **kwargs):
"""
Log a message with severity 'ERROR' on the root logger, with exception
information. If the logger has no handlers, basicConfig() is called to add
a console handler with a pre-defined format.
"""
- kwargs['exc_info'] = True
- error(msg, *args, **kwargs)
+ error(msg, *args, exc_info=exc_info, **kwargs)
def warning(msg, *args, **kwargs):
"""
diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py
index 070e636..7442381 100644
--- a/Lib/test/test_logging.py
+++ b/Lib/test/test_logging.py
@@ -3712,6 +3712,19 @@ class LoggerAdapterTest(unittest.TestCase):
self.assertEqual(record.exc_info,
(exc.__class__, exc, exc.__traceback__))
+ def test_exception_excinfo(self):
+ try:
+ 1 / 0
+ except ZeroDivisionError as e:
+ exc = e
+
+ self.adapter.exception('exc_info test', exc_info=exc)
+
+ self.assertEqual(len(self.recording.records), 1)
+ record = self.recording.records[0]
+ self.assertEqual(record.exc_info,
+ (exc.__class__, exc, exc.__traceback__))
+
def test_critical(self):
msg = 'critical test! %r'
self.adapter.critical(msg, self.recording)