GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Expose log format to users in assertLogs · Issue #134567 · python/cpython

Expose log format to users in assertLogs #134567

New issue
Copy link
New issue
Copy link
Closed
Closed
Expose log format to users in assertLogs#134567
Copy link
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

Description

@garry-cairns
garry-cairns
opened on May 23, 2025
Issue body actions

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

  • gh-134567: Exposes log format to users in unittest.TestCase.assertLogs #134570
  • gh-134567: Move unittest What’s New entry #136630

Activity

  1. garry-cairns
    added
    type-featureA feature request or enhancementA feature request or enhancement
    on May 23, 2025
  2. picnixz
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on May 23, 2025
  3. picnixz
    added this to Logging issues 🪵on May 23, 2025
  4. vsajip commented on May 26, 2025

    @vsajip
    vsajip
    on May 26, 2025
    Member
    More actions

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

  5. vsajip
    added this to Unittest & doctest issues and removed this from Logging issues 🪵on May 26, 2025
  6. github-project-automation
    moved this to Todo in Unittest & doctest issueson May 26, 2025
  7. picnixz commented on May 26, 2025

    @picnixz
    picnixz
    on May 26, 2025
    Member
    More actions

    Oups my bad! thanks for correctly triaging this

  8. serhiy-storchaka commented on Jun 23, 2025

    @serhiy-storchaka
    serhiy-storchaka
    on Jun 23, 2025
    Member
    More actions

    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?

  9. vsajip commented on Jun 23, 2025

    @vsajip
    vsajip
    on Jun 23, 2025
    Member
    More actions

    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.

  10. garry-cairns commented on Jun 23, 2025

    @garry-cairns
    garry-cairns
    on Jun 23, 2025
    ContributorAuthor
    More actions

    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.

  11. serhiy-storchaka commented on Jun 24, 2025

    @serhiy-storchaka
    serhiy-storchaka
    on Jun 24, 2025
    Member
    More actions

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

  12. pitrou commented on Jun 25, 2025

    @pitrou
    pitrou
    on Jun 25, 2025
    Member
    More actions

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

  13. garry-cairns
    added a commit that references this issue on Jun 29, 2025

    Addresses review comments on pythongh-134567

    7ee8aba
  14. serhiy-storchaka commented on Jul 2, 2025

    @serhiy-storchaka
    serhiy-storchaka
    on Jul 2, 2025
    Member
    More actions

    Thank you for your contribution @garry-cairns.

  15. serhiy-storchaka
    closed this as completedon Jul 2, 2025
  16. github-project-automation
    moved this from Todo to Done in Unittest & doctest issueson Jul 2, 2025
  17. serhiy-storchaka
    added a commit that references this issue on Jul 2, 2025

    gh-134567: Add the formatter parameter in unittest.TestCase.assertLogs (

    51ab66b
  18. AndPuQing
    added a commit that references this issue on Jul 11, 2025

    pythongh-134567: Add the formatter parameter in unittest.TestCase.ass…

    7dec2ba
  19. Pranjal095
    added a commit that references this issue on Jul 12, 2025

    pythongh-134567: Add the formatter parameter in unittest.TestCase.ass…

    18a530e
  20. picnixz
    added a commit that references this issue on Jul 13, 2025

    pythongh-134567: Add the formatter parameter in unittest.TestCase.ass…

    10aed12
  21. hugovk
    added a commit that references this issue on Jul 14, 2025

    gh-134567: Move unittest What’s New entry (#136630)

    75e2c5d
  22. taegyunkim
    added 2 commits that reference this issue on Aug 4, 2025

    pythongh-134567: Add the formatter parameter in unittest.TestCase.ass…

    524a365

    pythongh-134567: Move unittest What’s New entry (python#136630)

    c39fc72
  23. Agent-Hellboy
    added 2 commits that reference this issue on Aug 19, 2025

    pythongh-134567: Add the formatter parameter in unittest.TestCase.ass…

    13fdcbf

    pythongh-134567: Move unittest What’s New entry (python#136630)

    6278acb
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/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

    Projects

    • Unittest & doctest issues
      Status
      Done
      Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.