pythongoogle-cloud-platformgoogle-cloud-logging

How to get the next page token from google cloud logging API


Im trying to get all the logs from the current month for an instance and I'm trying to get the next_page_token but it is not present in the response object. Here's what I tried so far.

from pytz import timezone
from datetime import datetime
from calendar import monthrange
from google.cloud import logging
from google.oauth2 import service_account

def getInstanceLogsList(
    instanceId: str,
    authTokenDict: dict,
):
    today = datetime.now().astimezone(tz = timezone("Asia/Kolkata"));

    monthStartDay = 1
    monthEndDay = monthrange(today.year, today.month)[1]
    
    credentials = service_account.Credentials.from_service_account_info(authTokenDict)
    loggingClient = logging.Client(credentials = credentials)

    endDateTimeObject = datetime(year = today.year, month = today.month, day = monthEndDay, hour = 23, minute = 59, second = 59).astimezone(tz = timezone("Asia/Kolkata")).strftime("%Y-%m-%dT%H:%M:%S%z")
    startDateTimeObject = datetime(year = today.year, month = today.month, day = monthStartDay, hour = 0, minute = 0, second = 0).astimezone(tz = timezone("Asia/Kolkata")).strftime("%Y-%m-%dT%H:%M:%S%z")

    pageToken = None
    listLogsResponse = []
    
    while True:
        response = loggingClient.list_entries(
            filter_ = f'resource.type = "gce_instance" AND resource.labels.instance_id = "{instanceId}" AND timestamp >= "{startDateTimeObject}" AND timestamp <= "{endDateTimeObject}"',
            page_token = pageToken
        )
        listLogsResponse.extend(response)
        if response.next_page_token:
            pageToken = response.next_page_token
        else:
            break

On trying this code I'm getting the following error

AttributeError: 'generator' object has no attribute 'next_page_token'

How to access next_page_token


Solution

  • That's the magic of the client API! The generator hides the complexity of page management and so on.

    Only loop over the generator. If the next page is requested, it will ask for it automatically. You only see your loop on your side, it's much more easy!

    More detail here