You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
stacklevel handling in logging module is inconsistent #89334
Handling of stacklevel in the logging module makes a few unwarranted assumptions, for instance on the depth of the stack due to internal logging module calls. This can be seen for instance when using the shortcut call logging.warning to the root logger, instead of using root_logger.warning. Consider the following stacklevel.py file:
The stacklevel arg was added 3+ years ago, wouldn't fixing this break a lot of code in a way that's hard to detect? That is to say, logs will look just fine, but when you try to debug an issue, you will realise it's pointing to an unhelpful location?
joukewitteveen commented on Nov 9, 2021
joukewitteveenmannequin
MannequinAuthor
More actions
I would expect the opposite. Since the issue is visible only in certain cases (shortcut calls such as logging.info over logger.info, or redirected calls such as logger.warn which adds a stack frame for redirecting to logger.warning), any code that uses the stacklevel argument is probably broken in subtle ways. It will work fine for the anticipated case, but for instance behave weirdly in interactive sessions such as in a debugger.
Added to this, if we want to fix the documentation instead of the logging module code, we have to come up with an understandable description of a behavior that is really inconsistent and odd. We would probably spend most of the documentation explaining edge cases.
Activity
joukewitteveen commentedon Sep 11, 2021
Handling of
stacklevelin the logging module makes a few unwarranted assumptions, for instance on the depth of the stack due to internal logging module calls. This can be seen for instance when using the shortcut calllogging.warningto the root logger, instead of usingroot_logger.warning. Consider the followingstacklevel.pyfile:The output of running
PYTHONWARNINGS=always python stacklevel.pyis:Looking at the line numbers, we get:
stacklevel 0: lines 8, 9, 10.
stacklevel 1: lines 8, 9, 10.
stacklevel 2: lines 14, 9, 14.
stacklevel 3: lines sys:1, 14, 10.
stacklevel 4: lines sys:1, 9, 10.
As can be seen, the stacklevel for the on-module (shortcut) calls lags one level of unwinding behind.
akulakov commentedon Nov 9, 2021
The stacklevel arg was added 3+ years ago, wouldn't fixing this break a lot of code in a way that's hard to detect? That is to say, logs will look just fine, but when you try to debug an issue, you will realise it's pointing to an unhelpful location?
joukewitteveen commentedon Nov 9, 2021
I would expect the opposite. Since the issue is visible only in certain cases (shortcut calls such as
logging.infooverlogger.info, or redirected calls such aslogger.warnwhich adds a stack frame for redirecting tologger.warning), any code that uses the stacklevel argument is probably broken in subtle ways. It will work fine for the anticipated case, but for instance behave weirdly in interactive sessions such as in a debugger.Added to this, if we want to fix the documentation instead of the logging module code, we have to come up with an understandable description of a behavior that is really inconsistent and odd. We would probably spend most of the documentation explaining edge cases.
vsajip commentedon Mar 27, 2022
New changeset 5ca6d74 by Jouke Witteveen in branch 'main':
bpo-45171: Fix stacklevel handling in logging. (GH-28287)
5ca6d74
tirkarthi commentedon Mar 27, 2022
The commit seems to emit a deprecation warning in test_logging. Probably the warning needs to be handled while setting trigger = self.logger.warn
== Tests result: SUCCESS ==
1 test OK.
Total duration: 20.1 sec
Tests result: SUCCESS
vsajip commentedon Mar 27, 2022
New changeset c12ba6b by Jouke Witteveen in branch 'main':
bpo-45171: Remove tests of deprecated logger.warn(). (GH-32139)
c12ba6b
joukewitteveen commentedon Jun 13, 2022
This was fixed by #28287 and the issue can be closed.