Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)
recategorized by

In my project I'm parsing some PDF files using pdfplumber. During tests execution (pytest) I sometimes would like to see logs from my code for debugging purposes. This can be done by setting --log-cli-level=DEBUG. However, this turns on messages from all code, also pdfplumber - which is very verbose and makes debugging difficult. Is there a way to selectively enable/disable loggers during test run?

pytest 4.6.3

python 3.7.3

Thanks for help!

1 Answer

0 votes
by (25.1k points)

You can do it by adding a custom command to you conftest.py to turn off specific loggers. Like this:

import pytest

import logging

def pytest_addoption(parser):

    """Add a command line option to disable logger."""

    parser.addoption(

        "--log-disable", action="append", default=[], help="disable specific loggers"

    )

def pytest_configure(config):

    """Disable the loggers."""

    for name in config.getoption("--log-disable", default=[]):

        logger = logging.getLogger(name)

        logger.propagate = False

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jun 26, 2020 in Python by Sudhir_1997 (55.6k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...