Skip to content

Commit

Permalink
Added excludeLogEventIds options to client. The logger will exclude l…
Browse files Browse the repository at this point in the history
…ogs if the log event id is on the provided list.

Prepare 9.3.0 release.
  • Loading branch information
novalisdenahi committed Jun 26, 2024
1 parent b366878 commit 724f4fb
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 9 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=9.2.0
version=9.3.0
10 changes: 9 additions & 1 deletion src/main/java/com/configcat/ConfigCatClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,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);
this.logger = new ConfigCatLogger(LoggerFactory.getLogger(ConfigCatClient.class), options.logLevel, options.configCatHooks, options.excludeLogEventIds);
this.clientLogLevel = options.logLevel;

this.sdkKey = sdkKey;
Expand Down Expand Up @@ -707,6 +707,7 @@ public static class Options {
private User defaultUser;
private boolean offline = false;
private final ConfigCatHooks configCatHooks = new ConfigCatHooks();
private List<Integer> excludeLogEventIds;


/**
Expand Down Expand Up @@ -813,6 +814,13 @@ public ConfigCatHooks hooks() {
return configCatHooks;
}

/**
* Set the exclude Log Event Ids.
*/
public void excludeLogEventIds(List<Integer> excludeLogEventIds) {
this.excludeLogEventIds = excludeLogEventIds;
}

private boolean isBaseURLCustom() {
return this.baseUrl != null && !this.baseUrl.isEmpty();
}
Expand Down
20 changes: 14 additions & 6 deletions src/main/java/com/configcat/ConfigCatLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,51 @@

import org.slf4j.Logger;

import java.util.List;

class ConfigCatLogger {
private final Logger logger;
private final LogLevel logLevel;
private final ConfigCatHooks configCatHooks;
private final List<Integer> excludeEventIds;

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

public ConfigCatLogger(Logger logger, LogLevel logLevel) {
this(logger, logLevel, null);
this(logger, logLevel, null, null);
}

public ConfigCatLogger(Logger logger) {
this(logger, LogLevel.WARNING);
}

public void warn(int eventId, String message) {
if (this.logLevel.ordinal() <= LogLevel.WARNING.ordinal()) {
if (this.logLevel.ordinal() <= LogLevel.WARNING.ordinal() && !checkExcludeEventId(eventId)) {
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()) {
if (this.logLevel.ordinal() <= LogLevel.ERROR.ordinal() && !checkExcludeEventId(eventId)) {
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()) {
if (this.logLevel.ordinal() <= LogLevel.ERROR.ordinal() && !checkExcludeEventId(eventId)) {
this.logger.error("[{}] {}", eventId, message);
}
}

public void info(int eventId, String message) {
if (this.logLevel.ordinal() <= LogLevel.INFO.ordinal()) {
if (this.logLevel.ordinal() <= LogLevel.INFO.ordinal() && !checkExcludeEventId(eventId)) {
this.logger.info("[{}] {}", eventId, message);
}
}
Expand All @@ -52,4 +56,8 @@ public void debug(String message) {
this.logger.debug("[{}] {}", 0, message);
}
}

private boolean checkExcludeEventId(int eventId) {
return this.excludeEventIds != null && this.excludeEventIds.contains(eventId);
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/configcat/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ private Constants() { /* prevent from instantiation*/ }
static final long DISTANT_PAST = 0;
static final String CONFIG_JSON_NAME = "config_v6.json";
static final String SERIALIZATION_FORMAT_VERSION = "v2";
static final String VERSION = "9.2.0";
static final String VERSION = "9.3.0";

static final String SDK_KEY_PROXY_PREFIX = "configcat-proxy/";
static final String SDK_KEY_PREFIX = "configcat-sdk-1";
Expand Down
31 changes: 31 additions & 0 deletions src/test/java/com/configcat/LoggerTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;

import java.util.ArrayList;
import java.util.List;

import static org.mockito.Mockito.*;

public class LoggerTests {
Expand Down Expand Up @@ -87,4 +90,32 @@ public void noLog() {
verify(mockLogger, never()).warn(anyString(), eq(3000), eq("warn"));
verify(mockLogger, never()).error(anyString(), eq(1000), eq("error"), any(Exception.class));
}

@Test
public void excludeLogEvents() {
Logger mockLogger = mock(Logger.class);

List<Integer> excludeEventIds = new ArrayList<>();
excludeEventIds.add(1001);
excludeEventIds.add(3001);
excludeEventIds.add(5001);

ConfigCatLogger logger = new ConfigCatLogger(mockLogger, LogLevel.INFO, null, excludeEventIds);

logger.debug("[0] debug");
logger.info(5000, "info");
logger.warn(3000, "warn");
logger.error(1000, "error", new Exception());
logger.info(5001, "info");
logger.warn(3001, "warn");
logger.error(1001, "error", new Exception());

verify(mockLogger, never()).debug(anyString(), eq(0), eq("debug"));
verify(mockLogger, times(1)).info(anyString(), eq(5000), eq("info"));
verify(mockLogger, times(1)).warn(anyString(), eq(3000), eq("warn"));
verify(mockLogger, times(1)).error(anyString(), eq(1000), eq("error"), any(Exception.class));
verify(mockLogger, never()).info(anyString(), eq(5001), eq("info"));
verify(mockLogger, never()).warn(anyString(), eq(3001), eq("warn"));
verify(mockLogger, never()).error(anyString(), eq(1001), eq("error"), any(Exception.class));
}
}

0 comments on commit 724f4fb

Please sign in to comment.