Niko
10/23/2025, 10:06 PMMarvin
10/23/2025, 10:06 PMMarvin
10/23/2025, 10:09 PMdisable_run_logger, you’ve turned off the APILogHandler (logs-to-API), which is great for tests. To see get_run_logger().info() in your terminal, just add a console handler to Prefect’s run loggers during tests.
Here are a few options; pick the one that best fits your setup.
Option A: Add a console handler in an autouse fixture (recommended)
- Keeps API run logging disabled
- Prints flow/task run logs to the terminal during tests
import logging
import pytest
from prefect.logging import disable_run_logger
from prefect.logging.handlers import PrefectConsoleHandler
@pytest.fixture(autouse=True)
def prefect_logs_to_terminal():
# Disable sending run logs to the API
with disable_run_logger():
# Add a console handler so run logs show up in the test terminal
for name in ("prefect.flow_runs", "prefect.task_runs"):
logger = logging.getLogger(name)
# avoid duplicates if tests reconfigure logging
if not any(isinstance(h, PrefectConsoleHandler) for h in logger.handlers):
logger.addHandler(PrefectConsoleHandler())
logger.setLevel(<http://logging.INFO|logging.INFO>)
logger.propagate = False # avoids duplicate logs via root
yield
Option B: Use pytest’s built-in live logging
- Useful if you prefer pytest to manage printing
- Make sure Prefect logs propagate to the root logger
1) Enable live log output in pytest:
- Via CLI: pytest -o log_cli=true --log-cli-level=INFO
- Or in pytest.ini:
[pytest]
log_cli = true
log_cli_level = INFO
2) Ensure Prefect run logs propagate to the root logger:
import logging
import pytest
from prefect.logging import disable_run_logger
@pytest.fixture(autouse=True)
def route_prefect_logs_to_pytest_cli():
with disable_run_logger():
for name in ("prefect.flow_runs", "prefect.task_runs"):
logging.getLogger(name).setLevel(<http://logging.INFO|logging.INFO>)
logging.getLogger(name).propagate = True
yield
Option C: Capture logs for assertions and still see them
- Uses Prefect’s testing helper to capture run logs
- Combine with pytest live logging if you want them printed during runs
import logging
import pytest
from prefect.logging import disable_run_logger
from prefect.testing.fixtures import add_prefect_loggers_to_caplog
@pytest.fixture(autouse=True)
def capture_prefect_logs(caplog):
with disable_run_logger():
add_prefect_loggers_to_caplog(caplog)
caplog.set_level(<http://logging.INFO|logging.INFO>)
yield
Then enable live logging with pytest -o log_cli=true --log-cli-level=INFO (or via pytest.ini) so they show in the terminal too.
Notes
- The functions/classes referenced above are available in Prefect 3.x:
- Run loggers and helpers: prefect.logging.loggers (API)
- Handlers: prefect.logging.handlers (API)
- Pytest helper: prefect.testing.fixtures (API)
- General logging customization: Logging customization guide
If you prefer to control everything via a logging config file (and optionally send to both API + console for tests), you can also point PREFECT_LOGGING_SETTINGS_PATH at a test-specific YAML that includes both APILogHandler and PrefectConsoleHandler. Happy to share a minimal YAML if that’s helpful.
Do you want the logs printed for all tests, or only on failures? I can tailor the fixture for either behavior.