Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

google.cloud.logging_v2 not returning logs

My code:

 

def get_failed_jobs(project_id, max_jobs=5):
    # Initialize the Batch client
    batch_client = batch_v1.BatchServiceClient()

    # Define your project and location
    region = "us-west1"
    parent = f"projects/{project_id}/locations/{region}"
    client = batch_v1.BatchServiceClient()

    # Fetch the most recent batch jobs, filtering for failed ones
    failed_jobs = []
    for job in client.list_jobs(parent=parent):
        if str(job.status.state) == 'State.FAILED':
            failed_jobs.append(job)
        if len(failed_jobs) >= max_jobs:
            break
    return failed_jobs


def get_logs_for_job(job_id, project_id):
    # Initialize the Logging client
    log_client = logging_v2.Client(project=project_id)

    # create logger
    logger = log_client.logger("batch_task_logs")

    # set filter
    log_filter = f'resource.labels.job_id="{job_id}"'

    # get logs
    try:
        logs = logger.list_entries(filter_=log_filter)
        # get log entries
        log_entries = [entry.text_payload for entry in logs]
        return log_entries
    except Exception as e:
        print(f"Error fetching logs for {job_id}: {e}")
        return []


def main(args):
    # set GCP project
    project_id = "my-gcp-project"

    # get failed jobs
    failed_jobs = get_failed_jobs(project_id, max_jobs=5)

    # write logs
    for job in failed_jobs:
        logs = get_logs_for_job(job.uid, project_id)
        # Process or display logs
        print(f"Logs for job {job.uid}:")
        for log in logs:
            print(log)

 

The output:

 

Logs for job nf-0a509c4d-170269-76556b37-ax58-45990:
Logs for job nf-83f6c7c7-170268-545aaf76-15a6-4a990:
Logs for job nf-4a97b993-170269-fae4fb13-ed7a-441f0:
Logs for job nf-ef91af07-170268-8416f231-372a-44280:
Logs for job nf-2ea181f6-170268-684edcf5-41ae-43fa0:

 

I don't understand why the text for each log is not returned by `

[entry.text_payload for entry in logs]`. Gemini is terrible at answering such questions about the GCP API, and the GCP API docs are very hard to understand (dense and often rather unorganized). 

I should note that I'm using:
google-cloud-logging==3.8.0
google-cloud-storage==2.13.0
google-cloud-batch==0.17.6
 
Any suggestions would be appreciated!
0 4 907
4 REPLIES 4