Thread
#prefect-community
    John Ramey

    John Ramey

    6 months ago
    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.")
    Kevin Kho

    Kevin Kho

    6 months ago
    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
    John Ramey

    John Ramey

    6 months ago
    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
    Kevin Kho

    Kevin Kho

    6 months ago
    Btw you can also use
    @task(log_stdout=True)
    def some_task():
        print("something")
    and I think it should work
    John Ramey

    John Ramey

    6 months ago
    got it, thanks!