Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add logger with userid and clientid #2334

Merged
merged 8 commits into from
Oct 17, 2023
11 changes: 9 additions & 2 deletions app/src/beta/kotlin/com/wire/android/util/DataDogLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Any?>()
logger.log(severity.ordinal, message, throwable, attributes)
}
}
11 changes: 9 additions & 2 deletions app/src/dev/kotlin/com/wire/android/util/DataDogLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Any?>()
logger.log(severity.ordinal, message, throwable, attributes)
}
}
11 changes: 9 additions & 2 deletions app/src/internal/kotlin/com/wire/android/util/DataDogLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Any?>()
logger.log(severity.ordinal, message, throwable, attributes)
}
}
34 changes: 34 additions & 0 deletions app/src/main/kotlin/com/wire/android/AppLogger.kt
Original file line number Diff line number Diff line change
@@ -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")
}

Check warning on line 30 in app/src/main/kotlin/com/wire/android/AppLogger.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/AppLogger.kt#L28-L30

Added lines #L28 - L30 were not covered by tests
fun setLogLevel(level: KaliumLogLevel) {
appLoggerConfig.setLogLevel(level)
}

Check warning on line 33 in app/src/main/kotlin/com/wire/android/AppLogger.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/AppLogger.kt#L32-L33

Added lines #L32 - L33 were not covered by tests
}
39 changes: 14 additions & 25 deletions app/src/main/kotlin/com/wire/android/WireApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@
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 {

Expand Down Expand Up @@ -131,17 +128,22 @@
// 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()

Check warning on line 131 in app/src/main/kotlin/com/wire/android/WireApplication.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/WireApplication.kt#L131

Added line #L131 was not covered by tests
val config = if (isLoggingEnabled) {
KaliumLogger.Config.DEFAULT.apply {
setLogLevel(KaliumLogLevel.VERBOSE)
setLogWriterList(listOf(DataDogLogger, platformLogWriter()))
}

Check warning on line 136 in app/src/main/kotlin/com/wire/android/WireApplication.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/WireApplication.kt#L133-L136

Added lines #L133 - L136 were not covered by tests
} else {
KaliumLogger.Config.disabled()

Check warning on line 138 in app/src/main/kotlin/com/wire/android/WireApplication.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/WireApplication.kt#L138

Added line #L138 was not covered by tests
}
// 2. Initialize our internal logging framework
AppLogger.init(config)
CoreLogger.init(config)

Check warning on line 142 in app/src/main/kotlin/com/wire/android/WireApplication.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/WireApplication.kt#L141-L142

Added lines #L141 - L142 were not covered by tests
// 3. Initialize our internal FILE logging framework
enableLoggingAndInitiateFileLogging()
logFileWriter.start()

Check warning on line 144 in app/src/main/kotlin/com/wire/android/WireApplication.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/WireApplication.kt#L144

Added line #L144 was not covered by tests
// 4. Everything ready, now we can log device info
appLogger.i("Logger enabled")

Check warning on line 146 in app/src/main/kotlin/com/wire/android/WireApplication.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/WireApplication.kt#L146

Added line #L146 was not covered by tests
logDeviceInformation()
}
}
Expand All @@ -158,19 +160,6 @@
)
}

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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@
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
Expand Down Expand Up @@ -77,10 +75,10 @@
}
if (isEnabled) {
logFileWriter.start()
CoreLogger.setLoggingLevel(level = KaliumLogLevel.VERBOSE, logWriters = arrayOf(DataDogLogger, platformLogWriter()))
CoreLogger.setLoggingLevel(level = KaliumLogLevel.VERBOSE)

Check warning on line 78 in app/src/main/kotlin/com/wire/android/ui/debug/UserDebugViewModel.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/ui/debug/UserDebugViewModel.kt#L78

Added line #L78 was not covered by tests
} else {
logFileWriter.stop()
CoreLogger.setLoggingLevel(level = KaliumLogLevel.DISABLED, logWriters = arrayOf(DataDogLogger, platformLogWriter()))
CoreLogger.setLoggingLevel(level = KaliumLogLevel.DISABLED)

Check warning on line 81 in app/src/main/kotlin/com/wire/android/ui/debug/UserDebugViewModel.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/ui/debug/UserDebugViewModel.kt#L81

Added line #L81 was not covered by tests
}
}

Expand Down
11 changes: 9 additions & 2 deletions app/src/staging/kotlin/com/wire/android/util/DataDogLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Any?>()
logger.log(severity.ordinal, message, throwable, attributes)
}
}