Feat(Logger): Support persistent logging
This commit is contained in:
@@ -2,11 +2,19 @@ package top.fatweb.oxygen.toolbox
|
||||
|
||||
import android.app.Application
|
||||
import dagger.hilt.android.HiltAndroidApp
|
||||
import timber.log.Timber
|
||||
import top.fatweb.oxygen.toolbox.repository.userdata.UserDataRepository
|
||||
import top.fatweb.oxygen.toolbox.util.OxygenLogTree
|
||||
import javax.inject.Inject
|
||||
|
||||
@HiltAndroidApp
|
||||
class OxygenApplication : Application() {
|
||||
@Inject
|
||||
lateinit var userDataRepository: UserDataRepository
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
|
||||
Timber.plant(if (BuildConfig.DEBUG) Timber.DebugTree() else OxygenLogTree(this))
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import okhttp3.logging.HttpLoggingInterceptor
|
||||
import top.fatweb.oxygen.toolbox.BuildConfig
|
||||
import top.fatweb.oxygen.toolbox.data.network.OxygenNetworkDataSource
|
||||
import top.fatweb.oxygen.toolbox.network.retrofit.RetrofitOxygenNetwork
|
||||
import top.fatweb.oxygen.toolbox.util.HttpLogger
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Module
|
||||
@@ -27,11 +28,10 @@ internal object NetworkModule {
|
||||
fun okHttpCallFactory(): Call.Factory =
|
||||
OkHttpClient.Builder()
|
||||
.addInterceptor(
|
||||
HttpLoggingInterceptor()
|
||||
HttpLoggingInterceptor(HttpLogger())
|
||||
.apply {
|
||||
if (BuildConfig.DEBUG) {
|
||||
level = HttpLoggingInterceptor.Level.BODY
|
||||
}
|
||||
level =
|
||||
if (BuildConfig.DEBUG) HttpLoggingInterceptor.Level.BODY else HttpLoggingInterceptor.Level.BASIC
|
||||
}
|
||||
)
|
||||
.build()
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
package top.fatweb.oxygen.toolbox.util
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import androidx.annotation.IntDef
|
||||
import okhttp3.logging.HttpLoggingInterceptor
|
||||
import timber.log.Timber
|
||||
import java.io.File
|
||||
import java.time.LocalDateTime
|
||||
import java.time.format.DateTimeFormatter
|
||||
|
||||
class HttpLogger : HttpLoggingInterceptor.Logger {
|
||||
override fun log(message: String) {
|
||||
Timber.i(message)
|
||||
}
|
||||
}
|
||||
|
||||
class OxygenLogTree(
|
||||
context: Context
|
||||
) : Timber.DebugTree() {
|
||||
@IntDef(Log.ASSERT, Log.ERROR, Log.WARN, Log.INFO, Log.DEBUG, Log.VERBOSE)
|
||||
@Retention(
|
||||
AnnotationRetention.SOURCE
|
||||
)
|
||||
annotation class Level
|
||||
|
||||
private val fileNamePrefix = "oxygen_toolbox"
|
||||
private val fileNamePostfix = ".log"
|
||||
private val maxFileSize: Long = 10 * 1024 * 1024
|
||||
private val dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
|
||||
private val logDir = File(context.cacheDir, "logs")
|
||||
private val logFile = File(logDir, "$fileNamePrefix$fileNamePostfix")
|
||||
|
||||
@SuppressLint("LogNotTimber")
|
||||
override fun log(@Level priority: Int, tag: String?, message: String, t: Throwable?) {
|
||||
checkFile()
|
||||
try {
|
||||
if (logFile.length() >= maxFileSize) {
|
||||
rotateLogFile()
|
||||
}
|
||||
logFile.appendText(format(priority, tag, message, t))
|
||||
} catch (e: Exception) {
|
||||
Log.e("OxygenLogTree", "Error writing log message to file", e)
|
||||
}
|
||||
}
|
||||
|
||||
private fun checkFile() {
|
||||
try {
|
||||
if (!logDir.exists()) {
|
||||
logDir.mkdirs()
|
||||
}
|
||||
logFile.createNewFile()
|
||||
} catch (_: Exception) {}
|
||||
}
|
||||
|
||||
private fun format(@Level priority: Int, tag: String?, message: String, t: Throwable?) = "${
|
||||
LocalDateTime.now()
|
||||
.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"))
|
||||
}\t$tag\t${
|
||||
when (priority) {
|
||||
Log.ASSERT -> "Assert: "
|
||||
Log.DEBUG -> "Debug: "
|
||||
Log.ERROR -> "Error: "
|
||||
Log.INFO -> "Info: "
|
||||
Log.VERBOSE -> "Verbose: "
|
||||
Log.WARN -> "Warn: "
|
||||
else -> "Unknown: "
|
||||
}
|
||||
} $message${t?.run { " ${toString()}" } ?: ""}\n"
|
||||
|
||||
private fun rotateLogFile() {
|
||||
logFile.renameTo(
|
||||
File(
|
||||
logFile.parent,
|
||||
"$fileNamePrefix${LocalDateTime.now().format(dateFormat)}$fileNamePostfix"
|
||||
)
|
||||
)
|
||||
logFile.createNewFile()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user