https://prefect.io logo
Title
j

John Ramey

03/24/2022, 8:54 PM
Question about logging best practices. In the docs (see below), a logger is defined within a task with
prefect.context.get("logger")
. Does a logger have to be defined within each task? This gets a bit repetitive if I have 5-10 tasks within a flow. Is there a more concise way to define loggers across tasks?
import prefect

@task
def my_task():
    logger = prefect.context.get("logger")

    <http://logger.info|logger.info>("An info message.")
    logger.warning("A warning message.")
k

Kevin Kho

03/24/2022, 9:12 PM
Yes unless you use Script Based Storage (Git, S3, etc.) where the Flow code is executed during runtime. I think you can have the logger outside.
For pickle based storage yes you need it
j

John Ramey

03/24/2022, 9:15 PM
oh lol i answered my own question as you answered
within a script, i moved
logger = prefect.context.get("logger")
to the top to make it global. it just worked across all the tasks
k

Kevin Kho

03/24/2022, 9:16 PM
Btw you can also use
@task(log_stdout=True)
def some_task():
    print("something")
and I think it should work
j

John Ramey

03/24/2022, 9:16 PM
got it, thanks!