Ji-Oh Yoo
08/19/2024, 9:33 PMMarvin
08/19/2024, 9:33 PMpython
import logging
from prefect import task, flow, get_run_logger
from prefect.context import TaskRunContext
# Configure default logger
default_logger = logging.getLogger(__name__)
logging.basicConfig(level=<http://logging.INFO|logging.INFO>)
def get_logger():
# Check if running within a Prefect task
if TaskRunContext.get():
return get_run_logger()
return default_logger
# Sample task and flow using the shared logger function
@task
def sample_task():
logger = get_logger()
<http://logger.info|logger.info>("This is a log from within a Prefect Task")
@flow
def sample_flow():
logger = get_logger()
<http://logger.info|logger.info>("This is a log from within a Prefect Flow")
sample_task()
# Test logging outside Prefect context
if __name__ == "__main__":
logger = get_logger()
<http://logger.info|logger.info>("This is a log from outside Prefect context")
sample_flow()
### Summary:
- Use TaskRunContext.get()
to detect if the code is running within a Prefect task.
- Switch to Prefect's logger (get_run_logger()
) when within a task or flow.
- Default to the standard Python logger otherwise.
### References:
- Prefect Logging Guide: Prefect Docs - Logging
- Example issue discussing logger context: GitHub Issue #9322
This approach ensures that your shared functions use the appropriate logger based on the execution context.