Search code examples
androidsumologic

Log message to sumo logic from Android client


I've got a requirement to log message from the Android client. Is there any sumo logic API to log message from the Android application?


Solution

  • You can post your log message/ any message from your Android application to Summo Logic cloud-based log management.

    Summo Logic provides Web Services/ REST to perform POST, GET Request.

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

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

    For Instance: "https://endpoint1.collection.us2.sumologic.com/receiver/v1/http/SanTC12dhaV1oma90Vvb..."

    You can use Retorfit / Volley library for REST Communication .

    I have given a below pseudo code which conveys the basic REST Communication in background through Android Async Task.

    I strictly recommend to use the above mentioned libraries.

    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();
    }
    

    Async task

          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("");
    

    For more information, Please refer the documentation from Official Sumo Webpage

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