I'm struggling to get logs to display within flow/...
# ask-community
j
I'm struggling to get logs to display within flow/task runs. Here's some example code that gets executed by my worker. None of these logs are displayed.
Copy code
try:
    logger = get_run_logger()
    logger.setLevel(logging.INFO)
except MissingContextError:
    # If there's no active Prefect context, use a standard Python logger
    logger = logging.getLogger()


def run_single_pipeline(account_id: str, resource: str) -> Dict:
    """Run a single pipeline for a given account and resource"""
    logger.info(f"Starting pipeline for account {account_id}, resource {resource}")
    #...pipeline code
   logger.info(f"Pipeline run successful for {account_id} - {resource}")

@task(persist_result=True)
def process_ads(account_id: str) -> Dict:
    try:
        result = run_single_pipeline(account_id, "ads")
        logger.info(f"Pipeline result for table ads: {result}")
        return result
    except PrefectException as e:
        logger.error(f"Pipeline error for table ads: {str(e)}")
        raise  # Re-raise the exception to mark the flow run as failed
1
n
hi @Jeff S - is this block being run within a flow or task?
Copy code
try:
    logger = get_run_logger()
    logger.setLevel(<http://logging.INFO|logging.INFO>)
except MissingContextError:
    # If there's no active Prefect context, use a standard Python logger
    logger = logging.getLogger()
if not, it seems you'd always end up with a standard logger (not a run logger) and so any records emitted by that logger would not get sent up to the API
j
Ahh, no it's not. This is just the top of my worker file. Should I instantiate it in the flow?
n
yeah you should be able to do
Copy code
logger = get_run_logger()
inside the flow and then you shouldnt need the
try/except
🙌 1
j
thank you - I'll give that shot!
n
catjam
j
🦜 1