Search code examples
pythonprefect

using logger for prefect Tasks like prefect.tasks.database.sqlite.SQLiteQuery


How can I add a logger to prefect's prefect.tasks.database.sqlite.SQLiteQuery class just like with the @task decorator e.g.:

from prefect import task, Flow
import prefect
from time import sleep


@task()
def some_task():
    logger = prefect.context.get("logger")
    logger.info("Let's sleep a second!")
    sleep(1)


version_check = prefect.tasks.database.sqlite.SQLiteQuery(
    db="sqlite.db",
    query="Select sqlite_version()",
)


with Flow("a flow") as flow:

    some_task()
    print(version_check)


if __name__ == "__main__":

    flow.run()

Currently, only some_task is logged. How can I log precast tasks like SQLiteQuery ? (prefect 1.0 family)


Solution

  • I think the issue here is that version_check is not called in the Flow? You can try:

    with Flow("a flow") as flow:
    
        some_task()
        version_check()
    

    In the current form, it is just initialized but not added to the flow. You also can't print the output with the standard python print. The print will just print the Task class. You need to log from inside a task.

    @task
    def log_something(x):
        prefect.context.logger.info(x)
        return
    

    and then use it in the flow:

    with Flow("a flow") as flow:
    
        some_task()
        x = version_check()
        log_something(x)