SLS使用

public class SLSTest {

    private Client slsClient = new Client("cn-beijing.log.aliyuncs.com", "LTAI4GBpBFxtcht6pXbU6Vhn", "RqPo4ncVRmLYbKGaTaIGVUcBfENlb1");

    private String endPoint = "xxx";

    private String project = "xxx";

    private String logStore = "xxx";

    public ArrayList<QueriedLog> listLogsByTraceId(String traceId, String key) {


        if (traceId != null && !traceId.equals("")) {
            int from = (int) (System.currentTimeMillis() / 1000 - 60 * 60);
            int to = (int) (System.currentTimeMillis() / 1000);

            GetLogsResponse getLogsResponse = null;
            try {
                while (true) {
                    GetLogsRequest getLogsRequest = new GetLogsRequest(project, logStore,
                            (int) (System.currentTimeMillis() / 1000 - 60 * 60), (int) (System.currentTimeMillis() / 1000),
                            "",
                            "traceId: " + traceId + " and msg: " + key + "| select msg");
                    getLogsResponse = slsClient.GetLogs(getLogsRequest);
                    if (getLogsResponse != null && getLogsResponse.IsCompleted()) {
//                        完整日志,无需重复查询,跳出循环
                        break;
                    }
                }

                return getLogsResponse.GetLogs();
            } catch (LogException e) {
                e.printStackTrace();
            }
        }

        return new ArrayList();
    }

    @Test
    public void connectTest() {
        ArrayList<QueriedLog> listLogsByTraceId = listLogsByTraceId("xxx", "xxx");
        for (QueriedLog queriedLog : listLogsByTraceId) {
            ArrayList<LogContent> logContents = queriedLog.GetLogItem().GetLogContents();
            for (LogContent content : logContents) {
                System.out.println("进度:" + content.GetValue());
            }
        }

    }
}

Q.E.D.


Read The Fucking Source Code!