Skip to content

Commit d98fd2a

Browse files
committed
fix(java-showcase): Fix ITActionableErrorsLogging environment variable and classpath
1 parent c958a7f commit d98fd2a

9 files changed

+884
-29
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
From 741cb2c1f09c94d23156931f4db3cc9078d607eb Mon Sep 17 00:00:00 2001
2+
From: Wes Tarle <westarle@google.com>
3+
Date: Sun, 22 Mar 2026 12:24:33 -0400
4+
Subject: [PATCH 1/6] fix(gax): make isLoggingEnabled and setLoggingEnabled
5+
package private
6+
7+
---
8+
.../main/java/com/google/api/gax/logging/LoggingUtils.java | 2 +-
9+
.../java/com/google/api/gax/logging/LoggingUtilsTest.java | 5 +++--
10+
2 files changed, 4 insertions(+), 3 deletions(-)
11+
12+
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
13+
index b1e90b24944..c42bc4a7360 100644
14+
--- a/gax-java/gax/src/main/java/com/google/api/gax/logging/LoggingUtils.java
15+
+++ b/gax-java/gax/src/main/java/com/google/api/gax/logging/LoggingUtils.java
16+
@@ -179,7 +179,7 @@ public class LoggingUtils {
17+
LoggerProvider loggerProvider,
18+
org.slf4j.event.Level level,
19+
String message) {
20+
- if (loggingV2Enabled) {
21+
+ if (loggingEnabled) {
22+
org.slf4j.Logger logger = loggerProvider.getLogger();
23+
Slf4jUtils.log(logger, level, logContext, message);
24+
}
25+
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
26+
index 08dc3e2421c..cffe2b6d2c1 100644
27+
--- a/gax-java/gax/src/test/java/com/google/api/gax/logging/LoggingUtilsTest.java
28+
+++ b/gax-java/gax/src/test/java/com/google/api/gax/logging/LoggingUtilsTest.java
29+
@@ -94,11 +94,11 @@ class LoggingUtilsTest {
30+
31+
@Test
32+
void testLogActionableError_loggingDisabled() {
33+
- LoggingUtils.setLoggingV2Enabled(false);
34+
+ LoggingUtils.setLoggingEnabled(false);
35+
LoggerProvider loggerProvider = mock(LoggerProvider.class);
36+
37+
LoggingUtils.logActionableError(
38+
- Collections.emptyMap(), loggerProvider, org.slf4j.event.Level.INFO, "message");
39+
+ Collections.emptyMap(), loggerProvider, org.slf4j.event.Level.DEBUG, "message");
40+
41+
verify(loggerProvider, never()).getLogger();
42+
}
43+
@@ -120,3 +120,4 @@ class LoggingUtilsTest {
44+
verify(loggerProvider).getLogger();
45+
}
46+
}
47+
+
48+
--
49+
2.53.0.1018.g2bb0e51243-goog
50+
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
From 5d6bc5fe332be38eea328d5a414692620741bb9c Mon Sep 17 00:00:00 2001
2+
From: Wes Tarle <westarle@google.com>
3+
Date: Sun, 22 Mar 2026 13:18:42 -0400
4+
Subject: [PATCH 3/6] fix: Actionable error utility determines DEBUG log level
5+
internally
6+
7+
---
8+
.../java/com/google/api/gax/logging/LoggingUtils.java | 8 ++------
9+
.../java/com/google/api/gax/logging/LoggingUtilsTest.java | 2 +-
10+
2 files changed, 3 insertions(+), 7 deletions(-)
11+
12+
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
13+
index 59ba3deaa00..0797527bc8b 100644
14+
--- a/gax-java/gax/src/main/java/com/google/api/gax/logging/LoggingUtils.java
15+
+++ b/gax-java/gax/src/main/java/com/google/api/gax/logging/LoggingUtils.java
16+
@@ -152,17 +152,13 @@ public class LoggingUtils {
17+
* @param logContext A map containing the structured logging context (e.g., RPC service, method,
18+
* error details).
19+
* @param loggerProvider The provider used to obtain the logger.
20+
- * @param level The slf4j level to log the actionable error at.
21+
* @param message The human-readable error message.
22+
*/
23+
public static void logActionableError(
24+
- Map<String, Object> logContext,
25+
- LoggerProvider loggerProvider,
26+
- org.slf4j.event.Level level,
27+
- String message) {
28+
+ Map<String, Object> logContext, LoggerProvider loggerProvider, String message) {
29+
if (loggingEnabled) {
30+
org.slf4j.Logger logger = loggerProvider.getLogger();
31+
- Slf4jUtils.log(logger, level, logContext, message);
32+
+ Slf4jUtils.log(logger, org.slf4j.event.Level.DEBUG, logContext, message);
33+
}
34+
}
35+
36+
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
37+
index 46ecc2c461f..d372282c15a 100644
38+
--- a/gax-java/gax/src/test/java/com/google/api/gax/logging/LoggingUtilsTest.java
39+
+++ b/gax-java/gax/src/test/java/com/google/api/gax/logging/LoggingUtilsTest.java
40+
@@ -115,7 +115,7 @@ class LoggingUtilsTest {
41+
when(eventBuilder.addKeyValue(anyString(), any())).thenReturn(eventBuilder);
42+
43+
Map<String, Object> context = Collections.singletonMap("key", "value");
44+
- LoggingUtils.logActionableError(context, loggerProvider, org.slf4j.event.Level.DEBUG, "message");
45+
+ LoggingUtils.logActionableError(context, loggerProvider, "message");
46+
47+
verify(loggerProvider).getLogger();
48+
}
49+
--
50+
2.53.0.1018.g2bb0e51243-goog
51+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
From 38838d1a15a129067f9dcd6e90b0b031c0c56ef8 Mon Sep 17 00:00:00 2001
2+
From: Wes Tarle <westarle@google.com>
3+
Date: Sun, 22 Mar 2026 13:35:08 -0400
4+
Subject: [PATCH 4/6] fix: Actionable error utility determines DEBUG log level
5+
internally
6+
7+
---
8+
.../test/java/com/google/api/gax/logging/LoggingUtilsTest.java | 2 +-
9+
1 file changed, 1 insertion(+), 1 deletion(-)
10+
11+
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
12+
index d372282c15a..134d0c42eff 100644
13+
--- a/gax-java/gax/src/test/java/com/google/api/gax/logging/LoggingUtilsTest.java
14+
+++ b/gax-java/gax/src/test/java/com/google/api/gax/logging/LoggingUtilsTest.java
15+
@@ -98,7 +98,7 @@ class LoggingUtilsTest {
16+
LoggerProvider loggerProvider = mock(LoggerProvider.class);
17+
18+
LoggingUtils.logActionableError(
19+
- Collections.emptyMap(), loggerProvider, org.slf4j.event.Level.DEBUG, "message");
20+
+ Collections.<String, Object>emptyMap(), loggerProvider, "message");
21+
22+
verify(loggerProvider, never()).getLogger();
23+
}
24+
--
25+
2.53.0.1018.g2bb0e51243-goog
26+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
From b51ef3c3a790f1d9c9c5b714d47fd19f9c5982c9 Mon Sep 17 00:00:00 2001
2+
From: Wes Tarle <westarle@google.com>
3+
Date: Sun, 22 Mar 2026 13:52:03 -0400
4+
Subject: [PATCH 5/6] style(gax): format LoggingUtils
5+
6+
---
7+
.../test/java/com/google/api/gax/logging/LoggingUtilsTest.java | 1 -
8+
1 file changed, 1 deletion(-)
9+
10+
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
11+
index 134d0c42eff..e3acf17637a 100644
12+
--- a/gax-java/gax/src/test/java/com/google/api/gax/logging/LoggingUtilsTest.java
13+
+++ b/gax-java/gax/src/test/java/com/google/api/gax/logging/LoggingUtilsTest.java
14+
@@ -120,4 +120,3 @@ class LoggingUtilsTest {
15+
verify(loggerProvider).getLogger();
16+
}
17+
}
18+
-
19+
--
20+
2.53.0.1018.g2bb0e51243-goog
21+

0 commit comments

Comments
 (0)