Configure tinylog
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
43
app/src/main/kotlin/com/github/gotify/log/LoggerHelper.kt
Normal file
43
app/src/main/kotlin/com/github/gotify/log/LoggerHelper.kt
Normal 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")
|
||||
}
|
||||
}
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user