Configure tinylog

This commit is contained in:
Jannis Mattheis
2023-10-07 14:22:12 +02:00
parent 361c480a7c
commit 8adb3095f8
7 changed files with 61 additions and 67 deletions

View File

@@ -75,13 +75,15 @@ dependencies {
implementation 'androidx.preference:preference-ktx:1.2.0' implementation 'androidx.preference:preference-ktx:1.2.0'
implementation 'com.github.cyb3rko:QuickPermissions-Kotlin:1.1.2' implementation 'com.github.cyb3rko:QuickPermissions-Kotlin:1.1.2'
implementation 'com.hypertrack:hyperlog:0.0.10'
implementation 'com.squareup.picasso:picasso:2.71828' implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'io.noties.markwon:core:4.6.2' implementation 'io.noties.markwon:core:4.6.2'
implementation 'io.noties.markwon:image-picasso:4.6.2' implementation 'io.noties.markwon:image-picasso:4.6.2'
implementation 'io.noties.markwon:image: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-tables:4.6.2'
implementation 'io.noties.markwon:ext-strikethrough: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 { configurations {

View File

@@ -24,6 +24,7 @@ import com.github.gotify.api.ClientFactory
import com.github.gotify.client.model.User import com.github.gotify.client.model.User
import com.github.gotify.client.model.VersionInfo import com.github.gotify.client.model.VersionInfo
import com.github.gotify.log.Log import com.github.gotify.log.Log
import com.github.gotify.log.LoggerHelper
import com.github.gotify.log.UncaughtExceptionHandler import com.github.gotify.log.UncaughtExceptionHandler
import com.github.gotify.login.LoginActivity import com.github.gotify.login.LoginActivity
import com.github.gotify.messages.MessagesActivity import com.github.gotify.messages.MessagesActivity
@@ -47,7 +48,7 @@ internal class InitializationActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
Log.init(this) LoggerHelper.init(this)
val theme = PreferenceManager.getDefaultSharedPreferences(this) val theme = PreferenceManager.getDefaultSharedPreferences(this)
.getString(getString(R.string.setting_key_theme), getString(R.string.theme_default))!! .getString(getString(R.string.setting_key_theme), getString(R.string.theme_default))!!
ThemeHelper.setTheme(this, theme) ThemeHelper.setTheme(this, theme)

View File

@@ -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"
}

View File

@@ -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()
}
}

View File

@@ -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<String>()) { newLines, line -> groupExceptions(newLines, line) }
.takeLast(200)
.reversed()
.joinToString(separator = "\n")
private fun groupExceptions(
newLines: MutableList<String>,
line: String
): MutableList<String> {
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")
}
}

View File

@@ -28,7 +28,7 @@ import com.github.gotify.client.api.ApplicationApi
import com.github.gotify.client.api.MessageApi import com.github.gotify.client.api.MessageApi
import com.github.gotify.client.model.Application import com.github.gotify.client.model.Application
import com.github.gotify.client.model.Message 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.log.UncaughtExceptionHandler
import com.github.gotify.messages.Extras import com.github.gotify.messages.Extras
import com.github.gotify.messages.IntentUrlDialogActivity import com.github.gotify.messages.IntentUrlDialogActivity
@@ -88,7 +88,7 @@ internal class WebSocketService : Service() {
} }
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Log.init(this) LoggerHelper.init(this)
if (connection != null) { if (connection != null) {
connection!!.close() connection!!.close()
} }

View File

@@ -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