<@ULVA73B9P> I have the following code: ```@flow(l...
# ask-marvin
b
@Marvin I have the following code:
Copy code
@flow(log_prints=True)
def test_flow(logger):
    result = "This is a test flow. It doesn't actually do anything."
    <http://logger.info|logger.info>(result)
    return result


@flow
def test_flow_with_task(logger):
    test_task.test_task(logger)


@flow
def parent_flow_native_logging():
    logger = prefect_get_run_logger()
    <http://logger.info|logger.info>("Info message at beginning of parent_flow")
    logger.debug("Debug message at beginning of parent_flow")

    <http://logger.info|logger.info>("Calling child flow test_flow")
    test_flow(logger)

    <http://logger.info|logger.info>("Calling child flow test_flow_with_task")
    test_flow_with_task(logger)

@task
def test_task(logger):
    """A simple test task that returns a greeting message."""
    message = "Hello from test task!"
    <http://logger.info|logger.info>(message)
    return message
When I run the parent_flow_native_logging flow, the logs for the sub-flows only show up in the logs for the parent flow. How can I have the prefect UI show the logs for the sub-flows on their own page instead of mixed in with the parent flow logs?
m
thought for 108.4 seconds
You’re passing the parent flow’s logger down into your subflows and task. In Prefect 3.x, a logger returned by
get_run_logger()
is bound to the current run when it’s created. If you reuse the parent’s logger, all of those log records will be attributed to the parent flow run — so they only “live” on the parent’s page (and get aggregated there). Fix: don’t pass loggers around. Instead, call
get_run_logger()
inside each flow and task so logs are scoped to that specific run. Then the UI will show each subflow’s logs on its own run page, and still aggregate them into the parent view. Here’s your code adjusted accordingly:
Copy code
from prefect import flow, task, get_run_logger

@flow(log_prints=True)
def test_flow():
    logger = get_run_logger()
    result = "This is a test flow. It doesn't actually do anything."
    <http://logger.info|logger.info>(result)
    return result

@task
def test_task():
    logger = get_run_logger()
    message = "Hello from test task!"
    <http://logger.info|logger.info>(message)
    return message

@flow
def test_flow_with_task():
    logger = get_run_logger()
    <http://logger.info|logger.info>("Calling test_task")
    test_task()

@flow
def parent_flow_native_logging():
    logger = get_run_logger()
    <http://logger.info|logger.info>("Info message at beginning of parent_flow")
    logger.debug("Debug message at beginning of parent_flow")

    <http://logger.info|logger.info>("Calling child flow test_flow")
    test_flow()

    <http://logger.info|logger.info>("Calling child flow test_flow_with_task")
    test_flow_with_task()
Notes: - Each flow/task now obtains its own run-scoped logger, so their logs will appear on their own run pages. - The parent flow run page will still show an aggregated view (by design). To see only the subflow’s logs, click into the subflow node in the run graph or timeline to open its run page. Docs for more detail: - Add logging in Prefect 3 - Workflow design patterns (subflows)