From 237abad740bad119ba133143d357794e95ed1d0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Saleniuk?= Date: Mon, 16 Oct 2023 17:40:10 +0200 Subject: [PATCH 1/4] feat: add logger with userid and clientid --- .../com/wire/android/util/DataDogLogger.kt | 11 +++++- .../com/wire/android/util/DataDogLogger.kt | 11 +++++- .../com/wire/android/util/DataDogLogger.kt | 11 +++++- .../main/kotlin/com/wire/android/AppLogger.kt | 34 ++++++++++++++++ .../com/wire/android/WireApplication.kt | 39 +++++++------------ .../android/ui/debug/UserDebugViewModel.kt | 6 +-- .../com/wire/android/util/DataDogLogger.kt | 11 +++++- kalium | 2 +- 8 files changed, 87 insertions(+), 38 deletions(-) create mode 100644 app/src/main/kotlin/com/wire/android/AppLogger.kt diff --git a/app/src/beta/kotlin/com/wire/android/util/DataDogLogger.kt b/app/src/beta/kotlin/com/wire/android/util/DataDogLogger.kt index c29889edde5..08911615b31 100644 --- a/app/src/beta/kotlin/com/wire/android/util/DataDogLogger.kt +++ b/app/src/beta/kotlin/com/wire/android/util/DataDogLogger.kt @@ -23,18 +23,25 @@ package com.wire.android.util import co.touchlab.kermit.LogWriter import co.touchlab.kermit.Severity import com.datadog.android.log.Logger +import com.wire.kalium.logger.KaliumLogger object DataDogLogger : LogWriter() { private val logger = Logger.Builder() .setNetworkInfoEnabled(true) - .setLogcatLogsEnabled(true) + .setLogcatLogsEnabled(false) // we already use platformLogWriter() along with DataDogLogger, don't need duplicates in LogCat .setDatadogLogsEnabled(true) .setBundleWithTraceEnabled(true) .setLoggerName("DATADOG") .build() override fun log(severity: Severity, message: String, tag: String, throwable: Throwable?) { - logger.log(severity.ordinal, message, throwable) + val attributes = KaliumLogger.UserClientData.getFromTag(tag)?.let { userClientData -> + mapOf( + "userId" to userClientData.userId, + "clientId" to userClientData.clientId, + ) + } ?: emptyMap() + logger.log(severity.ordinal, message, throwable, attributes) } } diff --git a/app/src/dev/kotlin/com/wire/android/util/DataDogLogger.kt b/app/src/dev/kotlin/com/wire/android/util/DataDogLogger.kt index c29889edde5..4fdf9c5e0b2 100644 --- a/app/src/dev/kotlin/com/wire/android/util/DataDogLogger.kt +++ b/app/src/dev/kotlin/com/wire/android/util/DataDogLogger.kt @@ -23,18 +23,25 @@ package com.wire.android.util import co.touchlab.kermit.LogWriter import co.touchlab.kermit.Severity import com.datadog.android.log.Logger +import com.wire.kalium.logger.KaliumLogger object DataDogLogger : LogWriter() { private val logger = Logger.Builder() .setNetworkInfoEnabled(true) .setLogcatLogsEnabled(true) - .setDatadogLogsEnabled(true) + .setLogcatLogsEnabled(false) // we already use platformLogWriter() along with DataDogLogger, don't need duplicates in LogCat .setBundleWithTraceEnabled(true) .setLoggerName("DATADOG") .build() override fun log(severity: Severity, message: String, tag: String, throwable: Throwable?) { - logger.log(severity.ordinal, message, throwable) + val attributes = KaliumLogger.UserClientData.getFromTag(tag)?.let { userClientData -> + mapOf( + "userId" to userClientData.userId, + "clientId" to userClientData.clientId, + ) + } ?: emptyMap() + logger.log(severity.ordinal, message, throwable, attributes) } } diff --git a/app/src/internal/kotlin/com/wire/android/util/DataDogLogger.kt b/app/src/internal/kotlin/com/wire/android/util/DataDogLogger.kt index c29889edde5..4fdf9c5e0b2 100644 --- a/app/src/internal/kotlin/com/wire/android/util/DataDogLogger.kt +++ b/app/src/internal/kotlin/com/wire/android/util/DataDogLogger.kt @@ -23,18 +23,25 @@ package com.wire.android.util import co.touchlab.kermit.LogWriter import co.touchlab.kermit.Severity import com.datadog.android.log.Logger +import com.wire.kalium.logger.KaliumLogger object DataDogLogger : LogWriter() { private val logger = Logger.Builder() .setNetworkInfoEnabled(true) .setLogcatLogsEnabled(true) - .setDatadogLogsEnabled(true) + .setLogcatLogsEnabled(false) // we already use platformLogWriter() along with DataDogLogger, don't need duplicates in LogCat .setBundleWithTraceEnabled(true) .setLoggerName("DATADOG") .build() override fun log(severity: Severity, message: String, tag: String, throwable: Throwable?) { - logger.log(severity.ordinal, message, throwable) + val attributes = KaliumLogger.UserClientData.getFromTag(tag)?.let { userClientData -> + mapOf( + "userId" to userClientData.userId, + "clientId" to userClientData.clientId, + ) + } ?: emptyMap() + logger.log(severity.ordinal, message, throwable, attributes) } } diff --git a/app/src/main/kotlin/com/wire/android/AppLogger.kt b/app/src/main/kotlin/com/wire/android/AppLogger.kt new file mode 100644 index 00000000000..17d7cf6a030 --- /dev/null +++ b/app/src/main/kotlin/com/wire/android/AppLogger.kt @@ -0,0 +1,34 @@ +/* + * Wire + * Copyright (C) 2023 Wire Swiss GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://www.gnu.org/licenses/. + */ +package com.wire.android + +import com.wire.kalium.logger.KaliumLogLevel +import com.wire.kalium.logger.KaliumLogger + +private var appLoggerConfig = KaliumLogger.Config.disabled() +// App wide global logger, carefully initialized when our application is "onCreate" +internal var appLogger = KaliumLogger.disabled() +object AppLogger { + fun init(config: KaliumLogger.Config) { + appLoggerConfig = config + appLogger = KaliumLogger(config = config, tag = "WireAppLogger") + } + fun setLogLevel(level: KaliumLogLevel) { + appLoggerConfig.setLogLevel(level) + } +} diff --git a/app/src/main/kotlin/com/wire/android/WireApplication.kt b/app/src/main/kotlin/com/wire/android/WireApplication.kt index ec5ac394957..c9aff8ea4f9 100644 --- a/app/src/main/kotlin/com/wire/android/WireApplication.kt +++ b/app/src/main/kotlin/com/wire/android/WireApplication.kt @@ -47,9 +47,6 @@ import kotlinx.coroutines.flow.first import kotlinx.coroutines.launch import javax.inject.Inject -// App wide global logger, carefully initialized when our application is "onCreate" -var appLogger: KaliumLogger = KaliumLogger.disabled() - @HiltAndroidApp class WireApplication : Application(), Configuration.Provider { @@ -131,17 +128,22 @@ class WireApplication : Application(), Configuration.Provider { // 1. Datadog should be initialized first ExternalLoggerManager.initDatadogLogger(applicationContext, globalDataStore) // 2. Initialize our internal logging framework - appLogger = KaliumLogger( - config = KaliumLogger.Config( - severity = if (BuildConfig.PRIVATE_BUILD) KaliumLogLevel.DEBUG else KaliumLogLevel.DISABLED, - tag = "WireAppLogger" - ), - DataDogLogger, - platformLogWriter() - ) + val isLoggingEnabled = globalDataStore.isLoggingEnabled().first() + val config = if (isLoggingEnabled) { + KaliumLogger.Config.DEFAULT.apply { + setLogLevel(KaliumLogLevel.VERBOSE) + setLogWriterList(listOf(DataDogLogger, platformLogWriter())) + } + } else { + KaliumLogger.Config.disabled() + } + // 2. Initialize our internal logging framework + AppLogger.init(config) + CoreLogger.init(config) // 3. Initialize our internal FILE logging framework - enableLoggingAndInitiateFileLogging() + logFileWriter.start() // 4. Everything ready, now we can log device info + appLogger.i("Logger enabled") logDeviceInformation() } } @@ -158,19 +160,6 @@ class WireApplication : Application(), Configuration.Provider { ) } - private fun enableLoggingAndInitiateFileLogging() { - globalAppScope.launch { - if (globalDataStore.isLoggingEnabled().first()) { - CoreLogger.setLoggingLevel( - level = KaliumLogLevel.VERBOSE, - logWriters = arrayOf(DataDogLogger, platformLogWriter()) - ) - logFileWriter.start() - appLogger.i("Logger enabled") - } - } - } - override fun onTrimMemory(level: Int) { super.onTrimMemory(level) appLogger.w( diff --git a/app/src/main/kotlin/com/wire/android/ui/debug/UserDebugViewModel.kt b/app/src/main/kotlin/com/wire/android/ui/debug/UserDebugViewModel.kt index 8a7f8c85646..a9997493b68 100644 --- a/app/src/main/kotlin/com/wire/android/ui/debug/UserDebugViewModel.kt +++ b/app/src/main/kotlin/com/wire/android/ui/debug/UserDebugViewModel.kt @@ -25,10 +25,8 @@ import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope -import co.touchlab.kermit.platformLogWriter import com.wire.android.datastore.GlobalDataStore import com.wire.android.di.CurrentAccount -import com.wire.android.util.DataDogLogger import com.wire.android.util.EMPTY import com.wire.android.util.LogFileWriter import com.wire.kalium.logger.KaliumLogLevel @@ -77,10 +75,10 @@ class UserDebugViewModel } if (isEnabled) { logFileWriter.start() - CoreLogger.setLoggingLevel(level = KaliumLogLevel.VERBOSE, logWriters = arrayOf(DataDogLogger, platformLogWriter())) + CoreLogger.setLoggingLevel(level = KaliumLogLevel.VERBOSE) } else { logFileWriter.stop() - CoreLogger.setLoggingLevel(level = KaliumLogLevel.DISABLED, logWriters = arrayOf(DataDogLogger, platformLogWriter())) + CoreLogger.setLoggingLevel(level = KaliumLogLevel.DISABLED) } } diff --git a/app/src/staging/kotlin/com/wire/android/util/DataDogLogger.kt b/app/src/staging/kotlin/com/wire/android/util/DataDogLogger.kt index c29889edde5..4fdf9c5e0b2 100644 --- a/app/src/staging/kotlin/com/wire/android/util/DataDogLogger.kt +++ b/app/src/staging/kotlin/com/wire/android/util/DataDogLogger.kt @@ -23,18 +23,25 @@ package com.wire.android.util import co.touchlab.kermit.LogWriter import co.touchlab.kermit.Severity import com.datadog.android.log.Logger +import com.wire.kalium.logger.KaliumLogger object DataDogLogger : LogWriter() { private val logger = Logger.Builder() .setNetworkInfoEnabled(true) .setLogcatLogsEnabled(true) - .setDatadogLogsEnabled(true) + .setLogcatLogsEnabled(false) // we already use platformLogWriter() along with DataDogLogger, don't need duplicates in LogCat .setBundleWithTraceEnabled(true) .setLoggerName("DATADOG") .build() override fun log(severity: Severity, message: String, tag: String, throwable: Throwable?) { - logger.log(severity.ordinal, message, throwable) + val attributes = KaliumLogger.UserClientData.getFromTag(tag)?.let { userClientData -> + mapOf( + "userId" to userClientData.userId, + "clientId" to userClientData.clientId, + ) + } ?: emptyMap() + logger.log(severity.ordinal, message, throwable, attributes) } } diff --git a/kalium b/kalium index 4d801fc83ff..618ec62a9f2 160000 --- a/kalium +++ b/kalium @@ -1 +1 @@ -Subproject commit 4d801fc83ff23b824c987d4347e2d8083bd850c6 +Subproject commit 618ec62a9f24143f9c659baca1653e2f5cefc8b2 From 68005dd7b5026fd92a206ae301eacd7e7f1323a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Saleniuk?= Date: Mon, 16 Oct 2023 17:49:34 +0200 Subject: [PATCH 2/4] update kalium ref --- kalium | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kalium b/kalium index 618ec62a9f2..d7e6282dd64 160000 --- a/kalium +++ b/kalium @@ -1 +1 @@ -Subproject commit 618ec62a9f24143f9c659baca1653e2f5cefc8b2 +Subproject commit d7e6282dd64260c7d1404deef438d511355af0d5 From ac4167b68ce8b6f8ad821051a7312ee8bec1be40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Saleniuk?= Date: Mon, 16 Oct 2023 17:51:48 +0200 Subject: [PATCH 3/4] update kalium ref --- kalium | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kalium b/kalium index d7e6282dd64..5daa0f391ad 160000 --- a/kalium +++ b/kalium @@ -1 +1 @@ -Subproject commit d7e6282dd64260c7d1404deef438d511355af0d5 +Subproject commit 5daa0f391adfff635d146cd9baf35d94664b8e1b From 2d7a8ddd8eb3a4295866f02fda2352c18bdc9b70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Saleniuk?= Date: Tue, 17 Oct 2023 16:21:33 +0200 Subject: [PATCH 4/4] update kalium ref --- kalium | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kalium b/kalium index 5daa0f391ad..f113b0620af 160000 --- a/kalium +++ b/kalium @@ -1 +1 @@ -Subproject commit 5daa0f391adfff635d146cd9baf35d94664b8e1b +Subproject commit f113b0620af6ccedd825d877d9d345464ad8e01c