|
36 | 36 | @InternalApi |
37 | 37 | public class LoggingUtils { |
38 | 38 |
|
39 | | - private static boolean loggingEnabled = isLoggingEnabled(); |
| 39 | + private static boolean loggingEnabled = checkLoggingEnabled(); |
40 | 40 | static final String GOOGLE_SDK_JAVA_LOGGING = "GOOGLE_SDK_JAVA_LOGGING"; |
41 | 41 |
|
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() { |
43 | 52 | String enableLogging = System.getenv(GOOGLE_SDK_JAVA_LOGGING); |
44 | 53 | return "true".equalsIgnoreCase(enableLogging); |
45 | 54 | } |
@@ -126,6 +135,24 @@ public static <RespT> void logRequest( |
126 | 135 | } |
127 | 136 | } |
128 | 137 |
|
| 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 | + |
129 | 156 | public static void executeWithTryCatch(ThrowingRunnable action) { |
130 | 157 | try { |
131 | 158 | action.run(); |
|
0 commit comments