【问题标题】:How to increase console output at the android Log class如何在 android Log 类中增加控制台输出
【发布时间】:2011-06-07 19:17:27
【问题描述】:

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

我还没有找到比这更好的解决方案:

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

}

这个问题有更好的解决方案吗?

【问题讨论】:

  • 您是否要包装日志输出的文本?
  • 否,默认登录 android 平台的控制台输出字符数有限。大约等于 3000 多一点。因此,如果消息超过 3000 个字符,则不会显示在屏幕上。
  • logcat 的内核缓冲区拥有 64KB 的数据。在较低级别的日志记录功能中,每行有 1024 个字符的限制;之后,该行被截断。我不知道“3000”是从哪里来的,也不知道为什么某些东西会完全消失。或者为什么您尝试在一行上记录超过 3K 个字符的消息。
  • 有必要例如打印 JSON 响应
  • 这正是我试图做的,输出 GSON 消息,但它们被截断,因此难以调试。

标签: java android buffer logging


【解决方案1】:

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

它可能没有你的那么复杂,但它看起来很容易,并且不会使代码过于混乱。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-19
    • 2016-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多