https://prefect.io logo
Title
j

Joe Schmid

04/25/2020, 3:50 AM
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:
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

Chris White

04/25/2020, 3:55 AM
Good question; in this case you’ll want to create a new logger:
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

Joe Schmid

04/25/2020, 3:57 AM
Cool cool, that works. Any certain convention on naming? Maybe the usual:
logger = prefect.utilities.logging.get_logger(__name__)
c

Chris White

04/25/2020, 3:59 AM
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?”