<@ULVA73B9P> I have a global run_logger = get_run_...
# ask-marvin
k
@Marvin I have a global run_logger = get_run_logger() defined in my flow. I use this run_logger to log messages in my task. In the prefect server, they only appear in the flow run logs and not in the task run logs. Can I have them appear in both?
m
To have your logs appear in both the flow and task run logs, you should use the
get_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:
Copy code
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.