Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude log by Event Id #53

Merged
merged 7 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.logFilter);
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 LogFilterFunction logFilter;


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

/**
* Set the client's log filter callback function. When logFilterFunction returns true, the ConfigCatLogger skips the logging.
novalisdenahi marked this conversation as resolved.
Show resolved Hide resolved
*/
public void logFilter(LogFilterFunction logFilter) {
this.logFilter = logFilter;
}

private boolean isBaseURLCustom() {
return this.baseUrl != null && !this.baseUrl.isEmpty();
}
Expand Down
20 changes: 13 additions & 7 deletions src/main/java/com/configcat/ConfigCatLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,56 @@ class ConfigCatLogger {
private final Logger logger;
private final LogLevel logLevel;
private final ConfigCatHooks configCatHooks;
private final LogFilterFunction filterFunction ;

public ConfigCatLogger(Logger logger, LogLevel logLevel, ConfigCatHooks configCatHooks) {
public ConfigCatLogger(Logger logger, LogLevel logLevel, ConfigCatHooks configCatHooks, LogFilterFunction filterFunction ) {
this.logger = logger;
this.logLevel = logLevel;
this.configCatHooks = configCatHooks;
this.filterFunction = filterFunction ;
}

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 (filter(eventId, LogLevel.WARNING, message, null)) {
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 (filter(eventId, LogLevel.ERROR, message, 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()) {
if (filter(eventId, LogLevel.ERROR, message, null)) {
this.logger.error("[{}] {}", eventId, message);
}
}

public void info(int eventId, String message) {
if (this.logLevel.ordinal() <= LogLevel.INFO.ordinal()) {
if (filter(eventId, LogLevel.INFO, message, null)) {
this.logger.info("[{}] {}", eventId, message);
}
}

public void debug(String message) {
if (this.logLevel.ordinal() <= LogLevel.DEBUG.ordinal()) {
if (filter(0, LogLevel.DEBUG, message, null)) {
this.logger.debug("[{}] {}", 0, message);
}
}

private boolean filter(int eventId, LogLevel logLevel, String message, Exception exception) {
return this.logLevel.ordinal() <= logLevel.ordinal() && (this.filterFunction == null || this.filterFunction.apply(logLevel, eventId, message, exception));
}
}
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
19 changes: 19 additions & 0 deletions src/main/java/com/configcat/LogFilterFunction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.configcat;

/**
* The Log Filter Functional Interface provides a custom filter option for the ConfigCat Logger.
*/
@FunctionalInterface
interface LogFilterFunction {

/**
* Apply the custom filter option to the ConfigCatLogger.
*
* @param logLevel Event severity level.
* @param eventId Event identifier.
* @param message Message.
* @param exception The exception object related to the message (if any).
* @return True to log the event, false will leave out the log.
*/
boolean apply(LogLevel logLevel, int eventId, String message, Throwable exception);
}
25 changes: 25 additions & 0 deletions src/test/java/com/configcat/LoggerTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,29 @@ 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);

LogFilterFunction filterLogFunction = ( LogLevel logLevel, int eventId, String message, Throwable exception) -> eventId != 1001 && eventId != 3001 && eventId != 5001;

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

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));
}
}