<@ULVA73B9P> how do i save logs generated by logur...
# marvin-in-the-wild
d
@Marvin how do i save logs generated by loguru in imported functions be saved to task logs in prefect server
m
To save logs generated by Loguru in imported functions into Prefect task logs, you can configure a hybrid logger that will work both within a Prefect context and outside of it. Here's a solution that might help you achieve that: 1. Set up Loguru to use Prefect's logger when within a flow or task run: You can modify how Loguru handles logging within Prefect tasks by leveraging Prefect's logger. Here's a conceptual example:
Copy code
python
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.