prefect icon indicating copy to clipboard operation
prefect copied to clipboard

Add example to docs of setting PREFECT_LOGGING_EXTRA_LOGGERS=app

Open discdiver opened this issue 3 years ago • 4 comments

First check

  • [X] I added a descriptive title to this issue.
  • [X] I used the GitHub search to find a similar request and didn't find it.
  • [X] I searched the Prefect documentation for this feature.

Prefect Version

2.x

Describe the current behavior

Then users don't have to use get_run_logger in every task. Helps keep code DRY.

Would be nice to add to the logging section of the docs. Example:

env PREFECT_LOGGING_EXTRA_LOGGERS=app 
from prefect import flow, task
import logging

logger = logging.getLogger("app")


@task
def t1():
    print("hi")
    logger.info("Task time?")


@flow
def f1():
    t1()
    logger.info("Flow time")


f1()

Describe the proposed behavior

Include the example above with a little discussion.

Example Use

No response

Additional context

Example from Community Slack: https://linen.prefect.io/t/3323130/must-get-run-logger-be-called-in-each-task-flow-i-have-a-flo

discdiver avatar Oct 20 '22 20:10 discdiver

While I agree that we should have an example of how to use the extra loggers setting, it is intended to target people who have existing loggers.

We should introduce a logger that you can import that just works in tasks/flows without calling get_run_logger to solve the root issue here. I've seen this request several times now in issues here. We only did it this way initially to prevent premature optimization of the interface.

zanieb avatar Oct 20 '22 20:10 zanieb

Sounds great to me. Adding ability to use the same logger inside and outside of flows and tasks could be part of that work, too, I imagine.

discdiver avatar Oct 20 '22 21:10 discdiver

Yeah it'd be

from prefect import logger

logger.info("Local only, not sent to Cloud")

@flow
def foo():
   logger.info("Logged to flow run")
   bar()

@task
def bar()
   logger.info("Logged to task run")

bar.fn()  # Logs locally, not to Cloud

zanieb avatar Oct 20 '22 21:10 zanieb

I don't have anything to contribute, but thanks @discdiver for adding a ticket so quickly based on my Slack message!

ramhiser avatar Oct 20 '22 21:10 ramhiser

This issue is stale because it has been open 30 days with no activity. To keep this issue open remove stale label or comment.

github-actions[bot] avatar Mar 30 '23 19:03 github-actions[bot]

This issue was closed because it has been stale for 14 days with no activity. If this issue is important or you have more to add feel free to re-open it.

github-actions[bot] avatar Apr 13 '23 19:04 github-actions[bot]