Is there a preferred logging approach from within ...
# prefect-community
Is there a preferred logging approach from within callbacks? Use case is an
(or the existing
) called from an Environment. I can pass the environment's
-- 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")
    <|>("Checking Flow run parameters: {}".format(parameters))
👀 1
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:
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
Cool cool, that works. Any certain convention on naming? Maybe the usual:
Copy code
logger = prefect.utilities.logging.get_logger(__name__)
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?”