Skip to content

Commit 162687c

Browse files
Added FormattableLogMessage to optimize log message format. (#55)
* Added FormattableLogMessage to optimize log message format. * Updates based on code review. * Prepare 9.4.0 release * Refactor based on review * Fix issues based on quality check. * Update based on review * Set formatLogMessage methods to protected visibility.
1 parent 803d045 commit 162687c

17 files changed

Lines changed: 186 additions & 114 deletions

gradle.properties

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

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -480,17 +480,17 @@ private boolean checkSettingsAvailable(SettingResult settingResult, String empty
480480

481481
private <T> Result<Setting> checkSettingAvailable(SettingResult settingResult, String key, T defaultValue) {
482482
if (settingResult.isEmpty()) {
483-
String errorMessage = ConfigCatLogMessages.getConfigJsonIsNotPresentedWithDefaultValue(key, "defaultValue", defaultValue);
484-
this.logger.error(1000, errorMessage);
485-
return Result.error(errorMessage, null);
483+
Object formattableLogMessage = ConfigCatLogMessages.getConfigJsonIsNotPresentedWithDefaultValue(key, "defaultValue", defaultValue);
484+
this.logger.error(1000, formattableLogMessage);
485+
return Result.error(formattableLogMessage, null);
486486
}
487487

488488
Map<String, Setting> settings = settingResult.settings();
489489
Setting setting = settings.get(key);
490490
if (setting == null) {
491-
String errorMessage = ConfigCatLogMessages.getSettingEvaluationFailedDueToMissingKey(key, "defaultValue", defaultValue, settings.keySet());
492-
this.logger.error(1001, errorMessage);
493-
return Result.error(errorMessage, null);
491+
FormattableLogMessage formattableLogMessage = ConfigCatLogMessages.getSettingEvaluationFailedDueToMissingKey(key, "defaultValue", defaultValue, settings.keySet());
492+
this.logger.error(1001, formattableLogMessage);
493+
return Result.error(formattableLogMessage, null);
494494
}
495495

496496
return Result.success(setting);
@@ -506,9 +506,9 @@ private <T> T getValueFromSettingsMap(Class<T> classOfT, SettingResult settingRe
506506

507507
return this.evaluate(classOfT, checkSettingResult.value(), key, getEvaluateUser(user), settingResult.fetchTime(), settingResult.settings()).getValue();
508508
} catch (Exception e) {
509-
String errorMessage = ConfigCatLogMessages.getSettingEvaluationFailedForOtherReason(key, "defaultValue", defaultValue);
510-
this.logger.error(2001, errorMessage, e);
511-
this.configCatHooks.invokeOnFlagEvaluated(EvaluationDetails.fromError(key, defaultValue, errorMessage + " " + e.getMessage(), user));
509+
FormattableLogMessage formattableLogMessage = ConfigCatLogMessages.getSettingEvaluationFailedForOtherReason(key, "defaultValue", defaultValue);
510+
this.logger.error(2001, formattableLogMessage, e);
511+
this.configCatHooks.invokeOnFlagEvaluated(EvaluationDetails.fromError(key, defaultValue, formattableLogMessage + " " + e.getMessage(), user));
512512
return defaultValue;
513513
}
514514
}

src/main/java/com/configcat/ConfigCatHooks.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,14 @@ void invokeOnClientReady(ClientCacheState clientCacheState) {
116116
}
117117
}
118118

119-
void invokeOnError(String error) {
119+
void invokeOnError(Object error) {
120120
lock.readLock().lock();
121121
try {
122-
for (Consumer<String> func : this.onError) {
123-
func.accept(error);
122+
if(!this.onError.isEmpty()) {
123+
String errorMessage = error.toString();
124+
for (Consumer<String> func : this.onError) {
125+
func.accept(errorMessage);
126+
}
124127
}
125128
} finally {
126129
lock.readLock().unlock();

src/main/java/com/configcat/ConfigCatLogMessages.java

Lines changed: 63 additions & 64 deletions
Large diffs are not rendered by default.

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,39 +23,39 @@ public ConfigCatLogger(Logger logger) {
2323
this(logger, LogLevel.WARNING);
2424
}
2525

26-
public void warn(int eventId, String message) {
26+
public void warn(int eventId, Object message) {
2727
if (filter(eventId, LogLevel.WARNING, message, null)) {
2828
this.logger.warn("[{}] {}", eventId, message);
2929
}
3030
}
3131

32-
public void error(int eventId, String message, Exception exception) {
32+
public void error(int eventId, Object message, Exception exception) {
3333
if (this.configCatHooks != null) this.configCatHooks.invokeOnError(message);
3434
if (filter(eventId, LogLevel.ERROR, message, exception)) {
3535
this.logger.error("[{}] {}", eventId, message, exception);
3636
}
3737
}
3838

39-
public void error(int eventId, String message) {
39+
public void error(int eventId, Object message) {
4040
if (this.configCatHooks != null) this.configCatHooks.invokeOnError(message);
4141
if (filter(eventId, LogLevel.ERROR, message, null)) {
4242
this.logger.error("[{}] {}", eventId, message);
4343
}
4444
}
4545

46-
public void info(int eventId, String message) {
46+
public void info(int eventId, Object message) {
4747
if (filter(eventId, LogLevel.INFO, message, null)) {
4848
this.logger.info("[{}] {}", eventId, message);
4949
}
5050
}
5151

52-
public void debug(String message) {
52+
public void debug(Object message) {
5353
if (filter(0, LogLevel.DEBUG, message, null)) {
5454
this.logger.debug("[{}] {}", 0, message);
5555
}
5656
}
5757

58-
private boolean filter(int eventId, LogLevel logLevel, String message, Exception exception) {
58+
private boolean filter(int eventId, LogLevel logLevel, Object message, Exception exception) {
5959
return this.logLevel.ordinal() <= logLevel.ordinal() && (this.filterFunction == null || this.filterFunction.apply(logLevel, eventId, message, exception));
6060
}
6161
}

src/main/java/com/configcat/ConfigFetcher.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ private CompletableFuture<FetchResponse> getResponseAsync(final String eTag) {
9898
@Override
9999
public void onFailure(@NotNull Call call, @NotNull IOException e) {
100100
int logEventId = 1103;
101-
String message = ConfigCatLogMessages.FETCH_FAILED_DUE_TO_UNEXPECTED_ERROR;
101+
Object message = ConfigCatLogMessages.FETCH_FAILED_DUE_TO_UNEXPECTED_ERROR;
102102
if (!isClosed.get()) {
103103
if (e instanceof SocketTimeoutException) {
104104
logEventId = 1102;
@@ -130,14 +130,14 @@ public void onResponse(@NotNull Call call, @NotNull Response response) {
130130
logger.error(1100, message);
131131
future.complete(FetchResponse.failed(message, true));
132132
} else {
133-
String message = ConfigCatLogMessages.getFetchFailedDueToUnexpectedHttpResponse(response.code(), response.message());
134-
logger.error(1101, message);
135-
future.complete(FetchResponse.failed(message, false));
133+
FormattableLogMessage formattableLogMessage = ConfigCatLogMessages.getFetchFailedDueToUnexpectedHttpResponse(response.code(), response.message());
134+
logger.error(1101, formattableLogMessage);
135+
future.complete(FetchResponse.failed(formattableLogMessage, false));
136136
}
137137
} catch (SocketTimeoutException e) {
138-
String message = ConfigCatLogMessages.getFetchFailedDueToRequestTimeout(httpClient.connectTimeoutMillis(), httpClient.readTimeoutMillis(), httpClient.writeTimeoutMillis());
139-
logger.error(1102, message, e);
140-
future.complete(FetchResponse.failed(message, false));
138+
FormattableLogMessage formattableLogMessage = ConfigCatLogMessages.getFetchFailedDueToRequestTimeout(httpClient.connectTimeoutMillis(), httpClient.readTimeoutMillis(), httpClient.writeTimeoutMillis());
139+
logger.error(1102, formattableLogMessage, e);
140+
future.complete(FetchResponse.failed(formattableLogMessage, false));
141141
} catch (Exception e) {
142142
String message = ConfigCatLogMessages.FETCH_FAILED_DUE_TO_UNEXPECTED_ERROR;
143143
logger.error(1103, message, e);

src/main/java/com/configcat/ConfigService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ public ConfigService(String sdkKey,
5959
lock.lock();
6060
try {
6161
this.configCatHooks.invokeOnClientReady(determineCacheState(cachedEntry.get()));
62-
String message = ConfigCatLogMessages.getAutoPollMaxInitWaitTimeReached(autoPollingMode.getMaxInitWaitTimeSeconds());
63-
this.logger.warn(4200, message);
64-
completeRunningTask(Result.error(message, cachedEntry.get()));
62+
FormattableLogMessage formattableLogMessage = ConfigCatLogMessages.getAutoPollMaxInitWaitTimeReached(autoPollingMode.getMaxInitWaitTimeSeconds());
63+
this.logger.warn(4200, formattableLogMessage);
64+
completeRunningTask(Result.error(formattableLogMessage, cachedEntry.get()));
6565
} finally {
6666
lock.unlock();
6767
}

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.3.0";
10+
static final String VERSION = "9.4.0";
1111

1212
static final String SDK_KEY_PROXY_PREFIX = "configcat-proxy/";
1313
static final String SDK_KEY_PREFIX = "configcat-sdk-1";

src/main/java/com/configcat/EvaluationDetails.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class EvaluationDetails<T> {
99
private final String variationId;
1010
private final User user;
1111
private final boolean isDefaultValue;
12-
private final String error;
12+
private final Object error;
1313
private final long fetchTimeUnixMilliseconds;
1414
private final TargetingRule matchedTargetingRule;
1515
private final PercentageOption matchedPercentageOption;
@@ -19,7 +19,7 @@ public EvaluationDetails(T value,
1919
String variationId,
2020
User user,
2121
boolean isDefaultValue,
22-
String error,
22+
Object error,
2323
long fetchTimeUnixMilliseconds,
2424
TargetingRule matchedTargetingRule,
2525
PercentageOption matchedPercentageOption) {
@@ -34,7 +34,7 @@ public EvaluationDetails(T value,
3434
this.matchedPercentageOption = matchedPercentageOption;
3535
}
3636

37-
static <T> EvaluationDetails<T> fromError(String key, T defaultValue, String error, User user) {
37+
static <T> EvaluationDetails<T> fromError(String key, T defaultValue, Object error, User user) {
3838
return new EvaluationDetails<>(defaultValue, key, "", user, true, error, Constants.DISTANT_PAST, null, null);
3939
}
4040

@@ -81,7 +81,10 @@ public boolean isDefaultValue() {
8181
* In case of an error, this field contains the error message.
8282
*/
8383
public String getError() {
84-
return error;
84+
if(error != null) {
85+
return error.toString();
86+
}
87+
return null;
8588
}
8689

8790
/**

src/main/java/com/configcat/FetchResponse.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public enum Status {
99

1010
private final Status status;
1111
private final Entry entry;
12-
private final String error;
12+
private final Object error;
1313
private final boolean fetchTimeUpdatable;
1414

1515
public boolean isFetched() {
@@ -32,11 +32,11 @@ public Entry entry() {
3232
return this.entry;
3333
}
3434

35-
public String error() {
36-
return this.error;
35+
public Object error() {
36+
return error;
3737
}
3838

39-
FetchResponse(Status status, Entry entry, String error, boolean fetchTimeUpdatable) {
39+
FetchResponse(Status status, Entry entry, Object error, boolean fetchTimeUpdatable) {
4040
this.status = status;
4141
this.entry = entry;
4242
this.error = error;
@@ -51,7 +51,7 @@ public static FetchResponse notModified() {
5151
return new FetchResponse(Status.NOT_MODIFIED, Entry.EMPTY, null, true);
5252
}
5353

54-
public static FetchResponse failed(String error, boolean fetchTimeUpdatable) {
54+
public static FetchResponse failed(Object error, boolean fetchTimeUpdatable) {
5555
return new FetchResponse(Status.FAILED, Entry.EMPTY, error, fetchTimeUpdatable);
5656
}
5757
}

0 commit comments

Comments
 (0)