Skip to content

Commit

Permalink
Merge pull request #317 from gotify/log
Browse files Browse the repository at this point in the history
Migrate to tinylog
  • Loading branch information
jmattheis authored Oct 8, 2023
2 parents 361c480 + 5062031 commit 5e67cc8
Show file tree
Hide file tree
Showing 23 changed files with 166 additions and 148 deletions.
4 changes: 3 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />

<application
android:name=".GotifyApplication"
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
Expand Down
31 changes: 31 additions & 0 deletions app/src/main/kotlin/com/github/gotify/GotifyApplication.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.github.gotify

import android.app.Application
import android.app.NotificationManager
import android.os.Build
import androidx.preference.PreferenceManager
import com.github.gotify.log.LoggerHelper
import com.github.gotify.log.UncaughtExceptionHandler
import com.github.gotify.settings.ThemeHelper
import org.tinylog.kotlin.Logger

class GotifyApplication : Application() {
override fun onCreate() {
LoggerHelper.init(this)
UncaughtExceptionHandler.registerCurrentThread()
Logger.info("${javaClass.simpleName}: onCreate")

val theme = PreferenceManager.getDefaultSharedPreferences(this)
.getString(getString(R.string.setting_key_theme), getString(R.string.theme_default))!!
ThemeHelper.setTheme(this, theme)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationSupport.createForegroundChannel(
this,
this.getSystemService(NotificationManager::class.java)
)
}

super.onCreate()
}
}
4 changes: 2 additions & 2 deletions app/src/main/kotlin/com/github/gotify/MissedMessageUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.github.gotify.api.ApiException
import com.github.gotify.api.Callback
import com.github.gotify.client.api.MessageApi
import com.github.gotify.client.model.Message
import com.github.gotify.log.Log
import org.tinylog.kotlin.Logger

internal class MissedMessageUtil(private val api: MessageApi) {
fun lastReceivedMessage(acceptID: (Long) -> Unit) {
Expand Down Expand Up @@ -41,7 +41,7 @@ internal class MissedMessageUtil(private val api: MessageApi) {
since = pagedMessages.paging.since
}
} catch (e: ApiException) {
Log.e("cannot retrieve missing messages", e)
Logger.error(e, "cannot retrieve missing messages")
}
return result.reversed()
}
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/kotlin/com/github/gotify/NotificationSupport.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import android.os.Build
import androidx.annotation.RequiresApi
import androidx.preference.PreferenceManager
import com.github.gotify.client.model.Application
import com.github.gotify.log.Log
import org.tinylog.kotlin.Logger

internal object NotificationSupport {
@RequiresApi(Build.VERSION_CODES.O)
Expand Down Expand Up @@ -93,7 +93,7 @@ internal object NotificationSupport {
notificationManager.createNotificationChannel(messagesImportanceDefault)
notificationManager.createNotificationChannel(messagesImportanceHigh)
} catch (e: Exception) {
Log.e("Could not create channel", e)
Logger.error(e, "Could not create channel")
}
}

Expand Down Expand Up @@ -168,7 +168,7 @@ internal object NotificationSupport {
notificationManager.createNotificationChannel(messagesImportanceDefault)
notificationManager.createNotificationChannel(messagesImportanceHigh)
} catch (e: Exception) {
Log.e("Could not create channel", e)
Logger.error(e, "Could not create channel")
}
}

Expand Down
8 changes: 4 additions & 4 deletions app/src/main/kotlin/com/github/gotify/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.github.gotify.client.JSON
import com.github.gotify.log.Log
import com.google.android.material.snackbar.Snackbar
import com.google.gson.Gson
import com.squareup.picasso.Picasso.LoadedFrom
Expand All @@ -31,6 +30,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import okio.Buffer
import org.threeten.bp.OffsetDateTime
import org.tinylog.kotlin.Logger

internal object Utils {
val JSON: Gson = JSON().gson
Expand Down Expand Up @@ -63,10 +63,10 @@ internal object Utils {
URL(URL(baseURL), target).toString()
}
} catch (e: MalformedURLException) {
Log.e("Could not resolve absolute url", e)
Logger.error(e, "Could not resolve absolute url")
target
} catch (e: URISyntaxException) {
Log.e("Could not resolve absolute url", e)
Logger.error(e, "Could not resolve absolute url")
target
}
}
Expand All @@ -79,7 +79,7 @@ internal object Utils {
}

override fun onBitmapFailed(e: Exception, errorDrawable: Drawable) {
Log.e("Bitmap failed", e)
Logger.error(e, "Bitmap failed")
}

override fun onPrepareLoad(placeHolderDrawable: Drawable) {}
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/kotlin/com/github/gotify/api/Callback.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.github.gotify.api

import android.app.Activity
import com.github.gotify.log.Log
import com.github.gotify.api.Callback.ErrorCallback
import com.github.gotify.api.Callback.SuccessCallback
import org.tinylog.kotlin.Logger
import retrofit2.Call
import retrofit2.Response

