Question about logging best practices. In the docs...
# prefect-community
j
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?
Copy code
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
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
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
Btw you can also use
Copy code
@task(log_stdout=True)
def some_task():
    print("something")
and I think it should work
j
got it, thanks!