diff --git a/src/main/java/com/configcat/ConfigCatClient.java b/src/main/java/com/configcat/ConfigCatClient.java index 81b068e..4919c7a 100644 --- a/src/main/java/com/configcat/ConfigCatClient.java +++ b/src/main/java/com/configcat/ConfigCatClient.java @@ -8,6 +8,7 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Consumer; +import java.util.function.Function; /** * A client for handling configurations provided by ConfigCat. @@ -28,7 +29,7 @@ public final class ConfigCatClient implements ConfigurationProvider { private final LogLevel clientLogLevel; private ConfigCatClient(String sdkKey, Options options) { - this.logger = new ConfigCatLogger(LoggerFactory.getLogger(ConfigCatClient.class), options.logLevel, options.configCatHooks, options.excludeLogEventIds); + this.logger = new ConfigCatLogger(LoggerFactory.getLogger(ConfigCatClient.class), options.logLevel, options.configCatHooks, options.logFilterFunction); this.clientLogLevel = options.logLevel; this.sdkKey = sdkKey; @@ -707,7 +708,7 @@ public static class Options { private User defaultUser; private boolean offline = false; private final ConfigCatHooks configCatHooks = new ConfigCatHooks(); - private List excludeLogEventIds; + private Function logFilterFunction; /** @@ -815,10 +816,10 @@ public ConfigCatHooks hooks() { } /** - * Set the exclude Log Event Ids. + * Set the client's log filter callback function. When logFilterFunction returns true, the ConfigCatLogger skips the logging. */ - public void excludeLogEventIds(List excludeLogEventIds) { - this.excludeLogEventIds = excludeLogEventIds; + public void logFilterFunction(Function logFilterFunction) { + this.logFilterFunction = logFilterFunction; } private boolean isBaseURLCustom() { diff --git a/src/main/java/com/configcat/ConfigCatLogger.java b/src/main/java/com/configcat/ConfigCatLogger.java index c758bf3..5a81c18 100644 --- a/src/main/java/com/configcat/ConfigCatLogger.java +++ b/src/main/java/com/configcat/ConfigCatLogger.java @@ -2,19 +2,19 @@ import org.slf4j.Logger; -import java.util.List; +import java.util.function.Function; class ConfigCatLogger { private final Logger logger; private final LogLevel logLevel; private final ConfigCatHooks configCatHooks; - private final List excludeEventIds; + private final Function filterOutFunction; - public ConfigCatLogger(Logger logger, LogLevel logLevel, ConfigCatHooks configCatHooks, List excludeEventIds) { + public ConfigCatLogger(Logger logger, LogLevel logLevel, ConfigCatHooks configCatHooks, Function filterOutFunction) { this.logger = logger; this.logLevel = logLevel; this.configCatHooks = configCatHooks; - this.excludeEventIds = excludeEventIds; + this.filterOutFunction = filterOutFunction; } public ConfigCatLogger(Logger logger, LogLevel logLevel) { @@ -26,38 +26,38 @@ public ConfigCatLogger(Logger logger) { } public void warn(int eventId, String message) { - if (this.logLevel.ordinal() <= LogLevel.WARNING.ordinal() && !checkExcludeEventId(eventId)) { + if (this.logLevel.ordinal() <= LogLevel.WARNING.ordinal() && !checkFilterOut(new FilterFunctionParameters(eventId, message, LogLevel.WARNING))) { this.logger.warn("[{}] {}", eventId, message); } } public void error(int eventId, String message, Exception exception) { if (this.configCatHooks != null) this.configCatHooks.invokeOnError(message); - if (this.logLevel.ordinal() <= LogLevel.ERROR.ordinal() && !checkExcludeEventId(eventId)) { + if (this.logLevel.ordinal() <= LogLevel.ERROR.ordinal() && !checkFilterOut(new FilterFunctionParameters(eventId, message, LogLevel.ERROR, exception))) { this.logger.error("[{}] {}", eventId, message, exception); } } public void error(int eventId, String message) { if (this.configCatHooks != null) this.configCatHooks.invokeOnError(message); - if (this.logLevel.ordinal() <= LogLevel.ERROR.ordinal() && !checkExcludeEventId(eventId)) { + if (this.logLevel.ordinal() <= LogLevel.ERROR.ordinal() && !checkFilterOut(new FilterFunctionParameters(eventId, message, LogLevel.ERROR))) { this.logger.error("[{}] {}", eventId, message); } } public void info(int eventId, String message) { - if (this.logLevel.ordinal() <= LogLevel.INFO.ordinal() && !checkExcludeEventId(eventId)) { + if (this.logLevel.ordinal() <= LogLevel.INFO.ordinal() && !checkFilterOut(new FilterFunctionParameters(eventId, message, LogLevel.INFO))) { this.logger.info("[{}] {}", eventId, message); } } public void debug(String message) { - if (this.logLevel.ordinal() <= LogLevel.DEBUG.ordinal()) { + if (this.logLevel.ordinal() <= LogLevel.DEBUG.ordinal() && !checkFilterOut(new FilterFunctionParameters(0, message, LogLevel.DEBUG))) { this.logger.debug("[{}] {}", 0, message); } } - private boolean checkExcludeEventId(int eventId) { - return this.excludeEventIds != null && this.excludeEventIds.contains(eventId); + private boolean checkFilterOut(FilterFunctionParameters parameters) { + return this.filterOutFunction != null && this.filterOutFunction.apply(parameters); } } diff --git a/src/main/java/com/configcat/FilterFunctionParameters.java b/src/main/java/com/configcat/FilterFunctionParameters.java new file mode 100644 index 0000000..dbde464 --- /dev/null +++ b/src/main/java/com/configcat/FilterFunctionParameters.java @@ -0,0 +1,39 @@ +package com.configcat; + +public class FilterFunctionParameters { + + private final int eventId; + + private final String message; + + private final LogLevel logLevel; + + private final Exception exception; + + public FilterFunctionParameters(int eventId, String message, LogLevel logLevel, Exception exception) { + this.eventId = eventId; + this.message = message; + this.logLevel = logLevel; + this.exception = exception; + } + + public FilterFunctionParameters(int eventId, String message, LogLevel logLevel) { + this(eventId, message, logLevel, null); + } + + public int getEventId() { + return eventId; + } + + public Exception getException() { + return exception; + } + + public LogLevel getLogLevel() { + return logLevel; + } + + public String getMessage() { + return message; + } +} diff --git a/src/test/java/com/configcat/LoggerTests.java b/src/test/java/com/configcat/LoggerTests.java index 8cb8fd7..97a9082 100644 --- a/src/test/java/com/configcat/LoggerTests.java +++ b/src/test/java/com/configcat/LoggerTests.java @@ -3,8 +3,7 @@ import org.junit.jupiter.api.Test; import org.slf4j.Logger; -import java.util.ArrayList; -import java.util.List; +import java.util.function.Function; import static org.mockito.Mockito.*; @@ -95,12 +94,9 @@ public void noLog() { public void excludeLogEvents() { Logger mockLogger = mock(Logger.class); - List excludeEventIds = new ArrayList<>(); - excludeEventIds.add(1001); - excludeEventIds.add(3001); - excludeEventIds.add(5001); + Function filterLogFunction = configCatLoggerFilterFunction -> configCatLoggerFilterFunction.getEventId() == 1001 || configCatLoggerFilterFunction.getEventId() == 3001 || configCatLoggerFilterFunction.getEventId() == 5001; - ConfigCatLogger logger = new ConfigCatLogger(mockLogger, LogLevel.INFO, null, excludeEventIds); + ConfigCatLogger logger = new ConfigCatLogger(mockLogger, LogLevel.INFO, null, filterLogFunction); logger.debug("[0] debug"); logger.info(5000, "info");