如何在android Log类中增加控制台输出 [英] How to increase console output at the android Log class

查看:209
本文介绍了如何在android Log类中增加控制台输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于默认登录,在android平台上登录控制台输出的字符数量有限.大约等于3000多一点.因此,如果消息长于3000个字符,则该消息不会显示在屏幕上.

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?

推荐答案

处理大型输出(JSON等)时1024个字符限制的解决方法 是建立一个简单的for循环,将它们分块记录.

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

这篇关于如何在android Log类中增加控制台输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