【问题标题】:Make HttpLoggingInterceptor do not log images使 HttpLoggingInterceptor 不记录图像
【发布时间】:2018-03-02 14:06:21
【问题描述】:

我正在尝试清除日志中的垃圾,例如

*�$ʞx���J/

当我收到图片时

所以我尝试覆盖 HttpLoggingInterceptor intercept(),以检测是否有 Content-Type => image/jpeg 标头作为响应,但 HttpLoggingInterceptor 是最终的,所以我无法扩展它:(

RetrofitModule 中的代码:

OkHttpClient provideOkHttpClient(Context context, Application app, Preferences preferences) {
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(BuildConfig.DEBUG ? HttpLoggingInterceptor.Level.BODY : HttpLoggingInterceptor.Level.HEADERS);

        Cache cache = new Cache(app.getCacheDir(), cacheSize);

        return new OkHttpClient.Builder()
                .addNetworkInterceptor(new OkHttpInterceptor(context, preferences))
                .addInterceptor(loggingInterceptor)
                .cache(cache)
                .build();
    }

如何在我的项目中禁用图像记录?

【问题讨论】:

  • 不敢相信所有很酷的开发人员都忽略了日志中的垃圾:(

标签: android kotlin logging retrofit okhttp


【解决方案1】:

您可以使用OkHttpLogger 类打印没有二进制数据的日志:

class OkHttpLogger : HttpLoggingInterceptor.Logger {
    override fun log(message: String) {
        okHttpLog(message)
    }

    private fun okHttpLog(message: String, level: Int = Log.DEBUG, t: Throwable? = null) {
        val maxLogLength = 4000
        val tag = "OkHttp"
        val encoder = Charset.forName("ISO-8859-1").newEncoder()

        var logMessage = message
        if (t != null) logMessage = logMessage + '\n'.toString() + Log.getStackTraceString(t)

        // Split by line, then ensure each line can fit into Log's maximum length.
        var i = 0
        val length = logMessage.length
        var isBinaryLogDisplayed = false
        var isBinaryContentType = false
        while (i < length) {
            var newline = logMessage.indexOf('\n', i)
            newline = if (newline != -1) newline else length
            do {
                val end = minOf(newline, i + maxLogLength)
                val msg = logMessage.substring(i, end).trim()

                if (msg.contains("Content-Type") &&
                    msg.contains("application/octet-stream")) { // use another Content-Type if need
                    isBinaryContentType = true
                }
                val isBinaryData = !encoder.canEncode(msg)

                // multipart boundary
                if (isBinaryLogDisplayed && msg.startsWith("--")) {
                    isBinaryContentType = false
                    isBinaryLogDisplayed = false
                }

                // don't print binary data
                if (isBinaryContentType && isBinaryData && !isBinaryLogDisplayed) {
                    Log.println(level, tag, "<BINARY DATA>")
                    isBinaryLogDisplayed = true
                }

                if (!isBinaryLogDisplayed) {
                    Log.println(level, tag, msg)
                }

                i = end
            } while (i < newline)
            i++
        }
    }
}

要使用它将实例传递给HttpLoggingInterceptor 的构造函数:

val httpLoggingInterceptor = HttpLoggingInterceptor(OkHttpLogger()).apply {
    level = HttpLoggingInterceptor.Level.BODY
}

【讨论】:

    【解决方案2】:

    所以,既然没有人知道我自己发明了自行车:

        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                if(!message.contains("�")){
                    Timber.d(message);
                }
            }
        });
    

    不确定 String.contains() 是否足够便宜,可以像这样使用它,但已经达到目标

    【讨论】:

      猜你喜欢
      • 2019-12-16
      • 1970-01-01
      • 2017-02-26
      • 2017-08-07
      • 2016-03-31
      • 1970-01-01
      • 2010-09-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多