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 #2280

Merged
merged 5 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
10 changes: 8 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 @@ -28,13 +28,19 @@ 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)
}
}
10 changes: 8 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 @@ -28,13 +28,19 @@ 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)
}
}
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")
}
fun setLogLevel(level: KaliumLogLevel) {
appLoggerConfig.setLogLevel(level)
}
}
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.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 {

Expand Down Expand Up @@ -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()
}
}
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}

Expand Down
10 changes: 8 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 @@ -28,13 +28,19 @@ 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)
}
}
Loading