抛出异常并在 logcat 级别 "error" 中打印其堆栈跟踪

Throw an exception and print its stacktrace in the logcat at "error" level

我有一个可抛出的对象,我想在 logcat 中打印它的堆栈跟踪,日志级别为“Log.e”(错误)。我的方法抛出异常,但堆栈跟踪打印为警告。

这是我的代码:

@Throws(RuntimeException::class)
fun throwErrorIfNotHttp (e: Throwable) {
    if (e is HttpException) {
        val code = e.code()
    } else if (e is SocketTimeoutException) {
        Log.e("time out", "time out")
    } else if (e is IOException) {
        Log.e("File error", "file error")
    } else {
       throw RuntimeException(e) // This  should appear as error
    }
}

这是异常图像。

编辑

我的扩展函数:

fun <T> Observable<Response<T>>.makeRequest(
    context: Context,
    executeOnError: ((Any?) -> Unit)? = null, executeOnSuccess: (T?) -> Unit
): Disposable? {

   val disposable: Disposable = this
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe({
            if (it.isSuccessful) {
                executeOnSuccess(it?.body())
            } else {
                val error = getExceptionFromResponse(it, context)
                 handleError(error, context)

            }
        }, {
           throwErrorIfNotHttp(it)            
        })

    return disposable

}

如何让它在错误选项卡而不是警告选项卡中显示?

尝试添加到您的 else 语句

Log.e(TAG, "mymessage",e);

完整代码:

@Throws(RuntimeException::class)
fun throwErrorIfNotHttp (e: Throwable) {
    if (e is HttpException) {
            val code = e.code()
    } else if (e is SocketTimeoutException) {
            Log.e(TAG, "time out")
    } else if (e is IOException) {
            Log.e(TAG, "file error")
    } else {
      Log.e(TAG, "mymessage",e);
      throw RuntimeException(e) // This  should appear as error
    }
 }

companion object{
  val TAG = "class_tag"
}