You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何取消HttpLoggingInterceptor/Logcat中被脱敏的HTTP日志?

取消HttpLoggingInterceptor日志脱敏的解决方案

问题背景

使用HttpLoggingInterceptor打印HTTP请求日志时,Logcat中的请求URL(原应为https://example.com)和JSON体里的lat、lng经纬度字段被自动脱敏为星号,相关配置及日志输出如下:

现有配置

Hilt Retrofit配置

@Provides
fun provideHttpLoggingInterceptor(): HttpLoggingInterceptor {
    return HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)
}

@Provides
fun provideOkHttpClient(
    authInterceptor: AuthInterceptor,
    loggingInterceptor: HttpLoggingInterceptor
): OkHttpClient {
    return OkHttpClient
        .Builder()
        .addInterceptor(loggingInterceptor)
        .addInterceptor(authInterceptor)
        .build()
}

脱敏日志输出

I/okhttp.OkHttpClient: --> POST ******************************************************************************************************************************
I/okhttp.OkHttpClient: Content-Type: application/json; charset=UTF-8
I/okhttp.OkHttpClient: Content-Length: 57
I/okhttp.OkHttpClient: {"battery_level":100.0,"**************,"****************}
I/okhttp.OkHttpClient: --> END POST (57-byte body)

依赖与构建配置

// 依赖配置
dependencies {
    ...
    implementation "com.squareup.retrofit2:retrofit:2.9.0"
    implementation "com.squareup.retrofit2:converter-gson:2.9.0"
    implementation "com.squareup.okhttp3:logging-interceptor:4.9.1"
}

// Debug构建配置
debug {
     applicationIdSuffix ".dbg"
     minifyEnabled false
     proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

     buildConfigField("String", "API_URL", "\"https://example.com\"")
}

解决步骤

1. 关闭Logcat自动脱敏功能

Android Studio默认会对敏感数据(如地理位置、URL隐私信息)自动脱敏,关闭方法:

  • 打开Logcat面板
  • 点击右上角齿轮图标(设置)
  • 取消勾选 "Redact sensitive information"

2. 排查自定义拦截器影响

临时移除AuthInterceptor后重新运行程序,检查日志是否恢复正常显示。如果恢复,说明该拦截器内部存在修改请求URL或请求体的逻辑,需调整其实现代码。

3. 自定义Logger规避默认脱敏

若系统脱敏仍生效,可自定义Logger直接输出日志,绕过Logcat的自动处理:

@Provides
fun provideHttpLoggingInterceptor(): HttpLoggingInterceptor {
    val customLogger = HttpLoggingInterceptor.Logger { msg ->
        Log.d("OkHttpDebug", msg)
    }
    return HttpLoggingInterceptor(customLogger).setLevel(HttpLoggingInterceptor.Level.BODY)
}

4. 升级依赖版本

当前logging-interceptor:4.9.1与Retrofit版本可能存在兼容性问题,尝试升级到稳定新版本:

dependencies {
    ...
    implementation "com.squareup.retrofit2:retrofit:2.9.1"
    implementation "com.squareup.retrofit2:converter-gson:2.9.1"
    implementation "com.squareup.okhttp3:logging-interceptor:4.10.0"
}

内容的提问来源于stack exchange,提问作者Aditya Chandra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 19:50:43