Android

Before you get stated, you need to get an API key by signing up here.

We'd recommend that you use the code below to integrate this API into your Android app, but you can always just call our HTTP endpoint directly, at:

http://www.remotelogcat.com/log.php?apikey=55db5b0903944&channel=android&log=ok

First of all, request a permission to access network, add following to your manifest:

<uses-permission android:name="android.permission.INTERNET" />

Then add a class to your project, for example RemoteLogCat.java as follows:

package com.remotelogcat.log; // Change this to your package name

import android.os.AsyncTask;
import android.util.Log;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class RemoteLogCat extends AsyncTask<String, String, String> {

    public RemoteLogCat(){

    }

    public RemoteLogCat(String apikey){
        this.apikey = apikey;
    }

    public String apikey = "";


    public void log(String channel,String message)
    {
        try {
            this.execute("http://www.remotelogcat.com/log.php?apikey=" + apikey +
                    "&channel=" + URLEncoder.encode(channel, "utf-8") +
                    "&log=" + URLEncoder.encode(message, "utf-8"));
            Log.i(channel, message);
        }
        catch(UnsupportedEncodingException ex){

        }
    }

    public void log(String channel,String message,String apikey)
    {
        this.apikey = apikey;
        this.log(channel, message);
    }

    @Override
    protected String doInBackground(String... uri) {
        String responseString = "";
        try {
            URL url = new URL(uri[0]);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            InputStream in = new BufferedInputStream(urlConnection.getInputStream());
            responseString = new String(readFully(in), "utf-8");
        }
        catch(IOException ex)
        {
            responseString = ex.toString();
        }
        return responseString;
    }

    private byte[] readFully(InputStream inputStream)
            throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int length = 0;
        while ((length = inputStream.read(buffer)) != -1) {
            baos.write(buffer, 0, length);
        }
        return baos.toByteArray();
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
    }
}

You then can make a request by:

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Change the API Key below to your own key!!

        // In order to log in one line use this:
        new RemoteLogCat().log("android","test A","55db5b0903944");

        // or in two lines like this
        RemoteLogCat logcat = new RemoteLogCat("55db5b0903944");
        logcat.log("android","test B");

        TextView t = (TextView)findViewById(R.id.lblText);
        t.setText("Logging complete.");

    }

Download a sample app for Android Studio

Android - Alternative approach (Eclair / Froyo)

First of all, request a permission to access network, add following to your manifest:

<uses-permission android:name="android.permission.INTERNET" />

To make a simple log call then use:

    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response = httpclient.execute(new HttpGet(URL));
    StatusLine statusLine = response.getStatusLine();
    if(statusLine.getStatusCode() == HttpStatus.SC_OK){
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        response.getEntity().writeTo(out);
        String responseString = out.toString();
        out.close();
        //..more logic
    } else{
        //Closes the connection.
        response.getEntity().getContent().close();
        throw new IOException(statusLine.getReasonPhrase());
    }

If you need the logging to run in a background thread, for extra performance use:

class RequestTask extends AsyncTask<String, String, String>{

    @Override
    protected String doInBackground(String... uri) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response;
        String responseString = null;
        try {
            response = httpclient.execute(new HttpGet(uri[0]));
            StatusLine statusLine = response.getStatusLine();
            if(statusLine.getStatusCode() == HttpStatus.SC_OK){
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                response.getEntity().writeTo(out);
                responseString = out.toString();
                out.close();
            } else{
                //Closes the connection.
                response.getEntity().getContent().close();
                throw new IOException(statusLine.getReasonPhrase());
            }
        } catch (ClientProtocolException e) {
            //TODO Handle problems..
        } catch (IOException e) {
            //TODO Handle problems..
        }
        return responseString;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        //Do anything with response..
    }
}

You then can make a request by:

   new RequestTask().execute("http://www.remotelogcat.com/log.php?apikey=55db5b0903944&channel=Android&log=text");

Javascript (Phonegap / Cordova)

Create a Javascript file, for example "RemoteLogCat.js" with the following content:

Log = {
log : function (channel,message,apikey)
{
	if (apikey == undefined)
	{
		apikey = Log.apikey;
	}
	var nocache= Math.floor((Math.random() * 1000000) + 1);
	Log.addScript("http://www.remotelogcat.com/log.php?apikey=" + apikey + "&log=" + encodeURIComponent(message) + "&channel=" +encodeURIComponent(channel) + "&nocache=" + nocache);
},
apikey : "",
addScript : function(src){
		var s = document.createElement( 'script' );
		s.setAttribute( 'src', src );
		document.body.appendChild( s );
}
};

Then it can be called with either one or two lines of code:

// One liner
Log.log("javascript","log this click!","55db5b0903944");

// Two Liner
Log.apikey = "55db5b0903944";
Log.log("javascript","Two liner!");

Note: In Cordova 4+ you will need to relax the Content-Security-Policy meta in index.html, to something less restrictive like:

 <meta http-equiv="Content-Security-Policy" content="default-src * 'unsafe-inline' 'unsafe-eval'">


Exporting the entire logcat

Instead of selectively writing text to the remote log, you can also export the entire logcat in a thread. This would certainly be information overload, but for completeness, here's the code;

 new Thread(new Runnable() {
            @Override
            public void run() {
                try {

                        Process process = Runtime.getRuntime().exec("logcat");
                        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

                        String line;
                        while ((line = bufferedReader.readLine()) != null) {
                            new RemoteLogCat().log("android", line, "55db5b0903944");
                        }

                } catch (IOException e) {
                    new RemoteLogCat().log("android", "Failed to log", "55db5b0903944");
                }
            }
}).start();

You'll also need to add the following permission into the manifest:

<uses-permission android:name="android.permission.READ_LOGS"/>

- Note that post-jellybean, you can only read your own app's logs.

iOS Support with Swift

You can also use this code in Swift, by adding the following class to your project

import Foundation

class Logging
{
    static var Key : String?
    static func Log(Channel : String, Log : String, Completion: ((Bool) -> ())? = nil)
    {
        if let apiKey = Logging.Key
        {
            let strChannel = Channel.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!
            let strLog = Log.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!
            print("\(Channel):\(Log)")
            let url = URL(string: "http://www.remotelogcat.com/log.php?apikey=\(apiKey)&channel=\(strChannel)&log=\(strLog)")
            let task = URLSession.shared.dataTask(with: url!) {(data, response, error) in
                Completion?(error == nil)
            }
            task.resume()
        }
        else
        {
            print("No API Key set for RemoteLogCat.com API")
            Completion?(false)
        }
    }
}
Which can then be used as follows;
Logging.Key = "..."
Logging.Log(Channel: "macOS", Log: "Hello Log!")