|
| 1 | +From 8294d786c0a8eea0a35eae453e14c8aacbe549f7 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Wes Tarle <westarle@google.com> |
| 3 | +Date: Sun, 22 Mar 2026 12:31:58 -0400 |
| 4 | +Subject: [PATCH 2/6] fix(gax): make isLoggingEnabled and setLoggingEnabled |
| 5 | + package private |
| 6 | + |
| 7 | +--- |
| 8 | + .../google/api/gax/logging/LoggingUtils.java | 29 ++++--------------- |
| 9 | + .../api/gax/logging/Slf4jLoggingHelpers.java | 5 ++-- |
| 10 | + .../api/gax/logging/LoggingUtilsTest.java | 6 ++-- |
| 11 | + 3 files changed, 10 insertions(+), 30 deletions(-) |
| 12 | + |
| 13 | +diff --git a/gax-java/gax/src/main/java/com/google/api/gax/logging/LoggingUtils.java b/gax-java/gax/src/main/java/com/google/api/gax/logging/LoggingUtils.java |
| 14 | +index c42bc4a7360..59ba3deaa00 100644 |
| 15 | +--- a/gax-java/gax/src/main/java/com/google/api/gax/logging/LoggingUtils.java |
| 16 | ++++ b/gax-java/gax/src/main/java/com/google/api/gax/logging/LoggingUtils.java |
| 17 | +@@ -37,27 +37,16 @@ import java.util.Map; |
| 18 | + public class LoggingUtils { |
| 19 | + |
| 20 | + static final String GOOGLE_SDK_JAVA_LOGGING = "GOOGLE_SDK_JAVA_LOGGING"; |
| 21 | +- static final String GOOGLE_SDK_JAVA_LOGGING_V2 = "GOOGLE_SDK_JAVA_LOGGING_V2"; |
| 22 | + |
| 23 | + private static boolean loggingEnabled = checkLoggingEnabled(GOOGLE_SDK_JAVA_LOGGING); |
| 24 | +- private static boolean loggingV2Enabled = checkLoggingEnabled(GOOGLE_SDK_JAVA_LOGGING_V2); |
| 25 | + |
| 26 | + /** |
| 27 | +- * Returns whether client-side logging is enabled (V1 or V2). |
| 28 | ++ * Returns whether client-side logging is enabled. |
| 29 | + * |
| 30 | + * @return true if logging is enabled, false otherwise. |
| 31 | + */ |
| 32 | +- public static boolean isLoggingEnabled() { |
| 33 | +- return loggingEnabled || loggingV2Enabled; |
| 34 | +- } |
| 35 | +- |
| 36 | +- /** |
| 37 | +- * Returns whether client-side logging V2 (Actionable Errors) is enabled. |
| 38 | +- * |
| 39 | +- * @return true if V2 logging is enabled, false otherwise. |
| 40 | +- */ |
| 41 | +- public static boolean isLoggingV2Enabled() { |
| 42 | +- return loggingV2Enabled; |
| 43 | ++ static boolean isLoggingEnabled() { |
| 44 | ++ return loggingEnabled; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | +@@ -65,19 +54,11 @@ public class LoggingUtils { |
| 49 | + * |
| 50 | + * @param enabled true to enable logging, false to disable. |
| 51 | + */ |
| 52 | +- public static void setLoggingEnabled(boolean enabled) { |
| 53 | ++ @com.google.common.annotations.VisibleForTesting |
| 54 | ++ static void setLoggingEnabled(boolean enabled) { |
| 55 | + loggingEnabled = enabled; |
| 56 | + } |
| 57 | + |
| 58 | +- /** |
| 59 | +- * Sets whether client-side logging V2 is enabled. Visible for testing. |
| 60 | +- * |
| 61 | +- * @param enabled true to enable logging, false to disable. |
| 62 | +- */ |
| 63 | +- public static void setLoggingV2Enabled(boolean enabled) { |
| 64 | +- loggingV2Enabled = enabled; |
| 65 | +- } |
| 66 | +- |
| 67 | + private static boolean checkLoggingEnabled(String envVar) { |
| 68 | + String enableLogging = System.getenv(envVar); |
| 69 | + return "true".equalsIgnoreCase(enableLogging); |
| 70 | +diff --git a/gax-java/gax/src/main/java/com/google/api/gax/logging/Slf4jLoggingHelpers.java b/gax-java/gax/src/main/java/com/google/api/gax/logging/Slf4jLoggingHelpers.java |
| 71 | +index 85fda43c66f..2a914f4bf62 100644 |
| 72 | +--- a/gax-java/gax/src/main/java/com/google/api/gax/logging/Slf4jLoggingHelpers.java |
| 73 | ++++ b/gax-java/gax/src/main/java/com/google/api/gax/logging/Slf4jLoggingHelpers.java |
| 74 | +@@ -114,11 +114,10 @@ class Slf4jLoggingHelpers { |
| 75 | + LoggingUtils.executeWithTryCatch( |
| 76 | + () -> { |
| 77 | + Logger logger = loggerProvider.getLogger(); |
| 78 | +- boolean isV2 = LoggingUtils.isLoggingV2Enabled(); |
| 79 | +- if (!isV2 && logger.isInfoEnabled()) { |
| 80 | ++ if (logger.isInfoEnabled()) { |
| 81 | + logDataBuilder.responseStatus(status); |
| 82 | + } |
| 83 | +- if (!isV2 && logger.isInfoEnabled() && !logger.isDebugEnabled()) { |
| 84 | ++ if (logger.isInfoEnabled() && !logger.isDebugEnabled()) { |
| 85 | + Map<String, Object> responseData = logDataBuilder.build().toMapResponse(); |
| 86 | + Slf4jUtils.log(logger, Level.INFO, responseData, "Received response"); |
| 87 | + } |
| 88 | +diff --git a/gax-java/gax/src/test/java/com/google/api/gax/logging/LoggingUtilsTest.java b/gax-java/gax/src/test/java/com/google/api/gax/logging/LoggingUtilsTest.java |
| 89 | +index cffe2b6d2c1..46ecc2c461f 100644 |
| 90 | +--- a/gax-java/gax/src/test/java/com/google/api/gax/logging/LoggingUtilsTest.java |
| 91 | ++++ b/gax-java/gax/src/test/java/com/google/api/gax/logging/LoggingUtilsTest.java |
| 92 | +@@ -105,17 +105,17 @@ class LoggingUtilsTest { |
| 93 | + |
| 94 | + @Test |
| 95 | + void testLogActionableError_success() { |
| 96 | +- LoggingUtils.setLoggingV2Enabled(true); |
| 97 | ++ LoggingUtils.setLoggingEnabled(true); |
| 98 | + LoggerProvider loggerProvider = mock(LoggerProvider.class); |
| 99 | + Logger logger = mock(Logger.class); |
| 100 | + when(loggerProvider.getLogger()).thenReturn(logger); |
| 101 | + |
| 102 | + org.slf4j.spi.LoggingEventBuilder eventBuilder = mock(org.slf4j.spi.LoggingEventBuilder.class); |
| 103 | +- when(logger.atInfo()).thenReturn(eventBuilder); |
| 104 | ++ when(logger.atDebug()).thenReturn(eventBuilder); |
| 105 | + when(eventBuilder.addKeyValue(anyString(), any())).thenReturn(eventBuilder); |
| 106 | + |
| 107 | + Map<String, Object> context = Collections.singletonMap("key", "value"); |
| 108 | +- LoggingUtils.logActionableError(context, loggerProvider, org.slf4j.event.Level.INFO, "message"); |
| 109 | ++ LoggingUtils.logActionableError(context, loggerProvider, org.slf4j.event.Level.DEBUG, "message"); |
| 110 | + |
| 111 | + verify(loggerProvider).getLogger(); |
| 112 | + } |
| 113 | +-- |
| 114 | +2.53.0.1018.g2bb0e51243-goog |
| 115 | + |
0 commit comments