Koen
01/06/2025, 1:33 PMMarvin
01/06/2025, 1:33 PMget_run_logger()
function within the task itself, rather than defining it globally in the flow. This ensures that the logger is aware of the current task run context, allowing for more detailed and contextual logging.
Here's a brief example of how you might structure your task to achieve this:
python
from prefect import task, flow, get_run_logger
@task
def my_task():
logger = get_run_logger()
<http://logger.info|logger.info>("This is a task log message.")
@flow
def my_flow():
my_task()
if __name__ == "__main__":
my_flow()
In this example, get_run_logger()
is called within the task, ensuring that the logger is contextually aware of the task run and logs messages accordingly. You can explore more about logging in Prefect here.