pytest-dev / pytest-dev/pytest

Feature request: use caplog with custom formatter

Open
#2,987 10 comments 12 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

plugin: logging type: proposal
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

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()

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at _pytest.logging.LoggingPlugin and the pytest_configure workaround shown in the issue. Trace how the current formatter configuration is exposed, then define a supported way for applications to provide a custom formatter without replacing the plugin class. Done means the custom formatter can be supplied through the supported interface.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.