<@ULVA73B9P> I want to see log from external libra...
# marvin-ai
r
@Marvin I want to see log from external library in UI. External library is used by Prefect flow. I tryed this technique : app_logger = logging.getLogger("pycdt") app_logger.setLevel(logging.INFO) app_logger.addHandler(APILogHandler()) But now I see all messages logged twice. Why ?
1
m
thought for 255.1 seconds
Short answer: you’re getting duplicates because your logger is being handled twice. What’s happening in Prefect 3.x: - Prefect sends logs to the UI via an APILogHandler that it attaches automatically to Prefect’s run loggers and to any “extra” loggers you list in configuration (PREFECT_LOGGING_EXTRA_LOGGERS or logging.yml). - When you also add APILogHandler manually to your library logger (pycdt), you end up with two APILogHandler instances attached to that logger. Each log record is emitted twice → duplicates in the UI. - Duplicates can also occur if your logger has a handler and still propagates to a parent that also has a handler. Recommended fix (no code changes in your library): 1) Configure Prefect to capture your external logger once: - One time via env/profile:
Copy code
prefect config set PREFECT_LOGGING_EXTRA_LOGGERS="pycdt"
- Or set the env var:
Copy code
export PREFECT_LOGGING_EXTRA_LOGGERS=pycdt
2) Remove your manual handler addition:
Copy code
# Do not do this — it causes duplicates if Prefect is also attaching a handler
# app_logger.addHandler(APILogHandler())
3) Just use the logger normally:
Copy code
app_logger = logging.getLogger("pycdt")
app_logger.setLevel(<http://logging.INFO|logging.INFO>)
<http://app_logger.info|app_logger.info>("hello")  # will appear once in the Prefect UI
If you must add the handler in code, guard against duplicates and disable propagation:
Copy code
from prefect.logging.handlers import APILogHandler
import logging

app_logger = logging.getLogger("pycdt")
app_logger.propagate = False
if not any(isinstance(h, APILogHandler) for h in app_logger.handlers):
    app_logger.addHandler(APILogHandler())
Docs and source: - Logging customization (includes EXTRA_LOGGERS): How to customize Prefect logging - APILogHandler reference: prefect.logging.handlers - Default logging configuration code: configuration.py, logging.yml Quick check to confirm the cause: - Run your flow once, then inspect handlers at runtime:
Copy code
logger = logging.getLogger("pycdt")
print(logger.handlers)  # If you see two APILogHandler instances, that’s the duplication.
Question to confirm next step: - Are the duplicates showing up in the Prefect UI, your console, or both? And do you have PREFECT_LOGGING_EXTRA_LOGGERS set or a custom logging.yml in your Prefect profile?