All Products
Search
Document Center

Simple Log Service:Use Simple Log Service SDK for Java to manage projects

Last Updated:Oct 26, 2023

A project in Simple Log Service is a unit that is used to manage resources such as Logstores, Metricstores, and machine groups. You can use the endpoint of a project to access the resources of Simple Log Service. This topic describes how to create, modify, query, and delete a project by using Simple Log Service SDK for Java and provides the sample code.

Prerequisites

  • Simple Log Service is activated. For more information, see Activate Simple Log Service.

  • A Resource Access Management (RAM) user is created, and the required permissions are granted to the RAM user. For more information, see Create a RAM user and grant permissions to the RAM user.

  • The ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured. For more information, see Configure environment variables.

    Important
    • The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. We recommend that you use the AccessKey pair of a RAM user to call API operations or perform routine O&M.

    • We recommend that you do not save the AccessKey ID or AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked, and the security of all resources within your account may be compromised.

  • A Java development environment is installed.

    Simple Log Service SDK for Java supports Java Runtime Environment (JRE) 6.0 and later. You can run the java -version command to check the version of your JRE. If no JRE is installed, you can download an installation package from the Java official website and install JRE.

  • Simple Log Service SDK for Java is installed. For more information, see Install Simple Log Service SDK for Java.

Usage notes

In this example, the public Simple Log Service endpoint for the China (Hangzhou) region is used, which is https://cn-hangzhou.log.aliyuncs.com. If you want to access Simple Log Service by using 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 Endpoints.

Sample code that is used to create a project

The following sample code provides an example on how to create a project named ali-test-project:

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.exception.LogException;

public class CreateProject {
    public static void main(String[] args) throws LogException {
        // Configure environment variables. 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");
        // Enter a project name. 
        String projectName = "ali-test-project";
        // Specify the Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter value 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);
        // Create a project. 
        try {
            String projectDescription = "project description";
            System.out.println("ready to create project");
            client.CreateProject(projectName, projectDescription);
            System.out.println(String.format("create project %s success",projectName));

        } 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;
        }
    }
}

Expected results:

ready to create project
create project ali-test-project success

Sample code that is used to modify a project

The following sample code provides an example on how to modify the ali-test-project project:

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.UpdateProjectRequest;

public class UpdateProject {
    public static void main(String[] args) throws LogException {
        // Configure environment variables. 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");
        // Enter the project name. 
        String projectName = "ali-test-project";
        // Specify the Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter value 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);
        // Modify the project. 
        try {
            String projectDescription = "This is the new project description";
            System.out.println("ready to update project");
            UpdateProjectRequest request = new UpdateProjectRequest(projectName, projectDescription);
            client.updateProject(request);
            System.out.println(String.format("update project %s success",projectName));

        } 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;
        }
    }
}

Expected results:

ready to update project
update project ali-test-project success

Sample code that is used to query all projects

The following sample code provides an example on how to query all projects:

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.Project;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.response.ListProjectResponse;

public class ListProject {
    public static void main(String[] args) throws LogException {
        // Configure environment variables. 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");
        // Enter a project name. 
        String projectName = "ali-test-project";
        // Specify the Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter value 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);
        // Query all projects. 
        try {
            System.out.println("ready to list project");
            ListProjectResponse response = client.ListProject();
            for (Project project : response.getProjects()) {
                System.out.println(project.getProjectName());
            }

        } 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;
        }
    }
}

Expected results:

ready to list project
ali-test-project
local-ide

Sample code that is used to query a project

The following sample code provides an example on how to query a project:

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.response.GetProjectResponse;

public class GetProject {
    public static void main(String[] args) throws LogException {
        // Configure environment variables. 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");
        // Enter the project name. 
        String projectName = "ali-test-project";
        // Specify the Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter value 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);
        // Query the specified project. 
        try {
            System.out.println("ready to get project");
            GetProjectResponse response = client.GetProject(projectName);
            System.out.println(String.format("get project %s success",projectName));
            System.out.println("The Project description is:" + response.GetProjectDescription());
            System.out.println("The Project owner info is:" + response.GetProjectOwner());
            System.out.println("The Project region info is:" + response.GetProjectRegion());
            System.out.println("The Project status info is:" + response.GetProjectStatus());

        } 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;
        }
    }
}

Expected results:

ready to get project
get project ali-test-project success
The Project description is:project description
The Project owner is:Ownerid
The Project region is:cn-hangzhou
The Project status is:Normal

Sample code that is used to query logs in a project

The following sample code provides an example on how to query the number of logs in a Logstore of a project:

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.QueriedLog;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.response.GetLogsResponse;

public class GetProjectLogs {
    public static void main(String[] args) throws LogException {
        // Configure environment variables. 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");
        // Enter the project name. 
        String projectName = "ali-test-project";
        // Enter the Logstore name 
        String logstoreName = "ali-test-logstore";
        // Specify the Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter value 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);
        // Use an SQL statement to query the number of logs in the specified Logstore of the specified project. 
        try {
            System.out.println("ready to get project logs");
            String query = "SELECT COUNT(*) as pv FROM " + logstoreName + " where __time__ > 1646102500 and __time__ < 1646103400";
            GetLogsResponse response = client.GetProjectLogs(projectName,query);
            for (QueriedLog log: response.getLogs()) {
                System.out.println(log.GetLogItem().ToJsonString());
            }

        } 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;
        }
    }
}

Expected results:

ready to get project logs
{"pv":"4615","logtime":1}

Sample code that is used to delete a project

The following sample code provides an example on how to delete the ali-test-project project:

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.exception.LogException;

public class DeleteProject {
    public static void main(String[] args) throws LogException {
        // Configure environment variables. 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");
        // Enter the project name. 
        String projectName = "ali-test-project";
        // Specify the Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter value 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);
        // Delete the project. 
        try {
            System.out.println("ready to delete project");
            client.DeleteProject(projectName);
            System.out.println(String.format("delete project %s success",projectName));

        } 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;
        }
    }
}

Expected results:

ready to delete project
delete project ali-test-project success

References

  • If the response that is returned by Log Service contains error information after you call an API operation, the call fails. You can handle errors based on the error codes that are returned when API calls fail. For more information, see Error codes.
  • Alibaba Cloud OpenAPI Explorer provides debugging capabilities, SDKs, examples, and related documents. You can use OpenAPI Explorer to debug Log Service API operations without the need to manually encapsulate or sign requests. For more information, visit OpenAPI Portal.
  • Log Service provides the command-line interface (CLI) to meet the requirements for automated configurations in Log Service. For more information, see Log Service CLI.
  • For more information about project-related API operations, see the following topics:

  • For more information about sample code, see Alibaba Cloud Log Service SDK for Java on GitHub.