question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature request: use caplog with custom formatter

See original GitHub issue

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:

from _pytest import logging as pytest_logging

def pytest_configure(config):
    config.pluginmanager.get_plugin('logging').LoggingPlugin = MyLoggingPlugin

class MyLoggingPlugin(pytest_logging.LoggingPlugin):
    def __init__(self, config):
        super().__init__(config)
        self.formatter = MyCustomFormatter()

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:10
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

9reactions
brechincommented, Jun 29, 2020

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 == ....
2reactions
nicoddemuscommented, Nov 30, 2017

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 conftest.py file:

def pytest_logging_formatter(config):
    return MyCustomFormatter()
Read more comments on GitHub >

github_iconTop Results From Across the Web

Pytest caplog works with custom logger when created with a ...
I have been testing my custom loggers with Pytest. Created a custom logger from Yaml config file, and wrote the following test:
Read more >
How to manage logging — pytest documentation
If desired the log and date format can be specified to anything that the ... To access logs from other stages, use the...
Read more >
_pytest.logging module — pytest API documentation
Returns a list of a stripped down version of log records intended for use in assertion comparison. The format of the tuple is:...
Read more >
loguru Documentation - Read the Docs
Loguru is a library which aims to bring enjoyable logging in Python. Did you ever feel lazy about configuring a logger and used...
Read more >
Reference — pytest documentation
@pytest.fixture; config.cache; capsys; capsysbinary; capfd; capfdbinary; doctest_namespace; request; pytestconfig; record_property; caplog; monkeypatch ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found