Good day, Have a question about logging. We have a...
# ask-community
i
Good day, Have a question about logging. We have a separate own library "LIB" that is doing some magic. This library has own root logger defined
logger = logging.getLogger()
. The idea is to extend Prefect Task class and run some magic using LIB library. I've read that we can add as many NAMED loggers as we want using https://docs.prefect.io/core/concepts/logging.html#extra-loggers, but since with have root one, what happens now is: Prefect logs its records as usual, but messages from LIB are not passed. I suppose this is due to LIB logger is root one. Have you ever faced such situation?
a
@Ievgenii Martynenko what would be your end goal with this custom logger? If your end goal is to have those logs displayed in the Prefect UI or in logs of your execution layer, then adding it as extra loggers, as described in those docs should work with some configuration up-front. However, if your goal is formatting (i.e. to have logs displayed differently in the Prefect UI), then this won’t work, because the UI renders all logs in the same way regardless of how those logs are formatted in your code. Therefore, the easiest way to provide logs to your flow is to use the logger from the context, since it’s all rendered the same way anyways:
Copy code
logger = prefect.context.get("logger")
i
The goal is just to show messages generated from internal LIB. Does it matter whether internal logger is named or root?
In example from docs, you are adding "named" loggers, while ours is not.
a
To get root logger, you would leave the name empty
logging.getLogger()
. You can try it out:
Copy code
import prefect
import logging

@task
def do_something():
    root_logger = logging.getLogger()  # get your root logger
    logger = prefect.context.get("logger")
    logger.addHandler(root_logger)
i
ah, make sense, approach is the same. Thanks
👍 1