A Logstore is the unit for data collection, storage, and query in Simple Log Service. Each Logstore belongs to a project, and each project can contain multiple Logstores. You can use the Java SDK to create, update, query, and delete Logstores.
Prerequisites
Simple Log Service is activated.
Simple Log Service SDK for Python is initialized.
Simple Log Service SDK for Java is installed. For more information, see Install the Java SDK.
Precautions
In this example, the public Simple Log Service endpoint for the China (Hangzhou) region is used. Endpoint: https://cn-hangzhou.log.aliyuncs.com.
If you want to access Simple Log Service from other Alibaba Cloud services that reside in the same region as your project, you can use the internal Simple Log Service endpoint, which is https://cn-hangzhou-intranet.log.aliyuncs.com.
For more information about the supported regions and endpoints of Simple Log Service, see Endpoint.
Create a Logstore
The following example creates a Logstore named ali-test-logstore.
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.LogStore;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.CreateLogStoreRequest;
public class CreateLogstore {
public static void main(String[] args) throws LogException {
// In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// Specify the project name.
String projectName = "ali-test-project";
// Set the endpoint of Simple Log Service. In this example, the endpoint of the China (Hangzhou) region is used. Replace it with the actual endpoint.
String host = "https://cn-hangzhou.log.aliyuncs.com";
// Create a Simple Log Service client.
Client client = new Client(host, accessId, accessKey);
try {
// Specify the Logstore name.
String logstoreName = "ali-test-logstore";
System.out.println("ready to create logstore");
// Create a Logstore. Set the data retention period to 60 days and the number of shards to 2. Enable web tracking.
LogStore logStore = new LogStore(logstoreName, 60, 2, true);
// Enable auto split for shards.
logStore.setmAutoSplit(true);
// Set the maximum number of shards for auto split to 64.
logStore.setmMaxSplitShard(64);
// Enable the feature of recording the public IP address.
logStore.setAppendMeta(true);
// Set the storage period of data in the hot tier of the Logstore to 30 days.
logStore.setHotTTL(30);
// Set the Logstore type to standard.
logStore.setMode("standard");
CreateLogStoreRequest request = new CreateLogStoreRequest(projectName, logStore);
// Create the Logstore.
client.CreateLogStore(request);
System.out.println(String.format("create logstore %s success", logstoreName));
} catch (LogException e) {
System.out.println("LogException e :" + e.toString());
System.out.println("error code :" + e.GetErrorCode());
System.out.println("error message :" + e.GetErrorMessage());
throw e;
}
}
}
The following output is returned:
ready to create logstore
create logstore ali-test-logstore success
Update a Logstore
The following example updates the configurations of a Logstore named ali-test-logstore.
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.LogStore;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.UpdateLogStoreRequest;
public class UpdateLogstore {
public static void main(String[] args) throws LogException {
// In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// Specify the project name.
String projectName = "ali-test-project";
// Set the endpoint of Simple Log Service. In this example, the endpoint of the China (Hangzhou) region is used. Replace it with the actual endpoint.
String host = "https://cn-hangzhou.log.aliyuncs.com";
// Create a Simple Log Service client.
Client client = new Client(host, accessId, accessKey);
try {
// Specify the Logstore name.
String logstoreName = "ali-test-logstore";
System.out.println("ready to update logstore");
// Update the storage period of data in the hot tier of the Logstore to 45 days.
LogStore logStore = new LogStore(logstoreName, 60, 2, true);
logStore.setHotTTL(45);
UpdateLogStoreRequest request = new UpdateLogStoreRequest(projectName, logStore);
// Update the Logstore.
client.UpdateLogStore(request);
System.out.println(String.format("update logstore %s success", logstoreName));
} catch (LogException e) {
System.out.println("LogException e :" + e.toString());
System.out.println("error code :" + e.GetErrorCode());
System.out.println("error message :" + e.GetErrorMessage());
throw e;
}
}
}
The following output is returned:
ready to update logstore
update logstore ali-test-logstore success
Query all Logstores
The following example lists all Logstores in a project.
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.ListLogStoresRequest;
import com.aliyun.openservices.log.response.ListLogStoresResponse;
public class ListLogstore {
public static void main(String[] args) throws LogException {
// In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// Specify the project name.
String projectName = "ali-test-project";
// Set the endpoint of Simple Log Service. In this example, the endpoint of the China (Hangzhou) region is used. Replace it with the actual endpoint.
String host = "https://cn-hangzhou.log.aliyuncs.com";
// Create a Simple Log Service client.
Client client = new Client(host, accessId, accessKey);
try {
System.out.println("ready to list logstore");
// Query 10 Logstores.
ListLogStoresRequest request = new ListLogStoresRequest(projectName, 0, 10, "", "None");
ListLogStoresResponse response = client.ListLogStores(request);
for (String logStore : response.GetLogStores()) {
System.out.println(logStore.toString());
}
} catch (LogException e) {
System.out.println("LogException e :" + e.toString());
System.out.println("error code :" + e.GetErrorCode());
System.out.println("error message :" + e.GetErrorMessage());
throw e;
}
}
}
The following output is returned:
ready to list logstore
ali-test-logstore
Query a specific Logstore
The following example queries a specific Logstore.
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.GetLogStoreRequest;
import com.aliyun.openservices.log.response.GetLogStoreResponse;
public class GetLogstore {
public static void main(String[] args) throws LogException {
// In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// Specify the project name.
String projectName = "ali-test-project";
// Set the endpoint of Simple Log Service. In this example, the endpoint of the China (Hangzhou) region is used. Replace it with the actual endpoint.
String host = "https://cn-hangzhou.log.aliyuncs.com";
// Create a Simple Log Service client.
Client client = new Client(host, accessId, accessKey);
try {
// Specify the Logstore name.
String logStoreName = "ali-test-logstore";
System.out.println("ready to get logstore");
// Query the specified Logstore.
GetLogStoreRequest request = new GetLogStoreRequest(projectName, logStoreName);
GetLogStoreResponse response = client.GetLogStore(request);
System.out.println("The Logstore name is : " + response.GetLogStore().GetLogStoreName());
} catch (LogException e) {
System.out.println("LogException e :" + e.toString());
System.out.println("error code :" + e.GetErrorCode());
System.out.println("error message :" + e.GetErrorMessage());
throw e;
}
}
}
The following output is returned:
ready to get logstore
The Logstore name is : ali-test-logstore
Delete a Logstore
The following example deletes a Logstore named ali-test-logstore.
-
After a Logstore is deleted, the data in it is permanently deleted and cannot be recovered. Proceed with caution.
-
Before you delete a Logstore, delete all Logtail configurations associated with it.
-
If LogShipper is enabled for the Logstore, stop writing new data to the Logstore before you delete it. Make sure that all existing data in the Logstore is delivered.
-
If you use an Alibaba Cloud account to delete a Logstore and receive an insufficient permissions error, submit a ticket to delete the Logstore.
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.exception.LogException;
public class DeleteLogstore {
public static void main(String[] args) throws LogException {
// In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// Specify the project name.
String projectName = "ali-test-project";
// Set the endpoint of Simple Log Service. In this example, the endpoint of the China (Hangzhou) region is used. Replace it with the actual endpoint.
String host = "https://cn-hangzhou.log.aliyuncs.com";
// Create a Simple Log Service client.
Client client = new Client(host, accessId, accessKey);
try {
// Specify the Logstore name.
String logStoreName = "ali-test-logstore";
System.out.println("ready to delete logstore");
// Delete the specified Logstore.
client.DeleteLogStore(projectName,logStoreName);
System.out.println(String.format("delete logstore %s success", logStoreName));
} catch (LogException e) {
System.out.println("LogException e :" + e.toString());
System.out.println("error code :" + e.GetErrorCode());
System.out.println("error message :" + e.GetErrorMessage());
throw e;
}
}
}
The following output is returned:
ready to delete logstore
delete logstore ali-test-logstore success
References
In addition to its native SDK, SLS also supports the common Alibaba Cloud SDKs. For more information, see Log Service_SDKcenter-Alibaba CloudOpenAPIDeveloper Portal.
-
Project-related API operations: