Hi, Is it possible to output the logs also to a fi...
# ask-community
s
Hi, Is it possible to output the logs also to a file? thanks
j
Hi @simone do you mean all prefect logs or only logs from within your task? If doing the latter you could do something like this:
Copy code
import logging

import prefect
from prefect import task, Flow

@task
def log_to_file():
    logger = prefect.context.get("logger")
    file_handler = logging.FileHandler('my.log')
    file_handler.setLevel(logging.DEBUG)
    logger.addHandler(file_handler)

    <http://logger.info|logger.info>("I am log.")

f = Flow("log-file", tasks=[log_to_file])
f.run()
s
Hi Josh, thanks a lot! works great!