从 Android 客户端向 sumo 逻辑记录消息

Log message to sumo logic from Android client

我需要记录来自 Android 客户端的消息。是否有任何 sumo 逻辑 API 来记录来自 Android 应用程序的消息?

您可以 post 您的日志消息/来自您的 Android 应用程序的任何消息到 Summo Logic cloud-based 日志管理。

Summo Logic 提供 Web 服务/REST 来执行 POST、GET 请求。

You just need to post your data on the request body and mention your Sumo collection endpoint as well as UniqueHTTPCollectorCode.

REST 服务/Web 服务:https://[SumoEndpoint]/receiver/v1/http/[UniqueHTTPCollectorCode]

例如: "https://endpoint1.collection.us2.sumologic.com/receiver/v1/http/SanTC12dhaV1oma90Vvb..."

您可以使用 Retorfit/Volley 库进行 REST 通信。

我给出了下面的伪代码,它通过 Android 异步任务在后台传达基本的 REST 通信。

我强烈推荐使用上面提到的库。

public static String performPostRequest(String summoUrl, String payload, 
    Context context) throws IOException {
    URL url = new URL(summoUrl);
    HttpURLConnection uc = (HttpURLConnection) url.openConnection();
    String line;
    StringBuffer jsonString = new StringBuffer();

    uc.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
    uc.setRequestMethod("POST");
    uc.setDoInput(true);
    uc.setInstanceFollowRedirects(false);
    uc.connect();
    OutputStreamWriter writer = new OutputStreamWriter(uc.getOutputStream(), "UTF-8");
    writer.write(payload);
    writer.close();
    try {
        BufferedReader br = new BufferedReader(new InputStreamReader(uc.getInputStream()));
        while((line = br.readLine()) != null){
            jsonString.append(line);
        }
        br.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    uc.disconnect();
    return jsonString.toString();
}

异步任务

      new AsyncTask<String, String, String>() {

            @Override
            protected String doInBackground(String... params) {
                try {
                    String response = makePostRequest(""https://endpoint1.collection.us2.sumologic.com/receiver/v1/http/ZaVnC4dhaV1oma90Vvb..."", 
         // Sample JSON Data               "
  {     \"organization": \"organization.name\",
        \"environment": \"environment.name\",
        \"apiProduct": \("apiproduct.name"),
        \"proxyName": \("apiproxy.name"),
        \"appName": \("developer.app.name"),
        \"verb": \("request.verb"),
        \"url": '' + \("client.scheme") + '://' + \("request.header.host") + \("request.uri"),
        \"responseCode": \("message.status.code"),
        \"responseReason": \("message.reason.phrase"),
        \"clientLatency": total_client_time,
        \"targetLatency": total_target_time,
        \"totalLatency": total_request_time
        }", getApplicationContext());
    // Hard coded Success as response from Server, replace with this as per your need
                    return "Success";
                } catch (IOException exception) {
                    exception.printStackTrace();
                    return exception.getMessage();
                }
            }

        }.execute("");

更多信息,请参考 Sumo 官方网页的文档

https://help.sumologic.com/Send-Data/Sources/02Sources-for-Hosted-Collectors/HTTP-Source/Upload-Data-to-an-HTTP-Source