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
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)
withself.assertLogs(formatter=formatter) ascm:
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
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?
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.
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.
Activity
vsajip commentedon May 26, 2025
@picnixz this appears to be a
unittestissue rather thanlogging? The linked PR doesn't touch logging code.picnixz commentedon May 26, 2025
Oups my bad! thanks for correctly triaging this
serhiy-storchaka commentedon Jun 23, 2025
You can change the formatter by executing
logging.getLogger().handlers[0].setFormatter(formatter)in thewithblock. 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?
vsajip commentedon Jun 23, 2025
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
unittestrather thanlogging. Your suggestion would be using internalloggingAPI (handlersattribute ofLoggerisn't a documented API), so I wouldn't recommend it to be done in user code. The documented purpose ofassertLogsrelates to assertions about loggers and levels only, so changing it to encompass formats seems out of scope and not necessary. However,unittestmaintainers might have a different view.garry-cairns commentedon Jun 23, 2025
Agreed, the intent here was just to make this feel a little more natural.
serhiy-storchaka commentedon Jun 24, 2025
@pitrou, what is your opinion about this feature as the original author of
assertLogs()?pitrou commentedon Jun 25, 2025
That sounds reasonable to me, even if probably a bit niche.
Addresses review comments on pythongh-134567
serhiy-storchaka commentedon Jul 2, 2025
Thank you for your contribution @garry-cairns.
gh-134567: Add the formatter parameter in unittest.TestCase.assertLogs (
pythongh-134567: Add the formatter parameter in unittest.TestCase.ass…
pythongh-134567: Add the formatter parameter in unittest.TestCase.ass…
pythongh-134567: Add the formatter parameter in unittest.TestCase.ass…
gh-134567: Move unittest What’s New entry (#136630)
pythongh-134567: Add the formatter parameter in unittest.TestCase.ass…
pythongh-134567: Move unittest What’s New entry (python#136630)
pythongh-134567: Add the formatter parameter in unittest.TestCase.ass…
pythongh-134567: Move unittest What’s New entry (python#136630)