https://prefect.io logo
c

Christopher Boyd

09/13/2023, 2:21 PM
Passing a logging object:
Copy code
from prefect import get_run_logger

@task
get_weather():
    logger = get_run_logger()
    <http://logger.info|logger.info>(f"{value}")
    not_a_task(logger)

not_a_task(log_obj):
    <http://log_obj.info|log_obj.info>("hello world")
d

David Maxson

09/13/2023, 2:27 PM
Passing log objects around is not really a good solution, it's very non-standard.
Copy code
import logging
logging.basicConfig(level=<http://logging.INFO|logging.INFO>)

def some_external_func(x, y, z):
    <http://logging.info|logging.info>("...")  # Use the logging module directly
    # or...
    # logger = logging.getLogger(__name__)
    <http://logger.info|logger.info>("...")


def non_prefect_driver():
    # Logs get printed as expected, assuming we've configured logging as normal
    some_external_func(1, 2, 3)


@flow
def prefect_driver():
    # Logs get printed locally, but are not sent to API server
    some_external_func(1, 2, 3)
    
    # Could probably add the run logger as a handler to the built-in logger?
    logging.getLogger().addHandler(get_run_logger().handlers[0])