To consume logs, you first obtain a cursor using GetCursor for a specific shard and time range. Then, you use BatchGetLog in a loop to fetch batches of logs. The loop continues until the nextCursor returned by the response is equal to the current cursor, indicating no more data is available.
Best Practice Note: For production log consumption, it is recommended to use the Consumer Library which provides advanced features like load balancing and ordered consumption.
int shardId = 0;
GetCursorResponse res;
try {
// Get cursor for logs received in the last hour
long fromTime = (int)(System.currentTimeMillis()/1000.0 - 3600);
res = client.GetCursor(project, logStore, shardId, fromTime);
System.out.println("shard_id:" + shardId + " Cursor:" + res.GetCursor());
} catch (LogException e) {
e.printStackTrace();
}
String cursor = res.GetCursor();
while(true) {
BatchGetLogResponse logDataRes = client.BatchGetLog(project, logStore, shardId, 100, cursor);
List<LogGroupData> logGroups = logDataRes.GetLogGroups();
String nextCursor = logDataRes.GetNextCursor();
System.out.print("The Next cursor:" + nextCursor);
if (cursor.equals(nextCursor)) {
break;
}
cursor = nextCursor;
}