Use a API GetLogs para consultar logs coletados com o SDK do SLS para Java.
Pré-requisitos
O Simple Log Service está ativado. Para obter mais informações, consulte Activate Simple Log Service.
O SDK do Simple Log Service para Java foi inicializado. Para obter mais informações, consulte Initialize Simple Log Service SDK for Java.
Observações de uso
-
Este exemplo usa o endpoint público do Simple Log Service para a região China (Hangzhou). Endpoint:
https://cn-hangzhou.log.aliyuncs.com.Para acessar o Simple Log Service a partir de outros serviços da Alibaba Cloud que residam na mesma região do seu projeto, use o endpoint interno do Simple Log Service:
https://cn-hangzhou-intranet.log.aliyuncs.com.Para obter mais informações sobre as regiões e os endpoints compatíveis com o Simple Log Service, consulte Endpoint.
-
Chame o método
IsCompleted()no objeto de resposta do SDK para verificar se o conjunto de resultados está completo.true: A consulta foi bem-sucedida e o conjunto de resultados está completo.false: A consulta foi bem-sucedida, mas o conjunto de resultados está incompleto. Repita a solicitação para obter os resultados completos. Possible causes of inaccurate queries.
Parâmetros
Parâmetros de solicitação
Nome | Tipo | Obrigatório | Descrição |
project | String | Sim | O nome do projeto. O projeto no Simple Log Service isola os recursos de diferentes usuários e controla o acesso a recursos específicos. Consulte Manage projects. |
logstore | String | Sim | O nome do logstore. O logstore no Simple Log Service coleta, armazena e consulta logs. Consulte Manage Logstores. |
from | int | Sim | O início do intervalo de tempo a ser consultado. O valor é um timestamp UNIX. Nota
|
to | int | Sim | O fim do intervalo de tempo a ser consultado. O valor é um timestamp UNIX. Nota
|
topic | String | Não | O tópico dos logs. O valor padrão é uma string vazia. Para obter mais informações, consulte Log topics. |
query | String | Não | Uma instrução de pesquisa ou analítica. Query and Analysis Overview. Para usar o SQL Exclusive, adicione Nota Se query contiver uma instrução analítica (SQL), os parâmetros line e offset são inválidos. Defina-os como 0 e use a cláusula LIMIT para paginação. Paginate query and analysis results. |
line | int | Não | Válido apenas para instruções de pesquisa. Número máximo de logs retornados. Intervalo: 0 a 100. Padrão: 100. |
offset | int | Não | Válido apenas para instruções de pesquisa. Linha inicial para a consulta. Padrão: 0. |
reverse | boolean | Não | Indica se a API retornará os logs em ordem decrescente de timestamp. Precisão: nível de minuto.
Importante
|
powerSql | boolean | Não | Indica o uso do Dedicated SQL. High-performance and accurate query and analysis (Dedicated SQL).
Também é possível ativar o Dedicated SQL adicionando a configuração ao parâmetro query. |
scan | boolean | Não | Indica o uso do modo de varredura (scan) para a consulta. |
forward | boolean | Não | Válido apenas para consultas de varredura. true: recupera a próxima página. false: recupera a página anterior. |
Parâmetros de resposta
Log bruto
body_bytes_sent:1750
host:www.example.com
http_referer:www.example.com
http_user_agent:Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_6; it-it) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27
http_x_forwarded_for:203.0.XX.XX
remote_addr:203.0.XX.XX
remote_user:p288
request_length:13741
request_method:GET
request_time:71
request_uri:/request/path-1/file-1
http_code:200
time_local:11/Aug/2021:06:52:27
upstream_response_time:0.66
Exemplos de consulta e análise
Os exemplos em Java a seguir demonstram padrões comuns de consulta.
Ao chamar o GetLogs com o SDK do SLS para Java:
Se
querycontiver apenas uma instrução de pesquisa (por exemplo,request_method:POST), o parâmetrolinecontrola o número de logs retornados (máximo de 100). Para retornar mais de 100 logs, use a cláusula LIMIT no SQL. LIMIT clause.Se
querycontiver uma instrução de consulta e análise (por exemplo,request_method:POST | SELECT host, COUNT(*) AS pv GROUP BY host LIMIT 5), o parâmetrolineé ignorado. Use a cláusula LIMIT no SQL para controlar o número de linhas. LIMIT clause.
Exemplo 1: Consultar logs usando uma palavra-chave
Consulte logs usando a palavra-chave path-0/file-5. O parâmetro line limita os resultados a 3 logs.
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.LogItem;
import com.aliyun.openservices.log.common.QueriedLog;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.GetLogsRequest;
import com.aliyun.openservices.log.response.GetLogsResponse;
public class GetLogsTest {
public static void main(String[] args) throws LogException {
// This example obtains the AccessKey ID and AccessKey secret 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 project = "your-project-name";
// Set the endpoint for Simple Log Service. This example uses the endpoint of the China (Hangzhou) region. Replace the value with the actual endpoint.
String host = "cn-hangzhou.log.aliyuncs.com";
// Enter the Logstore name.
String logStore = "your-logstore-name";
// Create a Simple Log Service client.
Client client = new Client(host, accessId, accessKey);
// Execute a query in the specified Logstore.
try {
// Use the keyword "path-0/file-5" to query logs.
String query = "path-0/file-5";
int from = 1754449503;
int to = 1754449510;
// In this example, the query parameter is used to set the query statement. The line parameter is used to control the number of returned logs. The value is 3, and the maximum value is 100.
GetLogsRequest request = new GetLogsRequest(project, logStore, from, to, "", query);
GetLogsResponse logsResponse = client.GetLogs(request);
System.out.println("-------------Query is started.-------------");
System.out.println("Returned query result count :" + logsResponse.GetCount());
System.out.println("from time is :" + from);
System.out.println("to time is :" + to);
for (QueriedLog log : logsResponse.getLogs()) {
LogItem item = log.GetLogItem();
System.out.println("log time : " + item.mLogTime);
System.out.println("Jsonstring : " + item.ToJsonString());
}
System.out.println("-------------Query is finished.-------------");
} 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;
}
}
}
Resposta:
-------------Query is started.-------------
Returned query result count :3
from time is :1644573549
to time is :1644573849
log time : 1644573808
Jsonstring : {"remote_addr":"203.0.XX.XX","__topic__":"nginx_access_log","request_uri":"/request/path-0/file-5"...}
log time : 1644573808
Jsonstring : {"remote_addr":"203.0.XX.XX","__topic__":"nginx_access_log","request_uri":"/request/path-0/file-5"...}
log time : 1644573788
Jsonstring : {"remote_addr":"203.0.XX.XX","__topic__":"nginx_access_log","request_uri":"/request/path-0/file-5"...}
-------------Query is finished.-------------
Process finished with exit code 0
Exemplo 2: Consultar logs especificando um campo
Consulte logs em que o método de solicitação seja POST. O parâmetro line limita os resultados a 3 logs.
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.LogItem;
import com.aliyun.openservices.log.common.QueriedLog;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.GetLogsRequest;
import com.aliyun.openservices.log.response.GetLogsResponse;
public class GetLogsTest {
public static void main(String[] args) throws LogException {
// This example obtains the AccessKey ID and AccessKey secret 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 project = "your-project-name";
// Set the endpoint for Simple Log Service. This example uses the endpoint of the China (Hangzhou) region. Replace the value with the actual endpoint.
String host = "cn-hangzhou.log.aliyuncs.com";
// Enter the Logstore name.
String logStore = "your-logstore-name";
// Create a Simple Log Service client.
Client client = new Client(host, accessId, accessKey);
// Execute a query in the specified Logstore.
try {
// Query for logs where the request method is POST.
String query = "request_method:POST";
// Set the time range for the query.
int from = 1754449503;
int to = 1754449510;
// In this example, the query parameter is used to set the query statement. The line parameter is used to control the number of returned logs. The value is 3, and the maximum value is 100.
GetLogsRequest request = new GetLogsRequest(project, logStore, from, to, "", query);
GetLogsResponse logsResponse = client.GetLogs(request);
System.out.println("-------------Query is started.-------------");
System.out.println("Returned query result count :" + logsResponse.GetCount());
System.out.println("from time is :" + from);
System.out.println("to time is :" + to);
for (QueriedLog log : logsResponse.getLogs()) {
LogItem item = log.GetLogItem();
System.out.println("log time : " + item.mLogTime);
System.out.println("Jsonstring : " + item.ToJsonString());
}
System.out.println("-------------Query is finished.-------------");
} 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;
}
}
}
Resposta:
-------------Query is started.-------------
Returned query result count :3
from time is :1644574151
to time is :1644574451
log time : 1644574438
Jsonstring : {"remote_addr":"203.0.XX.XX","__topic__":"nginx_access_log","body_bytes_sent":"3604","request_method":"POST"...}
log time : 1644574438
Jsonstring : {"remote_addr":"203.0.XX.XX","__topic__":"nginx_access_log","body_bytes_sent":"3369","request_method":"POST"...}
log time : 1644574438
Jsonstring : {"remote_addr":"203.0.XX.XX","__topic__":"nginx_access_log","body_bytes_sent":"12714","request_method":"POST"...}
-------------Query is finished.-------------
Process finished with exit code 0
Exemplo 3: Analisar logs usando uma instrução SQL
Consulte logs em que o método de solicitação seja POST e conte as visualizações de página (PVs).
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.LogItem;
import com.aliyun.openservices.log.common.QueriedLog;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.GetLogsRequest;
import com.aliyun.openservices.log.response.GetLogsResponse;
public class GetLogsTest {
public static void main(String[] args) throws LogException {
// This example obtains the AccessKey ID and AccessKey secret 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 project = "your-project-name";
// Set the endpoint for Simple Log Service. This example uses the endpoint of the China (Hangzhou) region. Replace the value with the actual endpoint.
String host = "cn-hangzhou.log.aliyuncs.com";
// Enter the Logstore name.
String logStore = "your-logstore-name";
// Create a Simple Log Service client.
Client client = new Client(host, accessId, accessKey);
// Execute a query in the specified Logstore.
try {
// Query for logs where the request method is POST and count the number of PVs for the POST requests.
String query = "request_method:POST|select COUNT(*) as pv";
// Set the time range for the query.
int from = 1754449503;
int to = 1754449510;
// In this example, the query parameter is set to a query and analysis statement. The line parameter is invalid.
GetLogsRequest request = new GetLogsRequest(project, logStore, from, to, "", query);
GetLogsResponse logsResponse = client.GetLogs(request);
System.out.println("-------------Query is started.-------------");
System.out.println("Returned query result count :" + logsResponse.GetCount());
System.out.println("from time is :" + from);
System.out.println("to time is :" + to);
for (QueriedLog log : logsResponse.getLogs()) {
LogItem item = log.GetLogItem();
System.out.println("log time : " + item.mLogTime);
System.out.println("Jsonstring : " + item.ToJsonString());
}
System.out.println("-------------Query is finished.-------------");
} 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;
}
}
}
Resposta:
-------------Query is started.-------------
Returned query result count :1
from time is :1644574354
to time is :1644574654
log time : 1644574354
Jsonstring : {"pv":"162","logtime":1644574354}
-------------Query is finished.-------------
Process finished with exit code 0
Exemplo 4: Analisar logs usando a cláusula GROUP BY
Consulte logs em que o método de solicitação seja POST e agrupe os resultados por host.
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.LogItem;
import com.aliyun.openservices.log.common.QueriedLog;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.GetLogsRequest;
import com.aliyun.openservices.log.response.GetLogsResponse;
public class GetLogsTest {
public static void main(String[] args) throws LogException {
// This example obtains the AccessKey ID and AccessKey secret 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 project = "your-project-name";
// Set the endpoint for Simple Log Service. This example uses the endpoint of the China (Hangzhou) region. Replace the value with the actual endpoint.
String host = "cn-hangzhou.log.aliyuncs.com";
// Enter the Logstore name.
String logStore = "your-logstore-name";
// Create a Simple Log Service client.
Client client = new Client(host, accessId, accessKey);
// Execute a query in the specified Logstore.
try {
// Query for logs where the request method is POST and group the results by host.
// Use the LIMIT clause in the SQL syntax to limit the number of returned rows to 5.
String query = "request_method:POST|select host, COUNT(*) as pv group by host limit 5";
// Set the time range for the query.
int from = 1754449503;
int to = 1754449510;
// In this example, the query parameter is set to a query and analysis statement. The line parameter is invalid. The number of rows to return is determined by the LIMIT clause in the query.
GetLogsRequest request = new GetLogsRequest(project, logStore, from, to, "", query);
GetLogsResponse logsResponse = client.GetLogs(request);
System.out.println("-------------Query is started.-------------");
System.out.println("Returned query result count :" + logsResponse.GetCount());
System.out.println("from time is :" + from);
System.out.println("to time is :" + to);
for (QueriedLog log : logsResponse.getLogs()) {
LogItem item = log.GetLogItem();
System.out.println("log time : " + item.mLogTime);
System.out.println("Jsonstring : " + item.ToJsonString());
}
System.out.println("-------------Query is finished.-------------");
} 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;
}
}
}
Resposta:
-------------Query is started.-------------
Returned query result count :5
from time is :1644574445
to time is :1644574745
log time : 1644574445
Jsonstring : {"pv":"1","host":"www.example1.com","logtime":1644574445}
log time : 1644574445
Jsonstring : {"pv":"1","host":"www.example.org","logtime":1644574445}
log time : 1644574445
Jsonstring : {"pv":"1","host":"www.example.net","logtime":1644574445}
log time : 1644574445
Jsonstring : {"pv":"1","host":"www.example.edu","logtime":1644574445}
log time : 1644574445
Jsonstring : {"pv":"1","host":"www.aliyundoc.com","logtime":1644574445}
-------------Query is finished.-------------
Process finished with exit code 0
Exemplo 5: Analisar logs usando a cláusula GROUP BY (200 logs retornados)
Consulte logs em que o método de solicitação seja POST, agrupe por host e retorne até 200 linhas usando a cláusula LIMIT.
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.LogItem;
import com.aliyun.openservices.log.common.QueriedLog;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.GetLogsRequest;
import com.aliyun.openservices.log.response.GetLogsResponse;
public class GetLogsTest {
public static void main(String[] args) throws LogException {
// This example obtains the AccessKey ID and AccessKey secret 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 project = "your-project-name";
// Set the endpoint for Simple Log Service. This example uses the endpoint of the China (Hangzhou) region. Replace the value with the actual endpoint.
String host = "cn-hangzhou.log.aliyuncs.com";
// Enter the Logstore name.
String logStore = "your-logstore-name";
// Create a Simple Log Service client.
Client client = new Client(host, accessId, accessKey);
// Execute a query in the specified Logstore.
try {
// Query for logs where the request method is POST and group the results by host.
// Use the LIMIT clause in the SQL syntax to control the number of rows to return.
String query = "request_method:POST|select host, COUNT(*) as pv group by host limit 0,200";
// Set the time range for the query.
int from = 1754449503;
int to = 1754449510;
// In this example, the query parameter is set to a query and analysis statement. The line parameter is invalid. The number of rows to return is determined by the LIMIT clause in the query.
GetLogsRequest request = new GetLogsRequest(project, logStore, from, to, "", query);
GetLogsResponse logsResponse = client.GetLogs(request);
System.out.println("-------------Query is started.-------------");
System.out.println("Returned query result count :" + logsResponse.GetCount());
System.out.println("from time is :" + from);
System.out.println("to time is :" + to);
for (QueriedLog log : logsResponse.getLogs()) {
LogItem item = log.GetLogItem();
System.out.println("log time : " + item.mLogTime);
System.out.println("Jsonstring : " + item.ToJsonString());
}
System.out.println("-------------Query is finished.-------------");
} 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;
}
}
}
Resposta:
-------------Query is started.-------------
Returned query result count :200
from time is :1644574445
to time is :1644574745
log time : 1644574445
Jsonstring : {"pv":"1","host":"www.example1.com","logtime":1644574445}
log time : 1644574445
Jsonstring : {"pv":"1","host":"www.example.org","logtime":1644574445}
log time : 1644574445
Jsonstring : {"pv":"1","host":"www.example.net","logtime":1644574445}
log time : 1644574445
Jsonstring : {"pv":"1","host":"www.example.edu","logtime":1644574445}
log time : 1644574445
Jsonstring : {"pv":"1","host":"www.aliyundoc.com","logtime":1644574445}
......
-------------Query is finished.-------------
Process finished with exit code 0
Exemplo 6: Consultar o número total de logs na última hora usando uma instrução SQL
Use *|select count(*) as count para obter o número total de logs da última hora.
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.LogItem;
import com.aliyun.openservices.log.common.QueriedLog;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.GetLogsRequest;
import com.aliyun.openservices.log.response.GetLogsResponse;
import java.util.Date;
public class GetLogsTest {
public static void main(String[] args) throws LogException {
// This example obtains the AccessKey ID and AccessKey secret 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 project = "your-project-name";
// Set the endpoint for Simple Log Service. This example uses the endpoint of the China (Hangzhou) region. Replace the value with the actual endpoint.
String host = "cn-hangzhou.log.aliyuncs.com";
// Enter the Logstore name.
String logStore = "your-logstore-name";
// Create a Simple Log Service client.
Client client = new Client(host, accessId, accessKey);
// Execute a query in the specified Logstore.
try {
// Query the total number of logs.
String query = "*|select count(*) as count";
// Set the query time range to 1 hour (3,600 seconds).
int from = (int) (new Date().getTime() / 1000 - 3600);
int to = (int) (new Date().getTime() / 1000);
int offset = 0;
int line = 200;
// In this example, the SQL statement in the query parameter is used to query the total number of logs in the specified time range. The line and offset parameters are invalid for SQL queries.
GetLogsRequest request = new GetLogsRequest(project, logStore, from, to, "", query,line,offset,true);
GetLogsResponse logsResponse = client.GetLogs(request);
System.out.println("-------------Query is started.-------------");
System.out.println("Returned query result count :" + logsResponse.GetCount());
System.out.println("from time is :" + from);
System.out.println("to time is :" + to);
for (QueriedLog log : logsResponse.getLogs()) {
LogItem item = log.GetLogItem();
System.out.println("log time : " + item.mLogTime);
System.out.println("Jsonstring : " + item.ToJsonString());
}
System.out.println("-------------Query is finished.-------------");
} 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;
}
}
}
O número total de logs na última hora é 19.051. Resposta:
from time is :1675041679
to time is :1675045279
Returned sql result count :1
Jsonstring : {"count":"19051","logtime":1675041679}
-------------Query is finished.-------------
Referências
Além de seu SDK nativo, o SLS também é compatível com os SDKs comuns da Alibaba Cloud. Para obter mais informações, consulte Simple Log Service_SDK Center_Alibaba Cloud OpenAPI Explorer.