darwin
07/02/2024, 9:00 AMMarvin
07/02/2024, 9:00 AMpython
from prefect import flow, task, get_run_logger
from loguru import logger
# Define a sink function for Loguru to send logs to Prefect
def prefect_log_sink(msg):
logger = get_run_logger()
level_method = getattr(logger, msg.record["level"].name.lower(), "info")
level_method(msg)
# Configure Loguru to send logs to the Prefect logger
logger.remove() # Remove the default Loguru logging to stderr
logger.add(prefect_log_sink)
@task
def my_task():
<http://logger.info|logger.info>("This is a log from Loguru inside a Prefect task.")
@flow
def my_flow():
my_task()
if __name__ == "__main__":
my_flow()
In this example:
- A custom sink function prefect_log_sink
is defined to direct Loguru logs to Prefect.
- Loguru is configured to use this custom sink, so all Loguru logs within the flow will be directed to Prefect's task logs.
2. Run your Prefect flow:
When you run your flow, Loguru logs should now appear in the Prefect UI under the task logs.
For more detailed guidance, you can refer to an open issue discussing the integration of a hybrid logger in Prefect here.
This solution should allow you to see Loguru logs within your Prefect task logs effectively. If you need further customization or have additional questions, feel free to ask.