Skip to content

Commit bbbdfcf

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

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,19 @@
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+
public static boolean isLoggingEnabled() {
43+
return loggingEnabled;
44+
}
45+
46+
// visible for testing
47+
public static void setLoggingEnabled(boolean enabled) {
48+
loggingEnabled = enabled;
49+
}
50+
51+
private static boolean checkLoggingEnabled() {
4352
String enableLogging = System.getenv(GOOGLE_SDK_JAVA_LOGGING);
4453
return "true".equalsIgnoreCase(enableLogging);
4554
}
@@ -126,6 +135,24 @@ public static <RespT> void logRequest(
126135
}
127136
}
128137

138+
/**
139+
* Logs an actionable error message with structured context.
140+
*
141+
* @param logContext A map containing the structured logging context (e.g., RPC service, method,
142+
* error details).
143+
* @param loggerProvider The provider used to obtain the logger.
144+
* @param message The human-readable error message.
145+
*/
146+
public static void logActionableError(
147+
Map<String, Object> logContext, LoggerProvider loggerProvider, String message) {
148+
if (loggingEnabled) {
149+
org.slf4j.Logger logger = loggerProvider.getLogger();
150+
if (logger.isInfoEnabled()) {
151+
Slf4jUtils.log(logger, org.slf4j.event.Level.INFO, logContext, message);
152+
}
153+
}
154+
}
155+
129156
public static void executeWithTryCatch(ThrowingRunnable action) {
130157
try {
131158
action.run();

0 commit comments

Comments
 (0)