Tate Wilks
09/26/2024, 7:01 PMMarvin
09/26/2024, 7:02 PMlogging.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:
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:
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