Skip to content

Commit a0df2ff

Browse files
committed
feat(gax): add utilities for logging actionable errors
1 parent 6169067 commit a0df2ff

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

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

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

39-
private static boolean loggingEnabled = isLoggingEnabled();
39+
private static boolean loggingEnabled = checkLoggingEnabled();
4040
static final String GOOGLE_SDK_JAVA_LOGGING = "GOOGLE_SDK_JAVA_LOGGING";
4141

42-
static boolean isLoggingEnabled() {
42+
/**
43+
* Returns whether client-side logging is enabled.
44+
*
45+
* @return true if logging is enabled, false otherwise.
46+
*/
47+
public static boolean isLoggingEnabled() {
48+
return loggingEnabled;
49+
}
50+
51+
/**
52+
* Sets whether client-side logging is enabled. Visible for testing.
53+
*
54+
* @param enabled true to enable logging, false to disable.
55+
*/
56+
public static void setLoggingEnabled(boolean enabled) {
57+
loggingEnabled = enabled;
58+
}
59+
60+
private static boolean checkLoggingEnabled() {
4361
String enableLogging = System.getenv(GOOGLE_SDK_JAVA_LOGGING);
4462
return "true".equalsIgnoreCase(enableLogging);
4563
}
@@ -126,6 +144,26 @@ public static <RespT> void logRequest(
126144
}
127145
}
128146

147+
/**
148+
* Logs an actionable error message with structured context at a specific log level.
149+
*
150+
* @param logContext A map containing the structured logging context (e.g., RPC service, method,
151+
* error details).
152+
* @param loggerProvider The provider used to obtain the logger.
153+
* @param level The slf4j level to log the actionable error at.
154+
* @param message The human-readable error message.
155+
*/
156+
public static void logActionableError(
157+
Map<String, Object> logContext,
158+
LoggerProvider loggerProvider,
159+
org.slf4j.event.Level level,
160+
String message) {
161+
if (loggingEnabled) {
162+
org.slf4j.Logger logger = loggerProvider.getLogger();
163+
Slf4jUtils.log(logger, level, logContext, message);
164+
}
165+
}
166+
129167
public static void executeWithTryCatch(ThrowingRunnable action) {
130168
try {
131169
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.emptyMap(), loggerProvider, org.slf4j.event.Level.INFO, "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.atInfo()).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, org.slf4j.event.Level.INFO, "message");
119+
120+
verify(loggerProvider).getLogger();
121+
}
80122
}

0 commit comments

Comments
 (0)