https://prefect.io logo
Title
a

Ashe

05/18/2023, 3:25 PM
@Marvin this
logger.py
is not resulting in logs in prefect UI locally - what’s wrong?
import logging
import os
import sys

import prefect
from prefect.logging.loggers import get_run_logger

def get_logger(name):
    logger = logging.getLogger(name)
    logger.setLevel(<http://logging.INFO|logging.INFO>)  # Or any level you want
    os.environ["PREFECT__LOGGING__LEVEL"] = "INFO"
    os.environ["PREFECT__LOGGING__ROOT_LEVEL"] = "INFO"

    # Create a console handler
    ch = logging.StreamHandler()
    ch.setLevel(<http://logging.INFO|logging.INFO>)  # Or any level you want

    # Create a formatter
    formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")

    # Add the formatter to the handler
    ch.setFormatter(formatter)

    # Add the handler to the logger
    logger.addHandler(ch)

    return logger
m

Marvin

05/18/2023, 3:27 PM
Ah, I see. It appears that your logger configuration might not be properly integrated with Prefect's logging system. Prefect has its own logging settings and configuration, which you can find in their documentation. To ensure your logs appear in the Prefect UI, you should use Prefect's built-in logging system. You can get the Prefect logger by calling
prefect.utilities.logging.get_logger()
. Here's an example of how to use Prefect's logger:
python
import prefect
from prefect import task, Flow

logger = prefect.utilities.logging.get_logger()

@task
def log_something():
    <http://logger.info|logger.info>("This is an info log message.")
    logger.warning("This is a warning log message.")
    logger.error("This is an error log message.")

with Flow("logging_example") as flow:
    log_something()

flow.run()
In this example, we import Prefect's logger and use it within a task. This should ensure that your logs appear in the Prefect UI as expected. I hope this helps you find the light at the end of the log tunnel. Remember, logs are like breadcrumbs for humans, so don't get lost in the forest.
a

Ashe

05/18/2023, 3:27 PM
AttributeError: module 'prefect.utilities' has no attribute 'logging'