diff --git a/app/build.gradle b/app/build.gradle index decdc20..b3253cd 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -75,13 +75,15 @@ dependencies { implementation 'androidx.preference:preference-ktx:1.2.0' implementation 'com.github.cyb3rko:QuickPermissions-Kotlin:1.1.2' - implementation 'com.hypertrack:hyperlog:0.0.10' implementation 'com.squareup.picasso:picasso:2.71828' implementation 'io.noties.markwon:core:4.6.2' implementation 'io.noties.markwon:image-picasso:4.6.2' implementation 'io.noties.markwon:image:4.6.2' implementation 'io.noties.markwon:ext-tables:4.6.2' implementation 'io.noties.markwon:ext-strikethrough:4.6.2' + + implementation 'org.tinylog:tinylog-api-kotlin:2.6.2' + implementation 'org.tinylog:tinylog-impl:2.6.2' } configurations { diff --git a/app/src/main/kotlin/com/github/gotify/init/InitializationActivity.kt b/app/src/main/kotlin/com/github/gotify/init/InitializationActivity.kt index fc9e743..c8b6de3 100644 --- a/app/src/main/kotlin/com/github/gotify/init/InitializationActivity.kt +++ b/app/src/main/kotlin/com/github/gotify/init/InitializationActivity.kt @@ -24,6 +24,7 @@ import com.github.gotify.api.ClientFactory import com.github.gotify.client.model.User import com.github.gotify.client.model.VersionInfo import com.github.gotify.log.Log +import com.github.gotify.log.LoggerHelper import com.github.gotify.log.UncaughtExceptionHandler import com.github.gotify.login.LoginActivity import com.github.gotify.messages.MessagesActivity @@ -47,7 +48,7 @@ internal class InitializationActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - Log.init(this) + LoggerHelper.init(this) val theme = PreferenceManager.getDefaultSharedPreferences(this) .getString(getString(R.string.setting_key_theme), getString(R.string.theme_default))!! ThemeHelper.setTheme(this, theme) diff --git a/app/src/main/kotlin/com/github/gotify/log/Format.kt b/app/src/main/kotlin/com/github/gotify/log/Format.kt deleted file mode 100644 index 578d231..0000000 --- a/app/src/main/kotlin/com/github/gotify/log/Format.kt +++ /dev/null @@ -1,16 +0,0 @@ -package com.github.gotify.log - -import android.content.Context -import com.hypertrack.hyperlog.LogFormat - -internal class Format(context: Context) : LogFormat(context) { - override fun getFormattedLogMessage( - logLevelName: String, - tag: String, - message: String, - timeStamp: String, - senderName: String, - osVersion: String, - deviceUuid: String - ) = "$timeStamp $logLevelName: $message" -} diff --git a/app/src/main/kotlin/com/github/gotify/log/Log.kt b/app/src/main/kotlin/com/github/gotify/log/Log.kt deleted file mode 100644 index a4f3b91..0000000 --- a/app/src/main/kotlin/com/github/gotify/log/Log.kt +++ /dev/null @@ -1,47 +0,0 @@ -package com.github.gotify.log - -import android.content.Context -import android.util.Log -import com.hypertrack.hyperlog.HyperLog - -internal object Log { - private const val TAG = "gotify" - - fun init(content: Context) { - HyperLog.initialize(content, Format(content)) - HyperLog.setLogLevel(Log.INFO) // TODO configurable - } - - fun get(): String { - val logs = HyperLog.getDeviceLogsAsStringList(false) - return logs.takeLast(200).reversed().joinToString("\n") - } - - fun e(message: String) { - HyperLog.e(TAG, message) - } - - fun e(message: String, e: Throwable) { - HyperLog.e(TAG, "$message\n${Log.getStackTraceString(e)}") - } - - fun i(message: String) { - HyperLog.i(TAG, message) - } - - fun i(message: String, e: Throwable) { - HyperLog.i(TAG, "$message\n${Log.getStackTraceString(e)}") - } - - fun w(message: String) { - HyperLog.w(TAG, message) - } - - fun w(message: String, e: Throwable) { - HyperLog.w(TAG, "$message\n${Log.getStackTraceString(e)}") - } - - fun clear() { - HyperLog.deleteLogs() - } -} diff --git a/app/src/main/kotlin/com/github/gotify/log/LoggerHelper.kt b/app/src/main/kotlin/com/github/gotify/log/LoggerHelper.kt new file mode 100644 index 0000000..cee7cca --- /dev/null +++ b/app/src/main/kotlin/com/github/gotify/log/LoggerHelper.kt @@ -0,0 +1,43 @@ +package com.github.gotify.log + +import android.content.Context +import java.io.File +import org.tinylog.kotlin.Logger + +class LoggerHelper { + companion object { + fun read(ctx: Context): String = folder(ctx) + .listFiles() + .orEmpty() + .flatMap { it.readLines() } + .fold(mutableListOf()) { newLines, line -> groupExceptions(newLines, line) } + .takeLast(200) + .reversed() + .joinToString(separator = "\n") + + private fun groupExceptions( + newLines: MutableList, + line: String + ): MutableList { + if (newLines.isNotEmpty() && (line.startsWith('\t') || line.startsWith("Caused"))) { + newLines[newLines.lastIndex] += '\n' + line + } else { + newLines.add(line) + } + return newLines + } + + fun clear(ctx: Context) { + folder(ctx).listFiles()?.forEach { it.writeText("") } + Logger.info("Logs cleared") + } + + fun init(ctx: Context) { + val file = folder(ctx) + file.mkdirs() + System.setProperty("tinylog.directory", file.absolutePath) + } + + private fun folder(ctx: Context): File = File(ctx.filesDir, "log") + } +} diff --git a/app/src/main/kotlin/com/github/gotify/service/WebSocketService.kt b/app/src/main/kotlin/com/github/gotify/service/WebSocketService.kt index 9b6d981..b702c2d 100644 --- a/app/src/main/kotlin/com/github/gotify/service/WebSocketService.kt +++ b/app/src/main/kotlin/com/github/gotify/service/WebSocketService.kt @@ -28,7 +28,7 @@ import com.github.gotify.client.api.ApplicationApi import com.github.gotify.client.api.MessageApi import com.github.gotify.client.model.Application import com.github.gotify.client.model.Message -import com.github.gotify.log.Log +import com.github.gotify.log.LoggerHelper import com.github.gotify.log.UncaughtExceptionHandler import com.github.gotify.messages.Extras import com.github.gotify.messages.IntentUrlDialogActivity @@ -88,7 +88,7 @@ internal class WebSocketService : Service() { } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { - Log.init(this) + LoggerHelper.init(this) if (connection != null) { connection!!.close() } diff --git a/app/src/main/resources/tinylog.properties b/app/src/main/resources/tinylog.properties new file mode 100644 index 0000000..51eddf7 --- /dev/null +++ b/app/src/main/resources/tinylog.properties @@ -0,0 +1,11 @@ +writer1 = logcat +writer1.level = trace +writer1.format = {message} + +writer2 = rolling file +writer2.level = info +writer2.file = #{tinylog.directory}/log_{count}.txt +writer2.backups = 2 +writer2.format = {date: yyyy-MM-dd HH:mm:ss.SSS} {level}: {message} +writer2.policies = size: 1MB +writer2.charset = UTF-8