Is there a preferred logging approach from within ...
# ask-community
j
Is there a preferred logging approach from within callbacks? Use case is an
on_execute()
(or the existing
on_start()
) called from an Environment. I can pass the environment's
self.logger
-- that seems clunky, but the following doesn't seem to work:
Copy code
def on_execute(parameters: Dict[str, Any], provider_kwargs: Dict[str, Any]) -> None:
    logger = prefect.context.get("logger")
    <http://logger.info|logger.info>("Checking Flow run parameters: {}".format(parameters))
đź‘€ 1
c
Good question; in this case you’ll want to create a new logger:
Copy code
logger = prefect.utilities.logging.get_logger("Joe's Cool Logger")
Some background / behind the scenes info here:
context
is (generally speaking) only populated by Flow / Task Runners, so there are many keys in context that only available after the Runners have been instantiated. Environments and storage are two of the only places that run code outside of these runner pipelines
j
Cool cool, that works. Any certain convention on naming? Maybe the usual:
Copy code
logger = prefect.utilities.logging.get_logger(__name__)
c
We usually name loggers based on the class they are attached to but ultimately it’s up to you in this case; because agents ensure flow run id is always available in the environment these logs will be associated with your flow run (in the UI and via any queries) regardless of what you call it
👍 1
@Marvin archive “How should I create logs within on start or on exit environment callbacks?”