Question for logs : I'm running a job in a contain...
# prefect-community
a
Question for logs : I'm running a job in a container ( I'm using this example : https://docs.prefect.io/core/examples/functional_docker.html ) : I remove last part and add flow.register() - In UI, in log part, I can logs
May 10th 2020 at 4:48:48pm | prefect.CloudTaskRunner
INFO
Task 'GetContainerLogs': finished task run for task with final state: 'Success'
which is cool 🙂 but I don't have any print done by my job itself - now if I check log file (in folder .prefect/results) in console I can see all prints done by my job - Do you know how I can have it in UI ?
j
Hi @Adrien Boutreau - if you want to log stdout such as print(), you can set "log_stdout" to true. For example:
Copy code
from prefect import task, Flow

@task(log_stdout=True)
def my_task():
    print("This will be logged!")

flow = Flow("log-stdout", tasks=[my_task])
You can see more about logging in the docs here: https://docs.prefect.io/core/idioms/logging.html#logging-configuration-and-usage
Hope that helps!
a
Copy code
container = CreateContainer(
    image_name="img:latest",
    command='''python myjob.py''',
)
start = StartContainer()
logs = GetContainerLogs(trigger=always_run)
status_code = WaitOnContainer()


schedule = IntervalSchedule(interval=timedelta(hours=1))
#schedule = Schedule(clocks=[CronClock("0 7 * * *")])

with Flow("flow", schedule) as flow:
    start_container = start(container_id=container)
    code = status_code(container_id=container, upstream_tasks=[start_container])
    collect_logs = logs(container_id=container, upstream_tasks=[code])

## run flow and print logs
flow.register()
so my task is inside a container - how can I do that ?
j
@Adrien Boutreau The
GetContainerLogs
task in the task library returns a result that looks like this:
Copy code
api_result = client.logs(container=container_id).decode()
Since the logs are retrieved you should add a downstream task that does something with the result of that task where you can filter, save, output, etc.