FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Expose log format to users in assertLogs · Issue #134567 · python/cpython · GitHub

Repository navigation

Expose log format to users in assertLogs #134567

Closed

Description

Feature or enhancement

Proposal:

Use-case

Someone works in an organisation with a custom log formatter. They want to make assertions about logs in a particular test and would like the assertions to match the format used in the org. Currently the format is static in assertLogs.

Example usage

format = "[No.1: the larch] %(levelname)s:%(name)s:%(message)s"
formatter = logging.Formatter(format)
with self.assertLogs(formatter=formatter) as cm:
    log_foo.info("1")
    log_foobar.debug("2")
self.assertEqual(cm.output, ["[No.1: the larch] INFO:foo:1"])

Proposed change

My branch already has my proposed change and tests are passing. Since this would be my first contribution if accepted I'm raising the issue since I don't yet have a feel for what constitutes a "minor" enhancement that doesn't need an issue ref per the developer guide. Having raised this I'll create a PR as per the developer guide.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

Activity

  1. added
    stdlibStandard Library Python modules in the Lib/ directory
    on May 23, 2025
  2. vsajip commented on May 26, 2025

    Member

    @picnixz this appears to be a unittest issue rather than logging? The linked PR doesn't touch logging code.

  3. picnixz commented on May 26, 2025

    Member

    Oups my bad! thanks for correctly triaging this

  4. serhiy-storchaka commented on Jun 23, 2025

    Member

    You can change the formatter by executing logging.getLogger().handlers[0].setFormatter(formatter) in the with block. This is just a little verbose. You need to change the code if you also specified a logger.

    @vsajip, what is your opinion about usefulness of such feature?

  5. vsajip commented on Jun 23, 2025

    Member

    It seems like a "Wouldn't it be nice if ... ?" feature rather than an essential one, and in any case it seems like a change in unittest rather than logging. Your suggestion would be using internal logging API (handlers attribute of Logger isn't a documented API), so I wouldn't recommend it to be done in user code. The documented purpose of assertLogs relates to assertions about loggers and levels only, so changing it to encompass formats seems out of scope and not necessary. However, unittest maintainers might have a different view.

  6. garry-cairns commented on Jun 23, 2025

    ContributorAuthor

    You can change the formatter by executing logging.getLogger().handlers[0].setFormatter(formatter) in the with block. This is just a little verbose. You need to change the code if you also specified a logger.

    Agreed, the intent here was just to make this feel a little more natural.

  7. serhiy-storchaka commented on Jun 24, 2025

    Member

    @pitrou, what is your opinion about this feature as the original author of assertLogs()?

  8. pitrou commented on Jun 25, 2025

    Member

    That sounds reasonable to me, even if probably a bit niche.

  9. serhiy-storchaka commented on Jul 2, 2025

    Member

    Thank you for your contribution @garry-cairns.

  10. added a commit that references this issue on Jul 2, 2025
  11. added a commit that references this issue on Jul 11, 2025
  12. added a commit that references this issue on Jul 12, 2025
  13. added a commit that references this issue on Jul 13, 2025
  14. added a commit that references this issue on Jul 14, 2025
  15. added 2 commits that reference this issue on Aug 4, 2025
  16. added 2 commits that reference this issue on Aug 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


    Back | FazBrowse Home | New Git URL