diff --git a/app/src/main/java/com/tangem/tap/TapApplication.kt b/app/src/main/java/com/tangem/tap/TapApplication.kt index 28cd0f57de..e44d8366ab 100644 --- a/app/src/main/java/com/tangem/tap/TapApplication.kt +++ b/app/src/main/java/com/tangem/tap/TapApplication.kt @@ -41,6 +41,7 @@ import com.tangem.tap.common.chat.ChatManager import com.tangem.tap.common.feedback.AdditionalFeedbackInfo import com.tangem.tap.common.feedback.FeedbackManager import com.tangem.tap.common.images.createCoilImageLoader +import com.tangem.tap.common.log.TimberFormatStrategy import com.tangem.tap.common.log.TangemLogCollector import com.tangem.tap.common.redux.AppState import com.tangem.tap.common.redux.appReducer @@ -189,7 +190,7 @@ class TapApplication : Application(), ImageLoaderFactory { ) if (BuildConfig.DEBUG) { - Logger.addLogAdapter(AndroidLogAdapter()) + Logger.addLogAdapter(AndroidLogAdapter(TimberFormatStrategy())) Timber.plant( object : Timber.DebugTree() { override fun log(priority: Int, tag: String?, message: String, t: Throwable?) { diff --git a/app/src/main/java/com/tangem/tap/common/log/TimberFormatStrategy.kt b/app/src/main/java/com/tangem/tap/common/log/TimberFormatStrategy.kt new file mode 100644 index 0000000000..0a43f525d4 --- /dev/null +++ b/app/src/main/java/com/tangem/tap/common/log/TimberFormatStrategy.kt @@ -0,0 +1,63 @@ +package com.tangem.tap.common.log + +import com.orhanobut.logger.FormatStrategy +import com.orhanobut.logger.LogStrategy +import com.orhanobut.logger.LogcatLogStrategy + +class TimberFormatStrategy : FormatStrategy { + + private val logStrategy: LogStrategy = LogcatLogStrategy() + + override fun log(priority: Int, tag: String?, message: String) { + logTopBorder(priority, tag) + val bytes = message.toByteArray() + val length = bytes.size + if (length <= CHUNK_SIZE) { + logContent(priority, tag, message) + logBottomBorder(priority, tag) + return + } + var i = 0 + while (i < length) { + val count = (length - i).coerceAtMost(CHUNK_SIZE) + // create a new String with system's default charset (which is UTF-8 for Android) + logContent(priority, tag, String(bytes, i, count)) + i += CHUNK_SIZE + } + logBottomBorder(priority, tag) + } + + private fun logTopBorder(logType: Int, tag: String?) { + logChunk(logType, tag, TOP_BORDER) + } + + private fun logBottomBorder(logType: Int, tag: String?) { + logChunk(logType, tag, BOTTOM_BORDER) + } + + private fun logContent(logType: Int, tag: String?, chunk: String) { + chunk.split(System.lineSeparator()).forEach { line -> + logChunk(logType, tag, "$HORIZONTAL_LINE $line") + } + } + + private fun logChunk(priority: Int, tag: String?, chunk: String) { + logStrategy.log(priority, tag, chunk) + } + + private companion object { + /** + * Android's max limit for a log entry is ~4076 bytes, + * so 4000 bytes is used as chunk size since default charset + * is UTF-8 + */ + private const val CHUNK_SIZE = 4000 + + const val TOP_LEFT_CORNER = "┌" + const val BOTTOM_LEFT_CORNER = "└" + const val HORIZONTAL_LINE = "│" + const val DOUBLE_DIVIDER = "────────────────────────────────────────────────────────" + const val TOP_BORDER = TOP_LEFT_CORNER + DOUBLE_DIVIDER + DOUBLE_DIVIDER + const val BOTTOM_BORDER = BOTTOM_LEFT_CORNER + DOUBLE_DIVIDER + DOUBLE_DIVIDER + } +} \ No newline at end of file diff --git a/core/datasource/src/main/java/com/tangem/datasource/api/common/Retrofit.kt b/core/datasource/src/main/java/com/tangem/datasource/api/common/Retrofit.kt index 3d8b6f3930..830cc43c53 100644 --- a/core/datasource/src/main/java/com/tangem/datasource/api/common/Retrofit.kt +++ b/core/datasource/src/main/java/com/tangem/datasource/api/common/Retrofit.kt @@ -37,5 +37,8 @@ fun createNetworkLoggingInterceptor(): Interceptor { return LoggingInterceptor.Builder() .setLevel(Level.BODY) .log(Log.VERBOSE) + .tag(NETWORK_LOGS_TAG) .build() -} \ No newline at end of file +} + +private const val NETWORK_LOGS_TAG = "NetworkLogs" \ No newline at end of file