Skip to content

Commit 9ab8c11

Browse files
authored
Replaced log4j dependency with slf4j-api (#314)
* Replaced log4j dependency with slf4j-api * chore: removed stacked error log from internal modules
1 parent 1f2c816 commit 9ab8c11

21 files changed

+146
-199
lines changed

pom.xml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
<!-- utils -->
6161
<commons-lang3.version>3.17.0</commons-lang3.version>
6262
<commons-net.version>3.11.1</commons-net.version>
63-
<log4j.version>2.24.1</log4j.version>
63+
<slf4j-api.version>2.0.16</slf4j-api.version>
6464

6565
<!-- test -->
6666
<okhttp.version>5.0.0-alpha.14</okhttp.version>
@@ -124,9 +124,9 @@
124124
<version>${commons-net.version}</version>
125125
</dependency>
126126
<dependency>
127-
<groupId>org.apache.logging.log4j</groupId>
128-
<artifactId>log4j-core</artifactId>
129-
<version>${log4j.version}</version>
127+
<groupId>org.slf4j</groupId>
128+
<artifactId>slf4j-api</artifactId>
129+
<version>${slf4j-api.version}</version>
130130
</dependency>
131131

132132
<!-- test -->
@@ -160,6 +160,12 @@
160160
<version>${junit-pioneer.version}</version>
161161
<scope>test</scope>
162162
</dependency>
163+
<dependency>
164+
<groupId>org.slf4j</groupId>
165+
<artifactId>slf4j-simple</artifactId>
166+
<version>${slf4j-api.version}</version>
167+
<scope>test</scope>
168+
</dependency>
163169
</dependencies>
164170

165171
<dependencyManagement>
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.github.switcherapi.client;
22

33
import com.github.switcherapi.client.utils.SwitcherUtils;
4-
import org.apache.logging.log4j.LogManager;
4+
import org.slf4j.LoggerFactory;
55

66
public interface SnapshotCallback {
77

@@ -11,7 +11,7 @@ public interface SnapshotCallback {
1111
* @param version of the new snapshot
1212
*/
1313
default void onSnapshotUpdate(long version) {
14-
SwitcherUtils.debug(LogManager.getLogger(SnapshotCallback.class), "Snapshot updated: {}", version);
14+
SwitcherUtils.debug(LoggerFactory.getLogger(SnapshotCallback.class), "Snapshot updated: {}", version);
1515
}
1616

1717
/**
@@ -20,6 +20,6 @@ default void onSnapshotUpdate(long version) {
2020
* @param e Exception
2121
*/
2222
default void onSnapshotUpdateError(Exception e) {
23-
SwitcherUtils.debug(LogManager.getLogger(SnapshotCallback.class), "Failed to update snapshot: {}", e.getMessage());
23+
SwitcherUtils.debug(LoggerFactory.getLogger(SnapshotCallback.class), "Failed to update snapshot: {}", e.getMessage());
2424
}
2525
}

src/main/java/com/github/switcherapi/client/SwitcherContextBase.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
import com.github.switcherapi.client.utils.SnapshotWatcher;
1414
import com.github.switcherapi.client.utils.SwitcherUtils;
1515
import org.apache.commons.lang3.StringUtils;
16-
import org.apache.logging.log4j.LogManager;
17-
import org.apache.logging.log4j.Logger;
16+
import org.slf4j.Logger;
17+
import org.slf4j.LoggerFactory;
1818

1919
import java.io.IOException;
2020
import java.io.InputStream;
@@ -62,7 +62,7 @@
6262
*/
6363
public abstract class SwitcherContextBase {
6464

65-
protected static final Logger logger = LogManager.getLogger(SwitcherContextBase.class);
65+
protected static final Logger logger = LoggerFactory.getLogger(SwitcherContextBase.class);
6666

6767
protected static SwitcherProperties switcherProperties;
6868
protected static Set<String> switcherKeys;
@@ -189,7 +189,7 @@ public static boolean scheduleSnapshotAutoUpdate(String intervalValue, SnapshotC
189189
callbackFinal.onSnapshotUpdate(instance.getSnapshotVersion());
190190
}
191191
} catch (Exception e) {
192-
logger.error(e.getMessage());
192+
logger.error(e.getMessage(), e);
193193
callbackFinal.onSnapshotUpdateError(e);
194194
}
195195
};

src/main/java/com/github/switcherapi/client/SwitcherExecutor.java

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
import com.github.switcherapi.client.utils.SwitcherUtils;
1313
import com.google.gson.Gson;
1414
import org.apache.commons.lang3.StringUtils;
15-
import org.apache.logging.log4j.LogManager;
16-
import org.apache.logging.log4j.Logger;
15+
import org.slf4j.Logger;
16+
import org.slf4j.LoggerFactory;
1717

1818
import java.util.HashMap;
1919
import java.util.Map;
@@ -28,7 +28,7 @@
2828
*/
2929
public abstract class SwitcherExecutor {
3030

31-
private static final Logger logger = LogManager.getLogger(SwitcherExecutor.class);
31+
private static final Logger logger = LoggerFactory.getLogger(SwitcherExecutor.class);
3232

3333
private static final Map<String, CriteriaResponse> bypass = new HashMap<>();
3434

@@ -73,23 +73,19 @@ protected boolean checkSnapshotVersion(ClientRemote clientRemote, final Domain d
7373
return clientRemote.checkSnapshotVersion(domain.getVersion());
7474
}
7575

76-
protected Domain initializeSnapshotFromAPI(ClientRemote clientRemote) {
76+
protected Domain initializeSnapshotFromAPI(ClientRemote clientRemote)
77+
throws SwitcherRemoteException, SwitcherSnapshotWriteException {
7778
final String environment = SwitcherContextBase.contextStr(ContextKey.ENVIRONMENT);
7879
SwitcherUtils.debug(logger, "initializing snapshot from API - environment: {}", environment);
79-
80-
try {
81-
final Snapshot snapshot = clientRemote.resolveSnapshot();
82-
final String snapshotLocation = SwitcherContextBase.contextStr(ContextKey.SNAPSHOT_LOCATION);
8380

84-
if (snapshotLocation != null) {
85-
SnapshotLoader.saveSnapshot(snapshot, snapshotLocation, environment);
86-
}
87-
88-
return snapshot.getDomain();
89-
} catch (SwitcherRemoteException | SwitcherSnapshotWriteException e) {
90-
logger.error(e);
91-
throw e;
81+
final Snapshot snapshot = clientRemote.resolveSnapshot();
82+
final String snapshotLocation = SwitcherContextBase.contextStr(ContextKey.SNAPSHOT_LOCATION);
83+
84+
if (snapshotLocation != null) {
85+
SnapshotLoader.saveSnapshot(snapshot, snapshotLocation, environment);
9286
}
87+
88+
return snapshot.getDomain();
9389
}
9490

9591
public boolean isLocalEnabled() {

src/main/java/com/github/switcherapi/client/model/AsyncSwitcher.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.github.switcherapi.client.model;
22

33
import com.github.switcherapi.client.utils.SwitcherUtils;
4-
import org.apache.logging.log4j.LogManager;
5-
import org.apache.logging.log4j.Logger;
64

75
import com.github.switcherapi.client.exception.SwitcherException;
86
import com.github.switcherapi.client.model.response.CriteriaResponse;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
99

1010
import java.util.concurrent.ExecutorService;
1111
import java.util.concurrent.Executors;
@@ -20,7 +20,7 @@
2020
*/
2121
public class AsyncSwitcher implements Runnable {
2222

23-
private static final Logger logger = LogManager.getLogger(AsyncSwitcher.class);
23+
private static final Logger logger = LoggerFactory.getLogger(AsyncSwitcher.class);
2424

2525
private final ExecutorService executorService;
2626

@@ -62,7 +62,7 @@ public void run() {
6262

6363
switcherInterface.getHistoryExecution().add(response);
6464
} catch (SwitcherException e) {
65-
logger.error(e);
65+
logger.error(e.getMessage(), e);
6666
}
6767
}
6868

src/main/java/com/github/switcherapi/client/remote/ClientWSImpl.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,11 @@
1010
import com.github.switcherapi.client.model.response.AuthResponse;
1111
import com.github.switcherapi.client.model.response.CriteriaResponse;
1212
import com.github.switcherapi.client.model.response.SnapshotVersionResponse;
13-
import com.github.switcherapi.client.utils.SwitcherUtils;
1413
import jakarta.ws.rs.client.Client;
1514
import jakarta.ws.rs.client.Entity;
1615
import jakarta.ws.rs.client.WebTarget;
1716
import jakarta.ws.rs.core.MediaType;
1817
import jakarta.ws.rs.core.Response;
19-
import org.apache.logging.log4j.LogManager;
20-
import org.apache.logging.log4j.Logger;
2118

2219
import java.util.Optional;
2320
import java.util.Set;
@@ -29,9 +26,7 @@
2926
*/
3027
public class ClientWSImpl implements ClientWS {
3128

32-
private static final Logger logger = LogManager.getLogger(ClientWSImpl.class);
33-
34-
public static final String QUERY =
29+
public static final String QUERY =
3530
"{\"query\":\"{ domain(name: \\\"%s\\\", environment: \\\"%s\\\", _component: \\\"%s\\\") { " +
3631
"name version description activated " +
3732
"group { name description activated " +
@@ -51,8 +46,6 @@ public ClientWSImpl() {
5146

5247
@Override
5348
public CriteriaResponse executeCriteriaService(final Switcher switcher, final String token) {
54-
SwitcherUtils.debug(logger, "switcher: {}", switcher);
55-
5649
final String url = SwitcherContextBase.contextStr(ContextKey.URL);
5750
final WebTarget myResource = client.target(String.format(CRITERIA_URL, url))
5851
.queryParam(Switcher.KEY, switcher.getSwitcherKey())

src/main/java/com/github/switcherapi/client/service/local/ClientLocalService.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
import com.github.switcherapi.client.service.ValidatorService;
1313
import com.github.switcherapi.client.utils.SwitcherUtils;
1414
import org.apache.commons.lang3.ArrayUtils;
15-
import org.apache.logging.log4j.LogManager;
16-
import org.apache.logging.log4j.Logger;
15+
import org.slf4j.Logger;
16+
import org.slf4j.LoggerFactory;
1717

1818
import java.util.ArrayList;
1919
import java.util.Arrays;
@@ -29,7 +29,7 @@
2929
*/
3030
public class ClientLocalService {
3131

32-
private static final Logger logger = LogManager.getLogger(ClientLocalService.class);
32+
private static final Logger logger = LoggerFactory.getLogger(ClientLocalService.class);
3333

3434
public static final String DISABLED_DOMAIN = "Domain disabled";
3535
public static final String DISABLED_GROUP = "Group disabled";
@@ -38,6 +38,8 @@ public class ClientLocalService {
3838
private static final String STRATEGY_FAIL_PATTERN = "Strategy %s does not agree";
3939
private static final String STRATEGY_FAIL_NO_INPUT_PATTERN = "Strategy %s did not receive any input";
4040

41+
private static final String LOG_PROCESS_OP_TEMPLATE = "processOperation: configStrategies: %s";
42+
4143
private final ValidatorService validatorService;
4244

4345
public ClientLocalService() {
@@ -117,8 +119,7 @@ private Config findConfigInGroup(final Group group, final String switcherKey) {
117119
*/
118120
private CriteriaResponse processOperation(final Strategy[] configStrategies, final List<Entry> input,
119121
final Switcher switcher) {
120-
SwitcherUtils.debugSupplier(logger, "configStrategies: {}", Arrays.toString(configStrategies));
121-
SwitcherUtils.debugSupplier(logger, "input: {}", Arrays.toString(input != null ? input.toArray() : ArrayUtils.EMPTY_STRING_ARRAY));
122+
SwitcherUtils.debug(logger, LOG_PROCESS_OP_TEMPLATE, Arrays.toString(configStrategies));
122123

123124
for (final Strategy strategy : configStrategies) {
124125
if (!strategy.isActivated()) {

src/main/java/com/github/switcherapi/client/service/local/SwitcherLocalService.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
import com.github.switcherapi.client.utils.SnapshotLoader;
1414
import com.github.switcherapi.client.utils.SwitcherUtils;
1515
import org.apache.commons.lang3.StringUtils;
16-
import org.apache.logging.log4j.LogManager;
17-
import org.apache.logging.log4j.Logger;
16+
import org.slf4j.Logger;
17+
import org.slf4j.LoggerFactory;
1818

1919
import java.io.IOException;
2020
import java.util.List;
@@ -26,7 +26,7 @@
2626
*/
2727
public class SwitcherLocalService extends SwitcherExecutor {
2828

29-
private static final Logger logger = LogManager.getLogger(SwitcherLocalService.class);
29+
private static final Logger logger = LoggerFactory.getLogger(SwitcherLocalService.class);
3030

3131
private final ClientRemote clientRemote;
3232

@@ -84,7 +84,7 @@ public boolean notifyChange(final String snapshotFile, SnapshotEventHandler hand
8484
}
8585
} catch (SwitcherSnapshotLoadException | IOException e) {
8686
handler.onError(new SwitcherException(e.getMessage(), e));
87-
logger.error(e);
87+
logger.error(e.getMessage(), e);
8888
return false;
8989
}
9090

@@ -103,7 +103,7 @@ public boolean notifyChange(final String snapshotFile) {
103103

104104
@Override
105105
public CriteriaResponse executeCriteria(final Switcher switcher) {
106-
SwitcherUtils.debug(logger, "switcher: {}", switcher);
106+
SwitcherUtils.debug(logger, "[Local] request: {}", switcher);
107107

108108
CriteriaResponse response;
109109
try {

src/main/java/com/github/switcherapi/client/service/remote/SwitcherRemoteService.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
import com.github.switcherapi.client.service.local.SwitcherLocalService;
1212
import com.github.switcherapi.client.utils.SwitcherUtils;
1313
import org.apache.commons.lang3.StringUtils;
14-
import org.apache.logging.log4j.LogManager;
15-
import org.apache.logging.log4j.Logger;
14+
import org.slf4j.Logger;
15+
import org.slf4j.LoggerFactory;
1616

1717
import java.util.Arrays;
1818
import java.util.Set;
@@ -23,7 +23,7 @@
2323
*/
2424
public class SwitcherRemoteService extends SwitcherExecutor {
2525

26-
private static final Logger logger = LogManager.getLogger(SwitcherRemoteService.class);
26+
private static final Logger logger = LoggerFactory.getLogger(SwitcherRemoteService.class);
2727

2828
private final SwitcherLocalService switcherLocal;
2929

@@ -36,15 +36,15 @@ public SwitcherRemoteService() {
3636

3737
@Override
3838
public CriteriaResponse executeCriteria(final Switcher switcher) {
39-
SwitcherUtils.debug(logger, "switcher: {}", switcher);
39+
SwitcherUtils.debug(logger, "[Remote] request:: {}", switcher);
4040

4141
try {
4242
final CriteriaResponse response = this.clientRemote.executeCriteria(switcher);
4343
SwitcherUtils.debug(logger, "[Remote] response: {}", response);
4444

4545
return response;
4646
} catch (final SwitcherRemoteException e) {
47-
logger.error("Failed to execute criteria - {}\nCause: {}", e.getMessage(), e.getCause());
47+
logger.error("Failed to execute criteria - Cause: {}", e.getMessage(), e.getCause());
4848
return tryExecuteLocalCriteria(switcher, e);
4949
}
5050
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.github.switcherapi.client.service.validators;
2+
3+
import com.github.switcherapi.client.utils.SwitcherUtils;
4+
import org.apache.commons.lang3.RegExUtils;
5+
import org.apache.commons.lang3.StringUtils;
6+
7+
public abstract class DateTimeValidator extends Validator {
8+
9+
private static final String LOG_DATE = "date: {}";
10+
private static final String LOG_DATE_TIME = "date time: {} {}";
11+
private static final String FULL_DATE_REGEX = "([12]\\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\\d|3[01]))";
12+
13+
private static final String DATE_FORMAT = "%s 00:00:00";
14+
private static final String HOUR_FORMAT = "%s %s:00:00";
15+
private static final String MINUTE_FORMAT = "%s %s:00";
16+
private static final String TIME_SPLIT = ":";
17+
private static final String DATE_SPLIT = " ";
18+
19+
protected String getFullDate(final String date) {
20+
SwitcherUtils.debug(logger, LOG_DATE, date);
21+
22+
final String time = RegExUtils.removePattern(date, FULL_DATE_REGEX).trim();
23+
return getFullTime(date, time);
24+
}
25+
26+
protected String getFullTime(final String date, final String time) {
27+
SwitcherUtils.debug(logger, LOG_DATE_TIME, date, time);
28+
29+
if (StringUtils.isBlank(time)) {
30+
return String.format(DATE_FORMAT, date);
31+
}
32+
33+
String[] timeSplit = time.split(TIME_SPLIT);
34+
if (timeSplit.length == 1) {
35+
return String.format(HOUR_FORMAT, date.split(DATE_SPLIT)[0], time);
36+
}
37+
38+
if (timeSplit.length == 2) {
39+
return String.format(MINUTE_FORMAT, date.split(DATE_SPLIT)[0], time);
40+
}
41+
42+
return date;
43+
}
44+
45+
}

0 commit comments

Comments
 (0)