Skip to content

Commit

Permalink
Replace eventId exclude list with filter function callback option.
Browse files Browse the repository at this point in the history
  • Loading branch information
novalisdenahi committed Jun 27, 2024
1 parent 724f4fb commit bcc9f07
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 23 deletions.
11 changes: 6 additions & 5 deletions src/main/java/com/configcat/ConfigCatClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -707,7 +708,7 @@ public static class Options {
private User defaultUser;
private boolean offline = false;
private final ConfigCatHooks configCatHooks = new ConfigCatHooks();
private List<Integer> excludeLogEventIds;
private Function<FilterFunctionParameters, Boolean> logFilterFunction;


/**
Expand Down Expand Up @@ -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<Integer> excludeLogEventIds) {
this.excludeLogEventIds = excludeLogEventIds;
public void logFilterFunction(Function<FilterFunctionParameters, Boolean> logFilterFunction) {
this.logFilterFunction = logFilterFunction;
}

private boolean isBaseURLCustom() {
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/com/configcat/ConfigCatLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer> excludeEventIds;
private final Function<FilterFunctionParameters, Boolean> filterOutFunction;

public ConfigCatLogger(Logger logger, LogLevel logLevel, ConfigCatHooks configCatHooks, List<Integer> excludeEventIds) {
public ConfigCatLogger(Logger logger, LogLevel logLevel, ConfigCatHooks configCatHooks, Function<FilterFunctionParameters, Boolean> filterOutFunction) {
this.logger = logger;
this.logLevel = logLevel;
this.configCatHooks = configCatHooks;
this.excludeEventIds = excludeEventIds;
this.filterOutFunction = filterOutFunction;
}

public ConfigCatLogger(Logger logger, LogLevel logLevel) {
Expand All @@ -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);
}
}
39 changes: 39 additions & 0 deletions src/main/java/com/configcat/FilterFunctionParameters.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
10 changes: 3 additions & 7 deletions src/test/java/com/configcat/LoggerTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand Down Expand Up @@ -95,12 +94,9 @@ public void noLog() {
public void excludeLogEvents() {
Logger mockLogger = mock(Logger.class);

List<Integer> excludeEventIds = new ArrayList<>();
excludeEventIds.add(1001);
excludeEventIds.add(3001);
excludeEventIds.add(5001);
Function<FilterFunctionParameters, Boolean> 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");
Expand Down

0 comments on commit bcc9f07

Please sign in to comment.