Skip to content

Commit dc3dd91

Browse files
committed
add log messages when the log level changes
1 parent a5bc6e4 commit dc3dd91

3 files changed

Lines changed: 99 additions & 2 deletions

File tree

‎firebase-dataconnect/src/main/kotlin/com/google/firebase/dataconnect/LogLevel.kt‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,26 @@ public enum class LogLevel {
3030
WARN,
3131

3232
/** Do not log anything. */
33-
NONE,
33+
NONE;
34+
35+
internal companion object {
36+
37+
/**
38+
* Returns one of the two given log levels, the one that is "noisier" (i.e. that logs more).
39+
*
40+
* It can be useful to figure out which of two log levels are noisier on log level change, to
41+
* emit a message about the log level change at the noisiest level.
42+
*/
43+
fun noisiestOf(logLevel1: LogLevel, logLevel2: LogLevel): LogLevel =
44+
when (logLevel1) {
45+
DEBUG -> DEBUG
46+
NONE -> logLevel2
47+
WARN ->
48+
when (logLevel2) {
49+
DEBUG -> DEBUG
50+
WARN -> WARN
51+
NONE -> WARN
52+
}
53+
}
54+
}
3455
}

‎firebase-dataconnect/src/main/kotlin/com/google/firebase/dataconnect/core/Logger.kt‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,14 @@ import android.util.Log
2020
import com.google.firebase.dataconnect.BuildConfig
2121
import com.google.firebase.dataconnect.LogLevel
2222
import com.google.firebase.dataconnect.core.LoggerGlobals.LOG_TAG
23+
import com.google.firebase.dataconnect.core.LoggerGlobals.Logger
2324
import com.google.firebase.util.nextAlphanumericString
2425
import kotlin.random.Random
26+
import kotlinx.coroutines.CoroutineScope
27+
import kotlinx.coroutines.DelicateCoroutinesApi
28+
import kotlinx.coroutines.GlobalScope
2529
import kotlinx.coroutines.flow.MutableStateFlow
30+
import kotlinx.coroutines.launch
2631

2732
internal interface Logger {
2833
val name: String
@@ -59,7 +64,11 @@ private class LoggerImpl(override val name: String) : Logger {
5964
internal object LoggerGlobals {
6065
const val LOG_TAG = "FirebaseDataConnect"
6166

62-
val logLevel = MutableStateFlow(LogLevel.WARN)
67+
val logLevel =
68+
MutableStateFlow(LogLevel.WARN).also { logLevelFlow ->
69+
val logger = Logger("LogLevelChange")
70+
@OptIn(DelicateCoroutinesApi::class) logger.logChanges(logLevelFlow, GlobalScope)
71+
}
6372

6473
inline fun Logger.debug(message: () -> Any?) {
6574
if (logLevel.value <= LogLevel.DEBUG) debug("${message()}")
@@ -86,4 +95,21 @@ internal object LoggerGlobals {
8695
}
8796

8897
fun Logger(name: String): Logger = LoggerImpl(name)
98+
99+
// Log a message each time the log level changes. This is intended to provide context when debug
100+
// logging is enabled and no logs are produced, to at least confirm that debug logging has been
101+
// enabled. Also, it will leave a "mark" in the logs when debug logging is _disabled_ to explain
102+
// why the debug logs stop.
103+
private fun Logger.logChanges(flow: MutableStateFlow<LogLevel>, coroutineScope: CoroutineScope) {
104+
var previousLogLevel = flow.value
105+
coroutineScope.launch {
106+
flow.collect { newLogLevel: LogLevel ->
107+
if (newLogLevel != previousLogLevel) {
108+
val emitLogLevel = LogLevel.noisiestOf(newLogLevel, previousLogLevel)
109+
log(null, emitLogLevel, "Log level changed to $newLogLevel (was $previousLogLevel)")
110+
previousLogLevel = newLogLevel
111+
}
112+
}
113+
}
114+
}
89115
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.firebase.dataconnect
18+
19+
import io.kotest.assertions.assertSoftly
20+
import io.kotest.assertions.withClue
21+
import io.kotest.matchers.shouldBe
22+
import kotlinx.coroutines.test.runTest
23+
import org.junit.Test
24+
25+
class LoggerUnitTest {
26+
27+
@Test
28+
fun `noisiestOf()`() = runTest {
29+
assertSoftly {
30+
verifyNoisiestOf(LogLevel.NONE, LogLevel.NONE, LogLevel.NONE)
31+
verifyNoisiestOf(LogLevel.NONE, LogLevel.WARN, LogLevel.WARN)
32+
verifyNoisiestOf(LogLevel.NONE, LogLevel.DEBUG, LogLevel.DEBUG)
33+
verifyNoisiestOf(LogLevel.WARN, LogLevel.NONE, LogLevel.WARN)
34+
verifyNoisiestOf(LogLevel.WARN, LogLevel.WARN, LogLevel.WARN)
35+
verifyNoisiestOf(LogLevel.WARN, LogLevel.DEBUG, LogLevel.DEBUG)
36+
verifyNoisiestOf(LogLevel.DEBUG, LogLevel.NONE, LogLevel.DEBUG)
37+
verifyNoisiestOf(LogLevel.DEBUG, LogLevel.WARN, LogLevel.DEBUG)
38+
verifyNoisiestOf(LogLevel.DEBUG, LogLevel.DEBUG, LogLevel.DEBUG)
39+
}
40+
}
41+
42+
private companion object {
43+
44+
fun verifyNoisiestOf(logLevel1: LogLevel, logLevel2: LogLevel, expected: LogLevel) {
45+
withClue("noisiestOf($logLevel1, $logLevel2)") {
46+
LogLevel.noisiestOf(logLevel1, logLevel2) shouldBe expected
47+
}
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)