8000 gh-115233: Fix currentframe to get the frame of original caller by Agent-Hellboy · Pull Request #115241 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-115233: Fix currentframe to get the frame of original caller #115241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
Closed
Prev Previous commit
Next Next commit
Restructure AdapterLogger
  • Loading branch information
Agent-Hellboy committed Feb 11, 2024
commit 1751ab1d0f3a04e1e31956746c96189b44d07a5e
51 changes: 23 additions & 28 deletions Lib/logging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def addLevelName(level, levelName):
_nameToLevel[levelName] = level

if hasattr(sys, "_getframe"):
currentframe = lambda: sys._getframe(3)
currentframe = lambda: sys._getframe(1)
else: #pragma: no cover
def currentframe():
"""Return the frame object for the caller's stack frame."""
Expand Down Expand Up @@ -1830,6 +1830,7 @@ def __reduce__(self):

_loggerClass = Logger


class LoggerAdapter(object):
"""
An adapter for loggers which makes it easier to specify contextual
Expand Down Expand Up @@ -1880,50 +1881,56 @@ def process(self, msg, kwargs):
#
# Boilerplate convenience methods
#
def debug(self, msg, *args, **kwargs):
def debug(self, msg, *args, stacklevel=1, **kwargs):
"""
Delegate a debug call to the underlying logger.
"""
self.log(DEBUG, msg, *args, **kwargs)
self._log(DEBUG, msg, *args, stacklevel=stacklevel + 1, **kwargs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be simpler to just pass **kwargs, as in Logger methods, and just do a double correction in _log().

Also, it will be more efficient to pass args instead of *args.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, please check


def info(self, msg, *args, **kwargs):
def info(self, msg, *args, stacklevel=1, **kwargs):
"""
Delegate an info call to the underlying logger.
"""
self.log(INFO, msg, *args, **kwargs)
self._log(INFO, msg, *args, stacklevel=stacklevel + 1, **kwargs)

def warning(self, msg, *args, **kwargs):
def warning(self, msg, *args, stacklevel=1, **kwargs):
"""
Delegate a warning call to the underlying logger.
"""
self.log(WARNING, msg, *args, **kwargs)
self._log(WARNING, msg, *args, stacklevel=stacklevel + 1, **kwargs)

def error(self, msg, *args, **kwargs):
def error(self, msg, *args, stacklevel=1, **kwargs):
"""
Delegate an error call to the underlying logger.
"""
self.log(ERROR, msg, *args, **kwargs)
self._log(ERROR, msg, *args, stacklevel=stacklevel + 1, **kwargs)

def exception(self, msg, *args, exc_info=True, **kwargs):
def exception(self, msg, *args, stacklevel=1, exc_info=True, **kwargs):
"""
Delegate an exception call to the underlying logger.
"""
self.log(ERROR, msg, *args, exc_info=exc_info, **kwargs)
self._log(ERROR, msg, *args, stacklevel=stacklevel + 1, exc_info=exc_info, **kwargs)

def critical(self, msg, *args, **kwargs):
def critical(self, msg, *args, stacklevel=1, **kwargs):
"""
Delegate a critical call to the underlying logger.
"""
self.log(CRITICAL, msg, *args, **kwargs)
self._log(CRITICAL, msg, *args, stacklevel=stacklevel + 1, **kwargs)

def log(self, level, msg, *args, **kwargs):
def log(self, level, msg, *args, stacklevel=1, **kwargs):
"""
Delegate a log call to the underlying logger, after adding
contextual information from this adapter instance.
"""
self._log(level, msg, *args, stacklevel=stacklevel + 1, **kwargs)

def _log(self, level, msg, *args, stacklevel=1, **kwargs):
"""
Low-level log implementation, proxied to allow nested logger adapters.
"""
if self.isEnabledFor(level):
msg, kwargs = self.process(msg, kwargs)
self.logger.log(level, msg, *args, **kwargs)
self.logger._log(level, msg, args, **kwargs, stacklevel=stacklevel + 1)

def isEnabledFor(self, level):
"""
Expand All @@ -1949,19 +1956,6 @@ def hasHandlers(self):
"""
return self.logger.hasHandlers()

def _log(self, level, msg, args, exc_info=None, extra=None, stack_info=False):
"""
Low-level log implementation, proxied to allow nested logger adapters.
"""
return self.logger._log(
level,
msg,
args,
exc_info=exc_info,
extra=extra,
stack_info=stack_info,
)

@property
def manager(self):
return self.logger.manager
Expand All @@ -1981,6 +1975,7 @@ def __repr__(self):

__class_getitem__ = classmethod(GenericAlias)


root = RootLogger(WARNING)
Logger.root = root
Logger.manager = Manager(Logger.root)
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -5614,6 +5614,8 @@ def test_main_function_logs_info_message(self):
log_output = self.stream.getvalue()
assert 'main' in log_output



class LoggerTest(BaseTest, AssertErrorMessage):

def setUp(self):
Expand Down
0