开发者

How to increase console output at the android Log class

开发者 https://www.devze.com 2023-02-03 15:48 出处:网络
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

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));
                }
            } el开发者_开发问答se {
                android.util.Log.d(tag, msg);
            }
    }

}

Is there a better solution to this issue?


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

精彩评论

暂无评论...
验证码 换一张
取 消