Vincent Rubingh
08/28/2024, 7:40 PMMarvin
08/28/2024, 7:40 PMlog_prints
parameter:
python
from prefect import flow
@flow(log_prints=True) # This sends `print` statements as INFO logs
def my_workflow():
print("Logging from inside the Docker container")
3. Retrieve and stream logs:
Prefect provides functionality to get logs similar to the docker logs
command. Here's an abstracted approach based on provided excerpts:
python
from prefect import flow, task
import docker
@task
def run_container_with_runtime(ctx):
client = docker.from_env()
container = client.containers.run(
image="your-docker-image",
command="your-command",
detach=True,
environment={
"PREFECT_API_URL": "your-prefect-api-url",
"PREFECT_TASK_RUN_ID": ctx.task_run.id
}
)
for log in container.logs(stream=True):
print(log.strip().decode('utf-8')) # or use any preferted logging approach
@flow
def my_flow():
context = get_runtime_context()
run_container_with_runtime(context)
if __name__ == "__main__":
my_flow()
Using these steps, you can ensure that the logs inside the Docker container are captured and appropriately sent to the Prefect server, thus allowing real-time logging.
For more detailed information, refer to the Prefect documentation:
- Prefect Docker Integration
- Prefect AWS ECS Integration