<@ULVA73B9P> how to print loggings from task or fr...
# marvin-in-the-wild
j
@Marvin how to print loggings from task or from flow that are in the code?
m
To print logging from tasks or flows in Prefect, you can use the
get_run_logger
method for retrieving a logger instance, then call the standard Python logging methods. Here are more specific examples for both tasks and flows: Logging in Tasks
Copy code
python
from prefect import flow, task, get_run_logger

@task(name="log-example-task")
def logger_task():
    logger = get_run_logger()
    <http://logger.info|logger.info>("INFO level log message from a task.")

@flow(name="log-example-flow")
def logger_flow():
    logger_task()

logger_flow()
Reference Logging in Flows
Copy code
python
from prefect import flow

@flow(log_prints=True)  # send `print` statements as INFO logs
def my_workflow():
    print("I can call any python code here, including prefect tasks or flows")

if __name__ == "__main__":
    my_workflow()
Code Example
j
@Marvin what if I am calling another script and this script has loggigs?
Copy code
@task(name="log-example-task")
def logger_task():
m
If you want to capture logs from an external script called within a Prefect task, you can configure Python's logging module to handle those logs. Here's a step-by-step method: 1. Configure Logging in the External Script: Ensure the external script uses Python's logging module to emit log messages. 2. Invoke the External Script: Call the external script from your Prefect task. 3. Redirect the Script’s Logs: Ensure the logs from the external script are captured and directed to Prefect's logging system. Here's an example: External Script (external_script.py)
Copy code
python
import logging

# Set up logging configuration
logging.basicConfig(level=<http://logging.INFO|logging.INFO>, format='%(asctime)s - %(levelname)s - %(message)s')

def perform_task():
    logger = logging.getLogger(__name__)
    <http://logger.info|logger.info>("This is a log from the external script.")
    
if __name__ == "__main__":
    perform_task()
Prefect Task Script
Copy code
python
import subprocess
from prefect import flow, task, get_run_logger

@task(name="log-example-task")
def logger_task():
    logger = get_run_logger()
    result = subprocess.run(["python", "external_script.py"], capture_output=True, text=True)
    
    # Capture and log output from the external script
    if result.stdout:
        <http://logger.info|logger.info>(result.stdout.strip())
    if result.stderr:
        logger.error(result.stderr.strip())

@flow(name="log-example-flow")
def logger_flow():
    logger_task()

logger_flow()
This approach captures standard output and errors from the external script and logs them in Prefect's logging system. Additional reference on logging can be found here.