O método PutLogs do Simple Log Service SDK for Java permite fazer upload de logs operacionais de aplicações, logs de sistema operacional e logs de usuários no Simple Log Service. Este tópico descreve como usar o Simple Log Service SDK for Java para gravar logs no Simple Log Service.
Pré-requisitos
Simple Log Service is activated. For more information, see Activate Simple Log Service.
Simple Log Service SDK for Java is initialized. For more information, see Initialize Simple Log Service SDK for Java.
Gravação geral e gravação em locais específicos
public PutLogsResponse PutLogs(String project, String logStore,
String topic, List<LogItem> logItems, String source,
String shardHash)Parâmetros
Parâmetro | Tipo | Obrigatório | Descrição |
project | String | Sim | O projeto de destino. |
logStore | String | Sim | O Logstore de destino. |
topic | String | Não | O tópico dos logs. |
logItems | List | Sim | O log ou logs para upload. O upload deve ser feito no formato LogItem. |
source | String | Não | A origem dos logs. null Se esse parâmetro ficar vazio, o endereço IP do host onde o produtor reside será usado automaticamente. |
shardHash | String | Não | O ID de hash do local para upload dos logs. |
Código de exemplo
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.LogContent;
import com.aliyun.openservices.log.common.LogItem;
import com.aliyun.openservices.log.exception.LogException;
import java.util.ArrayList;
import java.util.List;
public class PutLogsTest {
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");
/**
* 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 = "cn-hangzhou.log.aliyuncs.com";
/**
* Create a Simple Log Service client.
*/
Client client = new Client(host, accessId, accessKey);
String project = "ali-project-test";
String logStore = "test-logstore";
String topic = "";
String source = "";
LogContent logContent = new LogContent("message", "2021-05-15 16:43:35 ParameterInvalid 400\n" +
"com.aliyun.openservices.log.exception.LogException:The body is not valid json string.\n" +
"at com.aliyun.openservices.log.Client.ErrorCheck(Client.java:2161)\n" +
"at com.aliyun.openservices.log.Client.SendData(Client.java:2312)\n" +
"at com.aliyun.openservices.log.Client.PullLogsk(Client.java:1397)\n" +
"at com.aliyun.openservices.log.Client.SendData(Client.java:2265)\n" +
"at com.aliyun.openservices.log.Client.GetCursor(Client.java:1123)\n" +
"at com.aliyun.openservices.log.Client.PullLogs(Client.java:2161)\n" +
"at com.aliyun.openservices.log.Client.ErrorCheck(Client.java:2426)\n" +
"at transformEvent.main(transformEvent.java:2559)");
List<LogItem> logItems = new ArrayList<>();
for (int i = 0; i < 5; ++i) {
LogItem logItem = new LogItem();
logItem.PushBack("language", "android");
logItem.PushBack("time", String.valueOf(System.currentTimeMillis()));
logItem.PushBack(logContent);
logItems.add(logItem);
}
client.PutLogs(project, logStore, topic, logItems, source, null);
}
}Próximos passos
Use o Simple Log Service SDK for Java para consultar logs. Para mais informações, consulte Usar GetLogs para consultar logs.
Chame uma operação para consultar logs. Para mais informações, consulte GetLogsV2.
Consulte logs no console do Simple Log Service. Para mais informações, consulte Início rápido de consulta e análise.
Referências
Use o Simple Log Service SDK for Java para criar um projeto e um Logstore. Para mais informações, consulte Introdução ao Simple Log Service SDK for Java.
Comprima os logs e faça upload no Simple Log Service em lote. Para mais informações, consulte Usar o Aliyun Log Java Producer para gravar logs no Simple Log Service.
Se a resposta do Simple Log Service contiver informações de erro ao usar o Simple Log Service SDK for Java, a chamada falhou. Solucione os erros com base nos códigos de erro retornados em caso de falha nas chamadas de API. Para mais informações, consulte Códigos de erro.