https://prefect.io logo
Title
a

Ashe

05/18/2023, 2:40 PM
Prefect is super cool! I have a logging question (after reading through: https://docs.prefect.io/latest/concepts/logs/?h=logging) We have this logger defined but are not seeing logs in UI - any way to easily set from this?
def get_logger(name):
    logger = logging.getLogger(name)
    logger.setLevel(logging.DEBUG)  # Or any level you want

    # Create a console handler
    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)  # 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
any help much appreciated - have been stuck on this for a while and Marvin/bots don’t know o.0 🙏
n

Nate

05/18/2023, 3:40 PM
hi @Ashe! have you checked out configuring this via this setting?
PREFECT_LOGGING_SETTINGS_PATH=PosixPath('${PREFECT_HOME}/logging.yml')
just as a note, you can see what logging settings are available like
❯ ipython
Python 3.11.0 | packaged by conda-forge | (main, Jan 14 2023, 12:25:12) [Clang 14.0.6 ]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.12.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import prefect
10:42:05.186 | DEBUG   | prefect.profiles - Using profile 'pong'

In [2]: prefect.settings.get_current_settings() # and ctrl-F for "log"
r

Robert Banick

05/18/2023, 4:05 PM
Hi @Ashe we had a similar problem. The key is to add an
APILogHandler
to your logger — this will pipe its outputs to the UI just like a
run_logger
.
from prefect.logging.handlers import APILogHandler

def get_logger(name):
     # your code
     api_handler = APILogHandler()
     logger.addHandler(api_handler)

     return logger
🙌 1
a

Ashe

05/22/2023, 3:58 PM
@Robert Banick how did you mute this when not running a flow locally?
r

Robert Banick

05/22/2023, 3:59 PM
I’m not sure I understand what you mean by “mute” — you mean turn off the logger?
a

Ashe

05/22/2023, 4:00 PM
since
get_logger
is global for me, when I’m not running a flow and testing other parts of the code base locally I get
attempted to send logs to the API without a flow run id. The API log handler can only send logs within flow run contexts unless the flow run id is manually provided.
wondering if you found a clever way to only add this handler when it knows its a “flow”
r

Robert Banick

05/22/2023, 5:10 PM
ah yeah I don’t do much testing of flows locally (our problems are usually server-side) so I haven’t encountered this
let me know what you find!
n

Nate

05/22/2023, 5:49 PM
perhaps this might help? otherwise, feel free to share more about the scenario where you want to disable logs