<@ULVA73B9P> How do I configure logging from a sub...
# ask-marvin
t
@Marvin How do I configure logging from a subflow to propagate to the parent flow so that it is visible in the UI?
m
It seems there are differences in how logging is configured across Prefect versions. Here's a concise guide for both Prefect 2.x and 3.x. ### Prefect 2.x 1. Central Logging Configuration: Prefect allows you to customize logging by creating or modifying a
logging.yml
file. This configuration can then be used by defining the environment variable
PREFECT_LOGGING_CONFIG
. 2. Subflows and Log Propagation: The relationship between parent and child flows involves creating a special task run in the parent flow. This task mirrors the state of the child flow. Example:
Copy code
python
   from prefect import flow, task

   @task(log_prints=True)
   def print_hello(name: str):
       print(f"Hello {name}!")

   @flow(name="Subflow")
   def my_subflow(msg: str):
       print(f"Subflow says: {msg}")

   @flow(name="Hello Flow")
   def hello_world(name="world"):
       message = print_hello(name)
       my_subflow(message)
   
   if __name__ == "__main__":
       hello_world("Marvin")
log_prints=True
ensures print statements are converted to logs. ### Prefect 3.x 1. Logging Configuration in Flows and Tasks: In Prefect 3.x, the
get_run_logger
function is used to obtain a logger instance that is aware of the current flow or task run context. This makes the integration of detailed logs straightforward. 2. Subflow Logging: Logs from subflows are automatically handled by Prefect's logging system and can be viewed in the UI. Example:
Copy code
python
   from prefect import flow, task, get_run_logger

   @task
   def print_hello(name: str):
       logger = get_run_logger() 
       <http://logger.info|logger.info>(f"Hello {name}!")

   @flow
   def my_subflow(msg: str):
       logger = get_run_logger()
       <http://logger.info|logger.info>(f"Subflow says: {msg}")

   @flow
   def hello_world(name="world"):
       message = print_hello(name)
       my_subflow(message)
   
   if __name__ == "__main__":
       hello_world("Marvin")
Here,
get_run_logger
is used to emit log messages which can be viewed in the UI for both parent and subflows. For more detailed information, refer to the respective Prefect documentation: - Prefect 2.x: Prefect 2.x Logging Guide - Prefect 3.x: Prefect 3.x Logging Guide