【问题标题】:Android Log.X not printing stacktraceAndroid Log.X 不打印堆栈跟踪
【发布时间】:2013-09-03 20:52:37
【问题描述】:

e.printStackTrace() 工作正常(即将我的堆栈跟踪打印到标准错误)但 Log.X 根本无法打印堆栈跟踪。

例如:

} catch (IOException e) {
    Log.e("Network", "Exception", e);
    e.printStackTrace();
}

输出:

08-31 03:46:21.992: W/Network(13238): Exception
08-31 03:46:22.092: W/System.err(13238): java.net.UnknownHostException: Unable to resolve host "...": No address associated with hostname
08-31 03:46:22.204: W/System.err(13238):    at java.net.InetAddress.lookupHostByName(InetAddress.java:394)
08-31 03:46:22.222: W/System.err(13238):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
08-31 03:46:22.222: W/System.err(13238):    at java.net.InetAddress.getAllByName(InetAddress.java:214)

【问题讨论】:

    标签: android exception logging stack-trace android-logcat


    【解决方案1】:

    原来,Log.X 使用的 Android 的 Log.getStackTraceString 吞下了 UnknownHostException。 :(

    发件人:http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.2.2_r1/android/util/Log.java#Log.getStackTraceString%28java.lang.Throwable%29

    public static String getStackTraceString(Throwable tr) {
        if (tr == null) {
            return "";
        }
    
        // This is to reduce the amount of log spew that apps do in the non-error
        // condition of the network being unavailable.
        Throwable t = tr;
        while (t != null) {
            if (t instanceof UnknownHostException) {
                return "";
            }
            t = t.getCause();
        }
    
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        tr.printStackTrace(pw);
        return sw.toString();
    }
    

    这一切都很好地减少了日志溢出,但甚至不告诉我我的异常是什么是糟糕的 joojoo!

    【讨论】:

    • 嗯,这就解释了。我认为这是 Android 中的一个错误。
    • 这简直是愚蠢、愚蠢和反开发者 - 那么如果应用程序记录 UnknownHostException 怎么办?这可能是有原因的!以这种愚蠢的方式隐藏了多少其他异常??? Android 太难开发了。
    • 对于其他在空堆栈跟踪中磕磕绊绊的人来说,奇怪的是,7 年后这仍然是一个正确的答案。
    【解决方案2】:

    我也遇到过这种问题,并创建了一个 Xposed 模块来解决这个问题。有关安装说明,请参阅 Xposed for Android 8+Original Xposed。后来我偶然发现了这个线程。

    这里是项目:FullStackTrace

    还有一个 apk 供在发布后下载。

    您的设备必须植根才能使用 Xposed。我在这里使用了Magisk

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-27
      • 2016-03-16
      • 2020-07-25
      • 2018-07-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多