Skip to content

Configure Prefect logger to capture default logs #3126

@marvin-robot

Description

@marvin-robot

Opened from the Prefect Public Slack Community

akumar: Hi Team, How can make the prefect to log the messages from the default python logger to log to cloud, so for example If I get the logger via:

logger = logging.getLogger(__name__)

I have this in code everywhere and I don't want replace it with from prefect.utilities.logging import get_logger everywhere, is there a way to send the default logger messages to cloud?

josh: Hi <@U017DLT7H5L> you can access the logger that your task is using during the run like this:

@task
def my_task():
    from prefect import context
    logger = context.get("logger")
    <http://logger.info|logger.info>("log this")

Alternatively you could log stdout and that will be sent to the cloud logger:

@task(log_stdout=True)
def my_task():
    print("log this")

akumar: Hi Josh thanks for your reply, I have the default logger already everywhere in the code base, is there no way to use that without changing it to prefect logger?

akumar: As we would have to replace all of them with prefect logger and that code doesn't only runs on prefect, it is ran on other system as well.

josh: Oh I see, you can attach extra loggers to the prefect logger if you know the name of it like this https://docs.prefect.io/core/concepts/logging.html#extra-loggers

akumar: Yeah, I saw that but the name is not constant in this case logger: logging.getLogger(__name__) I guess, we need to use a common name then

josh: We’re looking to make some improvements to the logger and if you would like to open a ticket for this as a suggestion on the repo that would be great 🙂

akumar: Sure, will add it thanks. 🙂

akumar: Can you trigger the automated conversion of this conversation as a Github issue, then I can add more details if required?

josh: <@ULVA73B9P> open “Configure Prefect logger to capture default logs”

Original thread can be found here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions