-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Feature request: use caplog with custom formatter #2987
Comments
Perhaps this can be implemented as a hook: @hookimpl(firstresult=True)
def pytest_logging_formatter(config):
"""Return a logging.Formatter instance that will be used by the internal logging capture""" Then you can do this in your root def pytest_logging_formatter(config):
return MyCustomFormatter() |
as with other topics, i'd like to point to the fact that pytest hooks are a poor mechanism for selecting a "driver" an ini option/section to select a formatter object and/or its parameters would be nice |
Closing this issue as the proposal has been inactive for over a year. |
I think this should be reopened. It is needed for #4485. |
Expanding on @nicoulaj's solution, I recommend patching the class StructlogLoggingPlugin(pytest_logging.LoggingPlugin):
"""Replacement logging plugin that uses our formatter
"""
def _create_formatter(self, log_format,
log_date_format) -> logging.Formatter:
"""Patch the logger method to always return out formatter
Returns:
logging.Formatter: Our structlog enhanced formatter
"""
del log_format, log_date_format
return <your formatter> |
One option (definitely not optimal) would be to instantiate your custom formatter and set it explicitly on the caplog handler. def test_my_function(caplog)
formatter = MyCustomFormatter()
caplog.handler.setFormatter(formatter)
my_function()
assert caplog.text == .... |
Simply replacing the formatter by retrieving the logging plugin also works.
There is only ever one formatter attached to handler, so you are effectively replacing the existing one. |
I've used this pattern, but just got bitten by an undesired side-effect: subsequent tests that also use A slightly improved version that does take care of restoring the original formatter is as follows: def test_my_function(caplog, monkeypatch)
monkeypatch.setattr(caplog.handler, "formatter", MyCustomFormatter())
my_function()
assert caplog.text == .... |
Maybe somewhat related to issue #11610 where we use a context manager to (temporarily) add a filter to the fixture… |
My application uses a quite heavily customized logging formatter.
Right now, the logging plugin only allows to specify the formats for the formatter, not completely replacing it.
It would be nice if there was a clean way to provide a custom formatter.
This is the workaround I use for now, patching the plugin class on the module:
The text was updated successfully, but these errors were encountered: