Skip to content

Live Logs don't print stdout or stderr to file #13432

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

Closed
illeatmyhat opened this issue May 20, 2025 · 4 comments
Closed

Live Logs don't print stdout or stderr to file #13432

illeatmyhat opened this issue May 20, 2025 · 4 comments
Labels
status: needs information reporter needs to provide more information; can be closed after 2 or more weeks of inactivity

Comments

@illeatmyhat
Copy link

illeatmyhat commented May 20, 2025

I want to print the captured stdout, stderr and other loggers to different files for each individual test.
So I wrote a hook which changes log_file for every test.

@pytest.hookimpl
def pytest_runtest_setup(item):
    logging_plugin = item.config.pluginmanager.get_plugin("logging-plugin")
    timestamp = datetime.strftime(datetime.now(), '%Y-%m-%d_%H-%M-%S')
    logging_plugin.set_log_path(f'logs'/{item.name}_{timestamp}.log')

with a test that only raises an exception

def test_foobar():
    raise Exception("foobar")

In pyproject.toml I played around with various options for --capture=... but it didn't matter

[tool.pytest.ini_options]
log_cli = true

all I get is an empty file.

I expected to see the exception and the full stack trace.

pytest==8.3.4
macOS Sonoma 14.5 (23F79)

@illeatmyhat
Copy link
Author

For anyone who really only cares about the final exception trace, and not any stray print() statements, you can print it out like this:

test_start = pytest.StashKey[datetime]()

@pytest.hookimpl
def pytest_collection_modifyitems(config, items):
    for item in items:
        item.stash[test_start] = datetime.strftime(datetime.now(), '%Y-%m-%d_%H-%M-%S')


@pytest.hookimpl
def pytest_runtest_setup(item):
    logging_plugin = item.config.pluginmanager.get_plugin("logging-plugin")
    log_file = Path('logs') / item.stash[test_start] / f'{item.name}.log'
    logging_plugin.set_log_path(log_file)


@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    result: TestReport = outcome.get_result()
    log_file = Path('logs') / item.stash[test_start] / f'{item.name}.log'
    if result.when == 'call' and result.failed:
        filepath, lineno, testname = result.location
        with log_file.open('a') as f:
            f.write(f'{filepath}:{lineno} ({testname})\n{result.longreprtext}')

@The-Compiler
Copy link
Member

I don't understand how this would be a bug. The logging plugin handles Python logging. Exceptions are a different Python feature and don't magically end up as a logging record.

@The-Compiler The-Compiler added the status: needs information reporter needs to provide more information; can be closed after 2 or more weeks of inactivity label Jun 6, 2025
@illeatmyhat
Copy link
Author

illeatmyhat commented Jun 6, 2025

Is that not besides the point?
What is the objective behind live logs and the logging plugin if not to provide developers with all of the information they need to determine why a test failed or reached an undesired state?
If exceptions don't get stored then what even is the point?
Am I supposed to tell people, "sorry you used a print() statement instead of logging.info like you should have so good luck figuring out your problem?"

I thought this feature was supposed to be better than pytest -s > file.log

@The-Compiler
Copy link
Member

The point of the feature is seeing logging output from Python (usually generated by your application) as that's happening. It's not intended to give you a log of the test run.

@The-Compiler The-Compiler closed this as not planned Won't fix, can't repro, duplicate, stale Jun 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs information reporter needs to provide more information; can be closed after 2 or more weeks of inactivity
Projects
None yet
Development

No branches or pull requests

2 participants