Log Service provides SDKs for multiple programming languages. You can use the SDKs to consume log data. This topic provides an example on how to use an SDK to consume data. This topic also describes the consumption preview feature that you can use to consume data in the Log Service console.
Example of SDK-based consumption
The following example shows how to use Log Service SDK for Java to consume log data after you create a PullLogsDemo.java file.
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.Consts;
import com.aliyun.openservices.log.common.LogGroupData;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.PullLogsRequest;
import com.aliyun.openservices.log.response.PullLogsResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class PullLogsDemo {
//The Log Service endpoint. For more information, see Endpoints. In this example, the Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter value with the actual endpoint.
private static final String endpoint = "cn-hangzhou.log.aliyuncs.com";
//The AccessKey pair of your Alibaba Cloud account. For more information, see AccessKey pair. An Alibaba Cloud account has permissions to call all API operations. If you use the AccessKey pair of an Alibaba Cloud account, security risks may occur. We recommend that you create and use a RAM user to call API operations or perform routine O&M.
private static final String accessKeyId = "your_access_id";
private static final String accessKeySecret = "your_access_key";
//The name of the project.
private static final String project = "your_project";
//The name of the Logstore.
private static final String logStore = "your_logstore";
public static void main(String[] args) throws Exception{
//Create a client for Log Service.
Client client = new Client(endpoint, accessKeyId, accessKeySecret);
//Query the number of shards in a Logstore.
int shardCount = client.GetLogStore(project, logStore).GetLogStore().GetShardCount();
System.out.println(String.format("%s has %d shards", logStore, shardCount));
//Use the BEGIN cursor or obtain a specific cursor to consume log data. If you want to use the END cursor to consume log data, use Consts.CursorMode.END.
List<String> cursors = new ArrayList<String>(shardCount);
for (int i = 0; i < shardCount; i++) {
cursors.add(i, client.GetCursor(project, logStore, i, Consts.CursorMode.BEGIN).GetCursor());
}
//Consume log data by shard.
try{
do {
for (int i = 0; i < shardCount; i++) {
//Obtain log data from each shard.
PullLogsRequest request = new PullLogsRequest(project, logStore, i, 1000, cursors.get(i));
PullLogsResponse response = client.pullLogs(request);
//Split log groups into logs by logic.
List<LogGroupData> logGroups = response.getLogGroups();
System.out.println(String.format("Get %d logGroup from logStore:%s:\tShard:%d", logGroups.size(), logStore, i));
//Move the cursor after the consumption is complete.
cursors.set(i, response.getNextCursor());
}
TimeUnit.SECONDS.sleep(3);
} while (true);
}catch (LogException e) {
System.out.println("error code :" + e.GetErrorCode());
System.out.println("error message :" + e.GetErrorMessage());
throw e;
}
}
}
For more information, see Overview of Log Service SDKs. Consumption preview
You can use the consumption preview feature to consume log data. The consumption preview feature allows you to preview specific log data that is stored in a Logstore in the Log Service console.
- Log on to the Log Service console.
- In the Projects section, click the project that you want to view.
- Choose . On the Logstores tab, click the Logstore that you want to view.
- Find the Logstore from which you want to consume log data and choose .
- In the Consumption Preview panel, select a shard and a time range. Then, click Preview. The log data of the first 10 packets in the specified time range is displayed in the Consumption Preview panel.