Skip to content
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

LoggerAdapter and %(funcName)s prints the wrong function name #115233

Closed
dogbertai opened this issue Feb 10, 2024 · 3 comments
Closed

LoggerAdapter and %(funcName)s prints the wrong function name #115233

dogbertai opened this issue Feb 10, 2024 · 3 comments
Labels
3.11 only security fixes 3.12 bugs and security fixes 3.13 new features, bugs and security fixes type-bug An unexpected behavior, bug, or error

Comments

@dogbertai
Copy link

dogbertai commented Feb 10, 2024

Bug report

Bug description:

Using the StyleAdapter from the Cookbook https://docs.python.org/3/howto/logging-cookbook.html
With the %(funcName)s formatter
Yields the wrong funcName in 3.11 and later.

No matter where you use the StyleAdapter logger, it always prints "log"

#!/usr/bin/env python3
import logging.handlers
import sys


class Message:
    def __init__(self, fmt, args):
        self.fmt = fmt
        self.args = args

    def __str__(self):
        return self.fmt.format(*self.args)


class StyleAdapter(logging.LoggerAdapter):
    def __init__(self, logger, extra=None):
        super().__init__(logger, extra or {})

    def log(self, level, msg, /, *args, **kwargs):
        if self.isEnabledFor(level):
            msg, kwargs = self.process(msg, kwargs)
            self.logger._log(level, Message(msg, args), (), **kwargs)


logger = StyleAdapter(logging.getLogger(__name__))


def main():
    root_logger = logging.getLogger()
    root_logger.setLevel(logging.DEBUG)

    # create formatter
    formatter = logging.Formatter(
        '%(asctime)s %(name)s %(funcName)s %(levelname)s %(message)s')

    # create console handler and set level to debug
    console_handler = logging.StreamHandler()
    console_handler.setFormatter(formatter)
    root_logger.addHandler(console_handler)
    root_logger.info('Logger initialized.')

    logger.info('test')


if __name__ == '__main__':
    sys.exit(main())
PS > py -3.10 test_log.py
2024-02-09 23:15:33,506 root main INFO Logger initialized.
2024-02-09 23:15:33,506 __main__ main INFO test
PS > py -3.12 test_log.py                                                          
2024-02-09 23:15:29,298 root main INFO Logger initialized.
2024-02-09 23:15:29,299 __main__ log INFO test

CPython versions tested on:

3.10, 3.12

Operating systems tested on:

Windows

Linked PRs

@dogbertai dogbertai added the type-bug An unexpected behavior, bug, or error label Feb 10, 2024
@Eclips4 Eclips4 added 3.11 only security fixes 3.12 bugs and security fixes 3.13 new features, bugs and security fixes labels Feb 10, 2024
@Agent-Hellboy
Copy link
Contributor

bisected to 5ca6d74

he changed currentframe function
5ca6d74#diff-1bf0ad6d121df3948853dafdd8e5406709fe0c3911b30e3cf2def41717455c98L162

Thanks, @Eclips4 for help

@Jason-Y-Z
Copy link
Contributor

Thanks all for the discussion. Issue aside, I think it would be really helpful to have some comments for 5ca6d74#diff-1bf0ad6d121df3948853dafdd8e5406709fe0c3911b30e3cf2def41717455c98L162 as sys._getframe(1) v.s. sys._getframe(3) feels equally magic :)

@Agent-Hellboy
Copy link
Contributor

sys._getframe(1) is missing the caller frame if _log is called through an intermediate function , I am going to add a PR

serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Feb 12, 2024
Also add more tests for LoggerAdapter.

Also support stacklevel in LoggerAdapter._log().
serhiy-storchaka added a commit that referenced this issue Feb 12, 2024
Also add more tests for LoggerAdapter.

Also support stacklevel in LoggerAdapter._log().
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Feb 12, 2024
…nGH-115325)

Also add more tests for LoggerAdapter.

Also support stacklevel in LoggerAdapter._log().
(cherry picked from commit 9182201)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
serhiy-storchaka added a commit that referenced this issue Feb 12, 2024
…H-115355)

Also add more tests for LoggerAdapter.

Also support stacklevel in LoggerAdapter._log().
(cherry picked from commit 9182201)
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Feb 12, 2024
…nGH-115325) (pythonGH-115355)

Also add more tests for LoggerAdapter.

(cherry picked from commit 225856e)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Also support stacklevel in LoggerAdapter._log().
(cherry picked from commit 9182201)
serhiy-storchaka pushed a commit that referenced this issue Feb 12, 2024
…H-115355) (GH-115357)

Also add more tests for LoggerAdapter.

Also support stacklevel in LoggerAdapter._log().
(cherry picked from commit 225856e)
(cherry picked from commit 9182201)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
fsc-eriker pushed a commit to fsc-eriker/cpython that referenced this issue Feb 14, 2024
)

Also add more tests for LoggerAdapter.

Also support stacklevel in LoggerAdapter._log().
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.11 only security fixes 3.12 bugs and security fixes 3.13 new features, bugs and security fixes type-bug An unexpected behavior, bug, or error
Projects
Development

No branches or pull requests

5 participants