Skip to content

Commit adb06fe

Browse files
authored
feat(gax): add utility for logging actionable errors (#4144)
This PR introduces the `logActionableError` utility method to the core `LoggingUtils` class within the `gax` module. This will be used by an upcoming gRPC interceptor (`gax-grpc`) and HTTP/JSON interceptor (`gax-httpjson`).
1 parent e8b6d50 commit adb06fe

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

gax-java/gax/src/main/java/com/google/api/gax/logging/LoggingUtils.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,31 @@
3636
@InternalApi
3737
public class LoggingUtils {
3838

39-
private static boolean loggingEnabled = isLoggingEnabled();
4039
static final String GOOGLE_SDK_JAVA_LOGGING = "GOOGLE_SDK_JAVA_LOGGING";
4140

41+
private static boolean loggingEnabled = checkLoggingEnabled(GOOGLE_SDK_JAVA_LOGGING);
42+
43+
/**
44+
* Returns whether client-side logging is enabled.
45+
*
46+
* @return true if logging is enabled, false otherwise.
47+
*/
4248
static boolean isLoggingEnabled() {
43-
String enableLogging = System.getenv(GOOGLE_SDK_JAVA_LOGGING);
49+
return loggingEnabled;
50+
}
51+
52+
/**
53+
* Sets whether client-side logging is enabled. Visible for testing.
54+
*
55+
* @param enabled true to enable logging, false to disable.
56+
*/
57+
@com.google.common.annotations.VisibleForTesting
58+
static void setLoggingEnabled(boolean enabled) {
59+
loggingEnabled = enabled;
60+
}
61+
62+
private static boolean checkLoggingEnabled(String envVar) {
63+
String enableLogging = System.getenv(envVar);
4464
return "true".equalsIgnoreCase(enableLogging);
4565
}
4666

@@ -126,6 +146,22 @@ public static <RespT> void logRequest(
126146
}
127147
}
128148

149+
/**
150+
* Logs an actionable error message with structured context at a specific log level.
151+
*
152+
* @param logContext A map containing the structured logging context (e.g., RPC service, method,
153+
* error details).
154+
* @param loggerProvider The provider used to obtain the logger.
155+
* @param message The human-readable error message.
156+
*/
157+
public static void logActionableError(
158+
Map<String, Object> logContext, LoggerProvider loggerProvider, String message) {
159+
if (loggingEnabled) {
160+
org.slf4j.Logger logger = loggerProvider.getLogger();
161+
Slf4jUtils.log(logger, org.slf4j.event.Level.DEBUG, logContext, message);
162+
}
163+
}
164+
129165
public static void executeWithTryCatch(ThrowingRunnable action) {
130166
try {
131167
action.run();

gax-java/gax/src/test/java/com/google/api/gax/logging/LoggingUtilsTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,20 @@
3333
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
3434
import static org.junit.jupiter.api.Assertions.assertEquals;
3535
import static org.junit.jupiter.api.Assertions.assertFalse;
36+
import static org.mockito.ArgumentMatchers.any;
37+
import static org.mockito.ArgumentMatchers.anyString;
38+
import static org.mockito.Mockito.mock;
39+
import static org.mockito.Mockito.never;
3640
import static org.mockito.Mockito.verify;
41+
import static org.mockito.Mockito.when;
3742

3843
import com.google.api.gax.logging.LoggingUtils.ThrowingRunnable;
44+
import java.util.Collections;
45+
import java.util.Map;
46+
import org.junit.jupiter.api.AfterEach;
3947
import org.junit.jupiter.api.Test;
4048
import org.mockito.Mockito;
49+
import org.slf4j.Logger;
4150

4251
class LoggingUtilsTest {
4352

@@ -77,4 +86,37 @@ void testExecuteWithTryCatch_WithNoSuchMethodError() throws Throwable {
7786
// Verify that the action was executed (despite the error)
7887
verify(action).run();
7988
}
89+
90+
@AfterEach
91+
void tearDown() {
92+
LoggingUtils.setLoggingEnabled(false);
93+
}
94+
95+
@Test
96+
void testLogActionableError_loggingDisabled() {
97+
LoggingUtils.setLoggingEnabled(false);
98+
LoggerProvider loggerProvider = mock(LoggerProvider.class);
99+
100+
LoggingUtils.logActionableError(
101+
Collections.<String, Object>emptyMap(), loggerProvider, "message");
102+
103+
verify(loggerProvider, never()).getLogger();
104+
}
105+
106+
@Test
107+
void testLogActionableError_success() {
108+
LoggingUtils.setLoggingEnabled(true);
109+
LoggerProvider loggerProvider = mock(LoggerProvider.class);
110+
Logger logger = mock(Logger.class);
111+
when(loggerProvider.getLogger()).thenReturn(logger);
112+
113+
org.slf4j.spi.LoggingEventBuilder eventBuilder = mock(org.slf4j.spi.LoggingEventBuilder.class);
114+
when(logger.atDebug()).thenReturn(eventBuilder);
115+
when(eventBuilder.addKeyValue(anyString(), any())).thenReturn(eventBuilder);
116+
117+
Map<String, Object> context = Collections.singletonMap("key", "value");
118+
LoggingUtils.logActionableError(context, loggerProvider, "message");
119+
120+
verify(loggerProvider).getLogger();
121+
}
80122
}

0 commit comments

Comments
 (0)