Skip to content

Commit 803d045

Browse files
Exclude log by Event Id (#53)
* Added excludeLogEventIds options to client. The logger will exclude logs if the log event id is on the provided list. Prepare 9.3.0 release. * Replace eventId exclude list with filter function callback option. * Rework based on code review. * Remove unused imports. * Fix method order. Add missing JavaDoc. * Update src/main/java/com/configcat/ConfigCatClient.java Co-authored-by: adams85 <31276480+adams85@users.noreply.github.com> * Fix interface visibility. --------- Co-authored-by: adams85 <31276480+adams85@users.noreply.github.com>
1 parent b9d2afe commit 803d045

File tree

6 files changed

+68
-10
lines changed

6 files changed

+68
-10
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version=9.2.0
1+
version=9.3.0

src/main/java/com/configcat/ConfigCatClient.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public final class ConfigCatClient implements ConfigurationProvider {
2828
private final LogLevel clientLogLevel;
2929

3030
private ConfigCatClient(String sdkKey, Options options) {
31-
this.logger = new ConfigCatLogger(LoggerFactory.getLogger(ConfigCatClient.class), options.logLevel, options.configCatHooks);
31+
this.logger = new ConfigCatLogger(LoggerFactory.getLogger(ConfigCatClient.class), options.logLevel, options.configCatHooks, options.logFilter);
3232
this.clientLogLevel = options.logLevel;
3333

3434
this.sdkKey = sdkKey;
@@ -707,6 +707,7 @@ public static class Options {
707707
private User defaultUser;
708708
private boolean offline = false;
709709
private final ConfigCatHooks configCatHooks = new ConfigCatHooks();
710+
private LogFilterFunction logFilter;
710711

711712

712713
/**
@@ -813,6 +814,13 @@ public ConfigCatHooks hooks() {
813814
return configCatHooks;
814815
}
815816

817+
/**
818+
* Set the client's log filter callback function. When logFilterFunction returns false, the ConfigCatLogger skips the log event.
819+
*/
820+
public void logFilter(LogFilterFunction logFilter) {
821+
this.logFilter = logFilter;
822+
}
823+
816824
private boolean isBaseURLCustom() {
817825
return this.baseUrl != null && !this.baseUrl.isEmpty();
818826
}

src/main/java/com/configcat/ConfigCatLogger.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,56 @@ class ConfigCatLogger {
66
private final Logger logger;
77
private final LogLevel logLevel;
88
private final ConfigCatHooks configCatHooks;
9+
private final LogFilterFunction filterFunction ;
910

10-
public ConfigCatLogger(Logger logger, LogLevel logLevel, ConfigCatHooks configCatHooks) {
11+
public ConfigCatLogger(Logger logger, LogLevel logLevel, ConfigCatHooks configCatHooks, LogFilterFunction filterFunction ) {
1112
this.logger = logger;
1213
this.logLevel = logLevel;
1314
this.configCatHooks = configCatHooks;
15+
this.filterFunction = filterFunction ;
1416
}
1517

1618
public ConfigCatLogger(Logger logger, LogLevel logLevel) {
17-
this(logger, logLevel, null);
19+
this(logger, logLevel, null, null);
1820
}
1921

2022
public ConfigCatLogger(Logger logger) {
2123
this(logger, LogLevel.WARNING);
2224
}
2325

2426
public void warn(int eventId, String message) {
25-
if (this.logLevel.ordinal() <= LogLevel.WARNING.ordinal()) {
27+
if (filter(eventId, LogLevel.WARNING, message, null)) {
2628
this.logger.warn("[{}] {}", eventId, message);
2729
}
2830
}
2931

3032
public void error(int eventId, String message, Exception exception) {
3133
if (this.configCatHooks != null) this.configCatHooks.invokeOnError(message);
32-
if (this.logLevel.ordinal() <= LogLevel.ERROR.ordinal()) {
34+
if (filter(eventId, LogLevel.ERROR, message, exception)) {
3335
this.logger.error("[{}] {}", eventId, message, exception);
3436
}
3537
}
3638

3739
public void error(int eventId, String message) {
3840
if (this.configCatHooks != null) this.configCatHooks.invokeOnError(message);
39-
if (this.logLevel.ordinal() <= LogLevel.ERROR.ordinal()) {
41+
if (filter(eventId, LogLevel.ERROR, message, null)) {
4042
this.logger.error("[{}] {}", eventId, message);
4143
}
4244
}
4345

4446
public void info(int eventId, String message) {
45-
if (this.logLevel.ordinal() <= LogLevel.INFO.ordinal()) {
47+
if (filter(eventId, LogLevel.INFO, message, null)) {
4648
this.logger.info("[{}] {}", eventId, message);
4749
}
4850
}
4951

5052
public void debug(String message) {
51-
if (this.logLevel.ordinal() <= LogLevel.DEBUG.ordinal()) {
53+
if (filter(0, LogLevel.DEBUG, message, null)) {
5254
this.logger.debug("[{}] {}", 0, message);
5355
}
5456
}
57+
58+
private boolean filter(int eventId, LogLevel logLevel, String message, Exception exception) {
59+
return this.logLevel.ordinal() <= logLevel.ordinal() && (this.filterFunction == null || this.filterFunction.apply(logLevel, eventId, message, exception));
60+
}
5561
}

src/main/java/com/configcat/Constants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ private Constants() { /* prevent from instantiation*/ }
77
static final long DISTANT_PAST = 0;
88
static final String CONFIG_JSON_NAME = "config_v6.json";
99
static final String SERIALIZATION_FORMAT_VERSION = "v2";
10-
static final String VERSION = "9.2.0";
10+
static final String VERSION = "9.3.0";
1111

1212
static final String SDK_KEY_PROXY_PREFIX = "configcat-proxy/";
1313
static final String SDK_KEY_PREFIX = "configcat-sdk-1";
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.configcat;
2+
3+
/**
4+
* The Log Filter Functional Interface provides a custom filter option for the ConfigCat Logger.
5+
*/
6+
@FunctionalInterface
7+
public interface LogFilterFunction {
8+
9+
/**
10+
* Apply the custom filter option to the ConfigCatLogger.
11+
*
12+
* @param logLevel Event severity level.
13+
* @param eventId Event identifier.
14+
* @param message Message.
15+
* @param exception The exception object related to the message (if any).
16+
* @return True to log the event, false will leave out the log.
17+
*/
18+
boolean apply(LogLevel logLevel, int eventId, String message, Throwable exception);
19+
}

src/test/java/com/configcat/LoggerTests.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,29 @@ public void noLog() {
8787
verify(mockLogger, never()).warn(anyString(), eq(3000), eq("warn"));
8888
verify(mockLogger, never()).error(anyString(), eq(1000), eq("error"), any(Exception.class));
8989
}
90+
91+
@Test
92+
public void excludeLogEvents() {
93+
Logger mockLogger = mock(Logger.class);
94+
95+
LogFilterFunction filterLogFunction = ( LogLevel logLevel, int eventId, String message, Throwable exception) -> eventId != 1001 && eventId != 3001 && eventId != 5001;
96+
97+
ConfigCatLogger logger = new ConfigCatLogger(mockLogger, LogLevel.INFO, null, filterLogFunction);
98+
99+
logger.debug("[0] debug");
100+
logger.info(5000, "info");
101+
logger.warn(3000, "warn");
102+
logger.error(1000, "error", new Exception());
103+
logger.info(5001, "info");
104+
logger.warn(3001, "warn");
105+
logger.error(1001, "error", new Exception());
106+
107+
verify(mockLogger, never()).debug(anyString(), eq(0), eq("debug"));
108+
verify(mockLogger, times(1)).info(anyString(), eq(5000), eq("info"));
109+
verify(mockLogger, times(1)).warn(anyString(), eq(3000), eq("warn"));
110+
verify(mockLogger, times(1)).error(anyString(), eq(1000), eq("error"), any(Exception.class));
111+
verify(mockLogger, never()).info(anyString(), eq(5001), eq("info"));
112+
verify(mockLogger, never()).warn(anyString(), eq(3001), eq("warn"));
113+
verify(mockLogger, never()).error(anyString(), eq(1001), eq("error"), any(Exception.class));
114+
}
90115
}

0 commit comments

Comments
 (0)