【问题标题】:How to log OkHttp log to Crashlytics如何将 OkHttp 日志记录到 Crashlytics
【发布时间】:2019-08-21 04:25:24
【问题描述】:

我正在尝试使用 OkHttp 通过拦截登录到 Crashlytics,因为我的一些客户收到 500 错误,我想完成他们在响应中发布和获得的内容的详细信息。就像 OkHttp 在 Android Studio 的日志中显示的那样。

当 200 错误发生但没有成功时,我尝试记录详细信息。

@Module
public class DIModule {

MyApplication application;

public DIModule(MyApplication application) {
    this.application = application;
}
public DIModule()
{

}

@Singleton
@Provides
OkHttpClient getOkHttpClient() {
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);
    return new OkHttpClient.Builder()
            .addInterceptor(logging).addInterceptor(new HeaderInterceptor())
            .build();
}
@Singleton
@Provides
Retrofit getRetro(OkHttpClient client) {

    return new Retrofit.Builder()
            .baseUrl(Api.BASE_URL)
            .client(client)
            .addConverterFactory(GsonConverterFactory.create()) //Here we are using the GsonConverterFactory to directly convert json data to object
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())

            .build();
}

@Singleton
@Provides
Context provideApplicationContext()
{
    return application;
}

@Singleton
@Provides
MSharedPref getSharedPreferences(Context app)
{
    return new MSharedPref(app);
}

class HeaderInterceptor implements Interceptor
{
    @Override
    public Response intercept(Chain chain) throws IOException
    {
        Request.Builder builder = chain.request().newBuilder();

        String token="No Value";
        if(StaticData.loginResponse!=null)
            token=StaticData.loginResponse.getAccess_token();

        builder.header("Authorization", "Bearer "+ token)
                .header("Accept", "application/json")
                .header("Content-Type", "application/json");

        Response response=chain.proceed(builder.build());

        if(response.code()==200)
        {
            Log.d("test67","ooooooooooooooooooo"+response);
            //here I will log
        }

        return response;

    }
}

}

Log.d() 正在显示 响应{protocol=http/1.1, code=200, message=OK, url=http://........./api/user}

但我想记录类似

到 crashlytics。

我会从响应中得到所有 OkHttp 的日志,还是我走错了路。 Crashlytics.logException(e); 的非致命日志记录也只需要 Throwable。那我该怎么做呢?

请帮忙..

【问题讨论】:

  • 你找到解决办法了吗?
  • 我还希望记录对 Firebase Crashlytics 的改造响应(400 和 500),但还没有找到在改造级别添加的方法,因此它是可扩展的。有这方面的更新吗?

标签: android rx-java retrofit2 crashlytics okhttp


【解决方案1】:

这是我为处理此案所做的工作。 首先,我创建了一个自定义日志记录异常,它接受一个名为 CustomLoggingException 的请求对象,它只调用异常主构造函数,并将请求对象作为字符串

public class CustomLoggingException extends Exception {
public CustomLoggingException(Request builderRequest, String body) {
    super(builderRequest.toString() + body);
}}

注意,我也将正文作为字符串传递,因为您会遇到请求字符串不记录正文内容而只记录对象类型的问题。

所以我正在检查正文,如果它不为空,然后获取它的字符串内容。

if (builderRequest.body() != null) 
        body = stringifyRequestBody(builderRequest.body());

还有 stringifyRequestBody 函数,它将把你的正文作为字符串获取

private String stringifyRequestBody(RequestBody body) {
    try {
        BufferedSink buffer = new Buffer();
        body.writeTo(buffer);
        return buffer.getBuffer().readUtf8();
    } catch (Exception e) {
        Logger.d("Failed to stringify request body: " + e.getMessage());
        return "";
    }
}

之后,您只需使用这两个输入将该异常记录到 Crashlytics

Crashlytics.logException(new CustomLoggingException(builderRequest, body));

【讨论】:

    【解决方案2】:

    如果您使用 Crashlytics,您还可以使用 Crashlytics.log(String) 记录消息,并且这些消息仅在发生致命错误或您使用 Crashlytics.recordException(throwable) 手动记录和异常时才会发送

    您可以在发生 500 错误时记录一个 throwable,并且您之前记录的所有消息都将在 Crashlytics 中报告的非致命错误的日志选项卡中找到

    这是一个如何将 Okhttp 请求记录到字符串的示例

       private fun stringifyRequestBody(request: Request): String? {
        if (request.body != null) {
            return try {
                val copy = request.newBuilder().build()
                val buffer = Buffer()
                copy.body!!.writeTo(buffer)
                buffer.readUtf8()
            } catch (e: IOException) {
                "failed to stringify Request body"
            }
        }
        return ""
    }
    
    
      fun logRequest(request: Request) {
        if (!BuildConfig.DEBUG) {
            crashlytics.log("Request$request")
            crashlytics.log("RequestBody${stringifyRequestBody(request)}")
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-11
      • 2016-02-26
      • 1970-01-01
      • 2017-08-28
      • 1970-01-01
      相关资源
      最近更新 更多