本文介绍了Android Log.X不打印stacktrace的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

e.printStackTrace()可以正常工作(即将我的堆栈跟踪信息打印到stderr),但是Log.X根本无法打印堆栈跟踪信息.

e.printStackTrace() works fine (i.e. prints my stacktrace to stderr) but Log.X fails to print a stacktrace at all.

例如:

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

推荐答案

显示出Log.X使用的Android Log.getStackTraceString吞噬UnknownHostException. :(

Turns out Android's Log.getStackTraceString which is used by Log.X swallows UnknownHostException. :(

来自:

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!

It's all very well reducing log spew but to not even tell me what my exception was is bad joojoo!

这篇关于Android Log.X不打印stacktrace的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 19:45
查看更多