Home » Android » java – How to increase console output at the android Log class

java – How to increase console output at the android Log class

Posted by: admin June 15, 2020 Leave a comment

Questions:

For default Log on android platform has limited amount of character for console output. Around equal a bit more than 3000. Therefore, if the message is longer than 3000 characters, it is not shown on screen.

I have not found a better solution than this:

public class Log {
    private static int mode = android.util.Log.INFO;

    public static void e(String tag, String msg, Throwable tr) {
        if ((mode & android.util.Log.ERROR) <= 0) return;
        android.util.Log.e(tag, msg, tr);
    }

    public static void e(String tag, String msg) {
        if ((mode & android.util.Log.ERROR) <= 0) return;
        android.util.Log.e(tag, msg);
    }

    public static void w(String tag, String msg) {
        if ((mode & android.util.Log.WARN) <= 0) return;
        android.util.Log.w(tag, msg);
    }

    public static void i(String tag, String msg) {
        if ((mode & android.util.Log.INFO) <= 0) return;
        android.util.Log.i(tag, msg);
    }

    public static void d(String tag, String msg) {
        if ((mode & android.util.Log.DEBUG) <= 0) return;

            int length = msg.length();
            int kind = 3000;
            if (length >= kind) {
                int count = length / kind;
                int u = length % kind;
                int i = 0;
                for (i = 0; i < count; ++i) {
                    int start = i * kind;
                    int end = start + kind;
                    android.util.Log.d(tag, msg.substring(start, end));
                }
                if (u != 0) {
                    int start = length - u;
                    int end = start + u;
                    android.util.Log.d(tag, msg.substring(start, end));
                }
            } else {
                android.util.Log.d(tag, msg);
            }
    }

}

Is there a better solution to this issue?

How to&Answers:

A workaround for the 1024 character limit when doing large outputs (JSON etc)
is to make a simple for-loop, logging them in chunks.

It may not be as elaborate as yours, but it’s easy on the eyes and won’t clutter the code too much.

        String json = data.toString();
        int length = json.length();

        for(int i=0; i<length; i+=1024)
        {
            if(i+1024<length)
                Log.d("JSON OUTPUT", json.substring(i, i+1024));
            else
                Log.d("JSON OUTPUT", json.substring(i, length));
        }