summaryrefslogtreecommitdiffstats
path: root/Lib/logging
diff options
context:
space:
mode:
authorVinay Sajip <vinay_sajip@yahoo.co.uk>2018-06-05 16:24:18 (GMT)
committerGitHub <noreply@github.com>2018-06-05 16:24:18 (GMT)
commitdde9fdbe453925279ac3d2a6a72102f6f9ef247c (patch)
treeae9970f3597b3ee855a468c49d7007fa6f57ba16 /Lib/logging
parent9ef1b0690b90c526798b6b3125b0fa7ae98319a2 (diff)
downloadcpython-dde9fdbe453925279ac3d2a6a72102f6f9ef247c.zip
cpython-dde9fdbe453925279ac3d2a6a72102f6f9ef247c.tar.gz
cpython-dde9fdbe453925279ac3d2a6a72102f6f9ef247c.tar.bz2
bpo-33165: Added stacklevel parameter to logging APIs. (GH-7424)
Diffstat (limited to 'Lib/logging')
-rw-r--r--Lib/logging/__init__.py13
1 files changed, 10 insertions, 3 deletions
diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py
index a3617b1..e6c9f32 100644
--- a/Lib/logging/__init__.py
+++ b/Lib/logging/__init__.py
@@ -1397,7 +1397,7 @@ class Logger(Filterer):
if self.isEnabledFor(level):
self._log(level, msg, args, **kwargs)
- def findCaller(self, stack_info=False):
+ def findCaller(self, stack_info=False, stacklevel=1):
"""
Find the stack frame of the caller so that we can note the source
file name, line number and function name.
@@ -1407,6 +1407,12 @@ class Logger(Filterer):
#IronPython isn't run with -X:Frames.
if f is not None:
f = f.f_back
+ orig_f = f
+ while f and stacklevel > 1:
+ f = f.f_back
+ stacklevel -= 1
+ if not f:
+ f = orig_f
rv = "(unknown file)", 0, "(unknown function)", None
while hasattr(f, "f_code"):
co = f.f_code
@@ -1442,7 +1448,8 @@ class Logger(Filterer):
rv.__dict__[key] = extra[key]
return rv
- def _log(self, level, msg, args, exc_info=None, extra=None, stack_info=False):
+ def _log(self, level, msg, args, exc_info=None, extra=None, stack_info=False,
+ stacklevel=1):
"""
Low-level logging routine which creates a LogRecord and then calls
all the handlers of this logger to handle the record.
@@ -1453,7 +1460,7 @@ class Logger(Filterer):
#exception on some versions of IronPython. We trap it here so that
#IronPython can use logging.
try:
- fn, lno, func, sinfo = self.findCaller(stack_info)
+ fn, lno, func, sinfo = self.findCaller(stack_info, stacklevel)
except ValueError: # pragma: no cover
fn, lno, func = "(unknown file)", 0, "(unknown function)"
else: # pragma: no cover