Skip to content

Commit

Permalink
replace txt message to json message with all fields duplicated.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhumin8 committed Dec 10, 2024
1 parent b870d81 commit 56a2870
Showing 1 changed file with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.google.api.core.InternalApi;
import com.google.api.gax.rpc.internal.EnvironmentProvider;
import com.google.api.gax.rpc.internal.SystemEnvironmentProvider;
import com.google.gson.Gson;
import java.util.Map;
import org.slf4j.ILoggerFactory;
import org.slf4j.Logger;
Expand All @@ -46,6 +47,7 @@ public class LoggingUtils {
private static final Logger NO_OP_LOGGER = org.slf4j.helpers.NOPLogger.NOP_LOGGER;
private static boolean loggingEnabled = isLoggingEnabled();
static final String GOOGLE_SDK_JAVA_LOGGING = "GOOGLE_SDK_JAVA_LOGGING";
private static final Gson gson = new Gson();
// expose this setter for testing purposes
static void setEnvironmentProvider(EnvironmentProvider provider) {
environmentProvider = provider;
Expand All @@ -71,7 +73,11 @@ static Logger getLogger(Class<?> clazz, LoggerFactoryProvider factoryProvider) {

public static void logWithMDC(
Logger logger, org.slf4j.event.Level level, Map<String, String> contextMap, String message) {
contextMap.forEach(MDC::put);
if (!contextMap.isEmpty()) {
contextMap.forEach(MDC::put);
contextMap.put("message", message);
message = gson.toJson(contextMap);
}
switch (level) {
case TRACE:
logger.trace(message);
Expand All @@ -92,7 +98,9 @@ public static void logWithMDC(
logger.info(message);
// Default to INFO level
}
MDC.clear();
if (!contextMap.isEmpty()) {
MDC.clear();
}
}

static boolean isLoggingEnabled() {
Expand Down

0 comments on commit 56a2870

Please sign in to comment.