Updated on 2026-08-14
This commit is contained in:
parent
91c2fb2d58
commit
d718cf557d
18 changed files with 1135 additions and 280 deletions
|
|
@ -4,34 +4,6 @@ import android.util.Log
|
|||
import com.ihsanbal.logging.Level
|
||||
import com.ihsanbal.logging.LoggingInterceptor
|
||||
import okhttp3.Interceptor
|
||||
import okhttp3.OkHttpClient
|
||||
import retrofit2.Retrofit
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
@Suppress("MagicNumber")
|
||||
@Deprecated("Create and provide by DI")
|
||||
fun createRetrofitInstance(
|
||||
baseUrl: String,
|
||||
okHttpBuilder: OkHttpClient.Builder = OkHttpClient.Builder(),
|
||||
interceptors: List<Interceptor> = emptyList(),
|
||||
logEnabled: Boolean,
|
||||
): Retrofit {
|
||||
okHttpBuilder.apply {
|
||||
callTimeout(10, TimeUnit.SECONDS)
|
||||
connectTimeout(20, TimeUnit.SECONDS)
|
||||
readTimeout(20, TimeUnit.SECONDS)
|
||||
writeTimeout(20, TimeUnit.SECONDS)
|
||||
}
|
||||
interceptors.forEach { okHttpBuilder.addInterceptor(it) }
|
||||
|
||||
if (logEnabled) okHttpBuilder.addInterceptor(createNetworkLoggingInterceptor())
|
||||
|
||||
return Retrofit.Builder()
|
||||
.baseUrl(baseUrl)
|
||||
.addConverterFactory(MoshiConverter.networkMoshiConverter)
|
||||
.client(okHttpBuilder.build())
|
||||
.build()
|
||||
}
|
||||
|
||||
fun createNetworkLoggingInterceptor(): Interceptor {
|
||||
return LoggingInterceptor.Builder()
|
||||
|
|
|
|||
|
|
@ -70,12 +70,17 @@ class AppLogsStore @Inject constructor(
|
|||
}
|
||||
}
|
||||
|
||||
/** Save log [message] */
|
||||
fun saveLogMessage(tag: String, message: String) {
|
||||
/**
|
||||
* Save log [message]. Pass [shouldSanitize] = false to bypass [LogsSanitizer].
|
||||
* The optional [throwable]'s stack trace is appended verbatim (never sanitized),
|
||||
* since stack traces routinely contain hex-like sequences that the sanitizer would
|
||||
* otherwise destroy.
|
||||
*/
|
||||
fun saveLogMessage(tag: String, message: String, throwable: Throwable? = null, shouldSanitize: Boolean = true) {
|
||||
launchWithLock {
|
||||
createFileIfNotExist()
|
||||
|
||||
writeMessage(tag = tag, message)
|
||||
writeMessage(tag = tag, shouldSanitize = shouldSanitize, throwable = throwable, message)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -84,7 +89,7 @@ class AppLogsStore @Inject constructor(
|
|||
launchWithLock {
|
||||
createFileIfNotExist()
|
||||
|
||||
writeMessage(tag = tag, *messages)
|
||||
writeMessage(tag = tag, shouldSanitize = true, throwable = null, messages = messages)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -97,24 +102,16 @@ class AppLogsStore @Inject constructor(
|
|||
}
|
||||
}
|
||||
|
||||
fun deleteOldLogsFile() {
|
||||
val file = File(applicationContext.filesDir, LOG_FILE_NAME)
|
||||
|
||||
if (file.exists()) file.delete()
|
||||
}
|
||||
|
||||
fun deleteLastLogFile() {
|
||||
val file = File(applicationContext.filesDir, NEW_LOG_FILE_NAME)
|
||||
|
||||
if (file.exists()) file.delete()
|
||||
}
|
||||
|
||||
private fun writeMessage(tag: String, vararg messages: String) {
|
||||
private fun writeMessage(tag: String, shouldSanitize: Boolean, throwable: Throwable?, vararg messages: String) {
|
||||
BufferedWriter(FileWriter(logFile, true)).use { writer ->
|
||||
writer.append(formatter.print(DateTime.now()))
|
||||
writer.append(": $tag ")
|
||||
messages.map(LogsSanitizer::sanitize)
|
||||
.forEach(writer::append)
|
||||
val processed = if (shouldSanitize) messages.map(LogsSanitizer::sanitize) else messages.toList()
|
||||
processed.forEach(writer::append)
|
||||
if (throwable != null) {
|
||||
writer.newLine()
|
||||
writer.append(throwable.stackTraceToString().trimEnd())
|
||||
}
|
||||
writer.newLine()
|
||||
}
|
||||
}
|
||||
|
|
@ -166,8 +163,6 @@ class AppLogsStore @Inject constructor(
|
|||
private companion object {
|
||||
const val BUFFER_SIZE = 1024
|
||||
|
||||
const val LOG_FILE_NAME = "logs.txt"
|
||||
const val NEW_LOG_FILE_NAME = "app_logs.txt"
|
||||
// the only name that we allow to send as email to company addresses
|
||||
const val PERMITTED_FILE_NAME = "log.txt"
|
||||
const val PERMITTED_FILE_NAME_ZIP = "log.zip"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue