If you want to upload application runtime logs, operating system logs, and user logs to Simple Log Service, you can use the PutLogs
method of the Simple Log Service SDK for Java. This topic describes how to use Simple Log Service SDK for Java to write logs to Simple Log Service.
Prerequisites
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 in Linux, macOS, and Windows.
ImportantThe 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.
Simple Log Service SDK for Java is installed. For more information, see Install Simple Log Service SDK for Java.
Indexes are created. For more information, see Create indexes.
Limits
The
aliyun-log-producer
internally calls thePutLogs
interface to upload logs. There are limits on the size of raw logs that can be written each time. For more information, see Data read and write and PutLogs.The basic resources of Simple Log Service, including the number of projects, Logstores, shards, LogtailConfigs, machine groups, single LogItem size, LogItem key length, and LogItem value length, all have limitations. For more information, see Basic resources.
After running the code for the first time, enable Logstore indexing in the Simple Log Service console, wait for a minute, and then proceed with querying.
When querying logs in the console, if the length of a single field value exceeds the maximum length, the exceeding part is truncated and not included in the analysis. For more information, see Create indexes.
Regular write and specified position write
@override
public PutLogsResponse PutLogs(project, logstore, logItem, topic, source, shardHash)
Parameters
Parameter | Type | Required | Description |
project | String | Yes | The destination project. |
logstore | String | Yes | The destination Logstore. |
logItem | List | Yes | The log or log list that you want to send. The format of the log entry is LogItem. |
topic | String | No | The topic of the log. Note If you do not specify this parameter, the parameter is assigned "". |
source | String | No | The source that sends the log. Note If you do not specify this parameter, the parameter is assigned the IP address of the host where the producer resides. |
shardHash | String | No | Hash ID of the log write position. |
Sample code
/**
* 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");
/**
* The Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Chengdu) region is used. Replace the parameter value with the actual endpoint.
*/
String host = "cn-chengdu.log.aliyuncs.com";
/**
* Create a Simple Log Service client.
*/
Client client = new Client(host, accessId, accessKey);
List<LogItem> logGroup = new ArrayList<LogItem>();
for (int i = 0; i < 5; ++i) {
LogItem logItem = new LogItem();
logItem.PushBack("language", "andriod");
logItem.PushBack("time", String.valueOf(System.currentTimeMillis()));
logGroup.add(logItem);
}
/**
* // Project name.
*/
String projectName = "your-project-name";
/**
* Logstore name.
*/
String logstoreName = "your-logstore-name";
client.PutLogs(projectName, logstoreName, topic, logGroup, source);
What to do next
For information about how to use Simple Log Service SDK for Java to query logs, see Use GetLogs to query logs.
For information about how to call an operation to query logs, see GetLogsV2.
For information about how to use the Simple Log Service console to query logs, see Query and analyze logs.
References
For information about how to use Simple Log Service SDK for Java to create a project and a Logstore, see Get started with Simple Log Service SDK for Java.
For information about how to compress logs and upload logs to Simple Log Service in batches, see Use Aliyun Log Java Producer to write logs to Simple Log Service.
If the response that is returned by Simple Log Service contains error information after you use Simple Log Service SDK for Java, 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.