Expand Down Expand Up @@ -65,7 +67,7 @@ internal class Callback<T> private constructor(
private fun <T> errorCallback(): Callback<T> {
return Callback(
onSuccess = {},
onError = { exception -> Log.e("Error while api call", exception) }
onError = { exception -> Logger.error(exception, "Error while api call") }
)
}

Expand Down
10 changes: 6 additions & 4 deletions app/src/main/kotlin/com/github/gotify/api/CertUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.github.gotify.api
import android.annotation.SuppressLint
import com.github.gotify.SSLSettings
import com.github.gotify.Utils
import com.github.gotify.log.Log
import java.io.IOException
import java.security.GeneralSecurityException
import java.security.KeyStore
Expand All @@ -16,15 +15,18 @@ import javax.net.ssl.TrustManager
import javax.net.ssl.TrustManagerFactory
import javax.net.ssl.X509TrustManager
import okhttp3.OkHttpClient
import org.tinylog.kotlin.Logger

internal object CertUtils {
@SuppressLint("CustomX509TrustManager")
private val trustAll = object : X509TrustManager {
@SuppressLint("TrustAllX509TrustManager")
override fun checkClientTrusted(chain: Array<X509Certificate>, authType: String) {}
override fun checkClientTrusted(chain: Array<X509Certificate>, authType: String) {
}

@SuppressLint("TrustAllX509TrustManager")
override fun checkServerTrusted(chain: Array<X509Certificate>, authType: String) {}
override fun checkServerTrusted(chain: Array<X509Certificate>, authType: String) {
}

override fun getAcceptedIssuers() = arrayOf<X509Certificate>()
}
Expand Down Expand Up @@ -62,7 +64,7 @@ internal object CertUtils {
}
} catch (e: Exception) {
// We shouldn't have issues since the cert is verified on login.
Log.e("Failed to apply SSL settings", e)
Logger.error(e, "Failed to apply SSL settings")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package com.github.gotify.init

import android.Manifest
import android.app.AlarmManager
import android.app.NotificationManager
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
Expand All @@ -13,8 +11,6 @@ import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.preference.PreferenceManager
import com.github.gotify.NotificationSupport
import com.github.gotify.R
import com.github.gotify.Settings
import com.github.gotify.api.ApiException
Expand All @@ -23,16 +19,14 @@ import com.github.gotify.api.Callback.SuccessCallback
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.UncaughtExceptionHandler
import com.github.gotify.login.LoginActivity
import com.github.gotify.messages.MessagesActivity
import com.github.gotify.service.WebSocketService
import com.github.gotify.settings.ThemeHelper
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.livinglifetechway.quickpermissionskotlin.runWithPermissions
import com.livinglifetechway.quickpermissionskotlin.util.QuickPermissionsOptions
import com.livinglifetechway.quickpermissionskotlin.util.QuickPermissionsRequest
import org.tinylog.kotlin.Logger

internal class InitializationActivity : AppCompatActivity() {

Expand All @@ -47,20 +41,8 @@ internal class InitializationActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Log.init(this)
val theme = PreferenceManager.getDefaultSharedPreferences(this)
.getString(getString(R.string.setting_key_theme), getString(R.string.theme_default))!!
ThemeHelper.setTheme(this, theme)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationSupport.createForegroundChannel(
this,
(this.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager)
)
}
UncaughtExceptionHandler.registerCurrentThread()
settings = Settings(this)
Log.i("Entering ${javaClass.simpleName}")
Logger.info("Entering ${javaClass.simpleName}")

installSplashScreen().setKeepOnScreenCondition { splashScreenActive }

Expand Down Expand Up @@ -115,6 +97,7 @@ internal class InitializationActivity : AppCompatActivity() {
dialog(getString(R.string.not_available, settings.url))
return
}

401 -> {
dialog(getString(R.string.auth_failed))
return
Expand Down Expand Up @@ -153,7 +136,7 @@ internal class InitializationActivity : AppCompatActivity() {
}

private fun authenticated(user: User) {
Log.i("Authenticated as ${user.name}")
Logger.info("Authenticated as ${user.name}")

settings.setUser(user.name, user.isAdmin)
requestVersion {
Expand All @@ -172,7 +155,7 @@ internal class InitializationActivity : AppCompatActivity() {
private fun requestVersion(runnable: Runnable) {
requestVersion(
callback = Callback.SuccessBody { version: VersionInfo ->
Log.i("Server version: ${version.version}@${version.buildDate}")
Logger.info("Server version: ${version.version}@${version.buildDate}")
settings.serverVersion = version.version
runnable.run()
},
Expand Down
16 changes: 0 additions & 16 deletions app/src/main/kotlin/com/github/gotify/log/Format.kt

This file was deleted.

47 changes: 0 additions & 47 deletions app/src/main/kotlin/com/github/gotify/log/Log.kt

This file was deleted.

43 changes: 43 additions & 0 deletions app/src/main/kotlin/com/github/gotify/log/LoggerHelper.kt
Original file line number Diff line number Diff line change
@@ -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")
}
}
Loading

0 comments on commit 5e67cc8

Please sign in to comment.