Android Log.X没有打印stacktrace

时间:2013-08-31 04:39:08

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

e.printStackTrace()工作正常(即将我的堆栈跟踪打印到stderr)但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)

2 个答案:

答案 0 :(得分:26)

关闭Android的Log.getStackTraceString,Log.X使用它来吞下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!

答案 1 :(得分:0)

我也遇到了此类问题,并创建了一个Xposed模块来克服此问题。有关安装说明,请参见Xposed for Android 8+Original Xposed。后来我偶然发现了这个线程。

这是项目:FullStackTrace

在发行后还会有一个APK下载。

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