<@ULVA73B9P> Is this true: Flow logs all task log...
# ask-marvin
c
@Marvin Is this true: Flow logs all task logs: This is Prefect's design. The flow run log view aggregates logs from all child task runs. There's no way to make a log appear in a task view but not in the parent flow view. The fix: switch per-epoch logging from tlogger.info() to print() and set log_prints=False on the trial task. Per-epoch goes to stdout/container logs only. Only the final "Finished" message uses the Prefect logger (1 line per trial in the flow view).
m
thought for 139.5 seconds
Short answer: mostly true, with one useful nuance. - Yes: In Prefect 3.x, the flow run log view aggregates logs from the flow and all child task runs. If a log is sent to the Prefect API with a
flow_run_id
(which task logs are), it will appear in the parent flow’s logs. - No: There isn’t a way to make a log appear in a task run’s UI view but not in the parent flow’s UI view. Once it’s in the API, it shows up in both scopes. - Your workaround is valid: use
print()
for high-frequency/per-epoch messages and set
log_prints=False
on that task so those prints stay in stdout/container logs only. Use the Prefect logger just for the final summary line. Example
Copy code
from prefect import flow, task
from prefect.logging import get_run_logger

@task(log_prints=False)  # prints won't be captured by Prefect
def trial():
    for epoch in range(5):
        print(f"epoch {epoch} metrics...")  # stdout-only, not in Prefect UI
    logger = get_run_logger()
    <http://logger.info|logger.info>("Finished trial with best metric=...")  # 1 line in UI

@flow
def orchestrate():
    trial()

if __name__ == "__main__":
    orchestrate()
Optional nuance (if you prefer not to switch to print): - You can keep using the Prefect logger but prevent specific records from going to the API by setting
extra={"send_to_api": False}
. Those logs will still go to any local/console handler but won’t appear in the Prefect UI at either the task or flow level.
Copy code
from prefect.logging import get_run_logger

@task
def trial():
    logger = get_run_logger()
    # High-frequency logs: console-only; not sent to Prefect API/UI
    <http://logger.info|logger.info>("epoch details ...", extra={"send_to_api": False})
    # Final summary: sent to Prefect API/UI
    <http://logger.info|logger.info>("Finished trial with best metric=...")
Notes on `log_prints`: -
log_prints=True
patches
print()
to the Prefect logger (so prints appear in the UI). -
log_prints=False
leaves prints on stdout only. - A task’s explicit
log_prints
overrides the flow’s setting; if left as
None
, it inherits.