From b160a5196cc744af6d3933dd19393f1af4cb29e9 Mon Sep 17 00:00:00 2001 From: Roger Floriano <31597636+petruki@users.noreply.github.com> Date: Tue, 14 Nov 2023 17:21:01 -0800 Subject: [PATCH 1/3] Improved debug logging - removed unnecessary condition (#261) * Bump to 2.0.6-SNAPSHOT * Improved debug logging - removed unnecessary condition * Updated sonar scan to use Java 17 * Upadated sonar scan to use sonar.token --- .../switcherapi/client/SnapshotCallback.java | 8 ++---- .../client/SwitcherContextBase.java | 3 +-- .../switcherapi/client/SwitcherExecutor.java | 12 +++------ .../client/model/AsyncSwitcher.java | 8 ++---- .../client/remote/ClientWSImpl.java | 4 +-- .../service/local/ClientLocalService.java | 8 ++---- .../service/local/SwitcherLocalService.java | 15 +++-------- .../service/remote/SwitcherRemoteService.java | 17 +++--------- .../client/service/validators/Validator.java | 12 +++------ .../client/utils/SnapshotEventHandler.java | 4 +-- .../client/utils/SwitcherUtils.java | 26 +++++++------------ 11 files changed, 32 insertions(+), 85 deletions(-) diff --git a/src/main/java/com/github/switcherapi/client/SnapshotCallback.java b/src/main/java/com/github/switcherapi/client/SnapshotCallback.java index f19cf867..8e9cd414 100644 --- a/src/main/java/com/github/switcherapi/client/SnapshotCallback.java +++ b/src/main/java/com/github/switcherapi/client/SnapshotCallback.java @@ -13,9 +13,7 @@ public class SnapshotCallback { * @param version of the new snapshot */ public void onSnapshotUpdate(long version) { - if (logger.isDebugEnabled()) { - logger.debug(String.format("Snapshot updated: %s", version)); - } + logger.debug("Snapshot updated: {}", version); } /** @@ -24,8 +22,6 @@ public void onSnapshotUpdate(long version) { * @param e Exception */ public void onSnapshotUpdateError(Exception e) { - if (logger.isDebugEnabled()) { - logger.debug(String.format("Failed to update snapshot: %s", e.getMessage())); - } + logger.debug("Failed to update snapshot: {}", e.getMessage()); } } diff --git a/src/main/java/com/github/switcherapi/client/SwitcherContextBase.java b/src/main/java/com/github/switcherapi/client/SwitcherContextBase.java index 88f4a28b..65905b19 100644 --- a/src/main/java/com/github/switcherapi/client/SwitcherContextBase.java +++ b/src/main/java/com/github/switcherapi/client/SwitcherContextBase.java @@ -212,8 +212,7 @@ private static void initExecutorService() { * @throws SwitcherKeyNotFoundException in case the key was not properly loaded */ public static Switcher getSwitcher(String key, boolean keepEntries) { - if (logger.isDebugEnabled()) - logger.debug(String.format("key: %s - keepEntries: %s", key, keepEntries)); + logger.debug("key: {} - keepEntries: {}", key, keepEntries); if (!switchers.containsKey(key)) throw new SwitcherKeyNotFoundException(key); diff --git a/src/main/java/com/github/switcherapi/client/SwitcherExecutor.java b/src/main/java/com/github/switcherapi/client/SwitcherExecutor.java index 33c16d47..1c1c3f9d 100644 --- a/src/main/java/com/github/switcherapi/client/SwitcherExecutor.java +++ b/src/main/java/com/github/switcherapi/client/SwitcherExecutor.java @@ -76,21 +76,15 @@ public abstract class SwitcherExecutor { protected boolean checkSnapshotVersion(ClientRemote clientRemote, final Domain domain) { final String environment = SwitcherContextBase.contextStr(ContextKey.ENVIRONMENT); - - if (logger.isDebugEnabled()) { - logger.debug(String.format("verifying snapshot version - environment: %s", environment)); - } + logger.debug("verifying snapshot version - environment: {}", environment); return clientRemote.checkSnapshotVersion(domain.getVersion()); } protected Domain initializeSnapshotFromAPI(ClientRemote clientRemote) { final String environment = SwitcherContextBase.contextStr(ContextKey.ENVIRONMENT); + logger.debug("initializing snapshot from API - environment: {}", environment); - if (logger.isDebugEnabled()) { - logger.debug(String.format("initializing snapshot from API - environment: %s", environment)); - } - try { final Snapshot snapshot = clientRemote.resolveSnapshot(); final String snapshotLocation = SwitcherContextBase.contextStr(ContextKey.SNAPSHOT_LOCATION); @@ -98,7 +92,7 @@ protected Domain initializeSnapshotFromAPI(ClientRemote clientRemote) { if (snapshotLocation != null) { SnapshotLoader.saveSnapshot(snapshot, snapshotLocation, environment); } - + return snapshot.getDomain(); } catch (SwitcherRemoteException | SwitcherSnapshotWriteException e) { logger.error(e); diff --git a/src/main/java/com/github/switcherapi/client/model/AsyncSwitcher.java b/src/main/java/com/github/switcherapi/client/model/AsyncSwitcher.java index c03f0d14..e1790b29 100644 --- a/src/main/java/com/github/switcherapi/client/model/AsyncSwitcher.java +++ b/src/main/java/com/github/switcherapi/client/model/AsyncSwitcher.java @@ -30,14 +30,10 @@ public class AsyncSwitcher implements Runnable { */ public synchronized void execute(final Switcher switcher) { this.switcher = switcher; - - if (logger.isDebugEnabled()) - logger.debug(String.format("nextRun: %s - currentTimeMillis: %s", nextRun, - System.currentTimeMillis())); + logger.debug("nextRun: {} - currentTimeMillis: {}", nextRun, System.currentTimeMillis()); if (nextRun < System.currentTimeMillis()) { - if (logger.isDebugEnabled()) - logger.debug("Running AsyncSwitcher"); + logger.debug("Running AsyncSwitcher"); this.nextRun = System.currentTimeMillis() + switcher.delay; new Thread(this, AsyncSwitcher.class.getName()).start(); diff --git a/src/main/java/com/github/switcherapi/client/remote/ClientWSImpl.java b/src/main/java/com/github/switcherapi/client/remote/ClientWSImpl.java index b2d44745..ce677f52 100644 --- a/src/main/java/com/github/switcherapi/client/remote/ClientWSImpl.java +++ b/src/main/java/com/github/switcherapi/client/remote/ClientWSImpl.java @@ -50,9 +50,7 @@ public ClientWSImpl() { @Override public CriteriaResponse executeCriteriaService(final Switcher switcher, final String token) { - if (logger.isDebugEnabled()) { - logger.debug(String.format("switcher: %s", switcher)); - } + logger.debug("switcher: {}", switcher); final String url = SwitcherContextBase.contextStr(ContextKey.URL); final WebTarget myResource = client.target(String.format(CRITERIA_URL, url)) diff --git a/src/main/java/com/github/switcherapi/client/service/local/ClientLocalService.java b/src/main/java/com/github/switcherapi/client/service/local/ClientLocalService.java index 9dad99be..23fbe031 100644 --- a/src/main/java/com/github/switcherapi/client/service/local/ClientLocalService.java +++ b/src/main/java/com/github/switcherapi/client/service/local/ClientLocalService.java @@ -131,12 +131,8 @@ public CriteriaResponse executeCriteria(final Switcher switcher, final Domain do */ private CriteriaResponse processOperation(final Strategy[] configStrategies, final List input, final Switcher switcher) { - - if (logger.isDebugEnabled()) { - logger.debug(String.format("configStrategies: %s", Arrays.toString(configStrategies))); - logger.debug(String.format("input: %s", - Arrays.toString(input != null ? input.toArray() : ArrayUtils.EMPTY_STRING_ARRAY))); - } + logger.debug("configStrategies: {}", () -> Arrays.toString(configStrategies)); + logger.debug("input: {}", () -> Arrays.toString(input != null ? input.toArray() : ArrayUtils.EMPTY_STRING_ARRAY)); boolean result; for (final Strategy strategy : configStrategies) { diff --git a/src/main/java/com/github/switcherapi/client/service/local/SwitcherLocalService.java b/src/main/java/com/github/switcherapi/client/service/local/SwitcherLocalService.java index 5437c766..ed5d7865 100644 --- a/src/main/java/com/github/switcherapi/client/service/local/SwitcherLocalService.java +++ b/src/main/java/com/github/switcherapi/client/service/local/SwitcherLocalService.java @@ -63,20 +63,13 @@ public void init() { @Override public CriteriaResponse executeCriteria(final Switcher switcher) { - if (logger.isDebugEnabled()) { - logger.debug(String.format("switcher: %s", switcher)); - } - + logger.debug("switcher: {}", switcher); final CriteriaResponse response = ClientLocalService.getInstance().executeCriteria(switcher, this.domain); - - if (logger.isDebugEnabled()) { - logger.debug(String.format("[Offline] response: %s", response)); - } + logger.debug("[Offline] response: {}", response); return response; } - @Override public boolean checkSnapshotVersion() { return super.checkSnapshotVersion(this.clientRemote, this.domain); @@ -89,9 +82,7 @@ public void updateSnapshot() { @Override public void checkSwitchers(final Set switchers) { - if (logger.isDebugEnabled()) { - logger.debug(String.format("switchers: %s", switchers)); - } + logger.debug("switchers: {}", switchers); final List response = ClientLocalService.getInstance().checkSwitchers(switchers, this.domain); if (!response.isEmpty()) { diff --git a/src/main/java/com/github/switcherapi/client/service/remote/SwitcherRemoteService.java b/src/main/java/com/github/switcherapi/client/service/remote/SwitcherRemoteService.java index f180fcd3..630fbdce 100644 --- a/src/main/java/com/github/switcherapi/client/service/remote/SwitcherRemoteService.java +++ b/src/main/java/com/github/switcherapi/client/service/remote/SwitcherRemoteService.java @@ -37,16 +37,11 @@ public SwitcherRemoteService() { @Override public CriteriaResponse executeCriteria(final Switcher switcher) { - if (logger.isDebugEnabled()) { - logger.debug(String.format("switcher: %s", switcher)); - } + logger.debug("switcher: {}", switcher); try { final CriteriaResponse response = this.clientRemote.executeCriteria(switcher); - - if (logger.isDebugEnabled()) { - logger.debug(String.format("[Online] response: %s", response)); - } + logger.debug("[Online] response: {}", response); return response; } catch (final SwitcherRemoteException e) { @@ -59,9 +54,7 @@ private CriteriaResponse executeSilentCriteria(final Switcher switcher, final SwitcherRemoteException e) { if (StringUtils.isNotBlank(SwitcherContextBase.contextStr(ContextKey.SILENT_MODE))) { CriteriaResponse response = this.switcherOffline.executeCriteria(switcher); - if (logger.isDebugEnabled()) { - logger.debug(String.format("[Silent] response: %s", response)); - } + logger.debug("[Silent] response: {}", response); return response; } else { @@ -87,9 +80,7 @@ public void updateSnapshot() { @Override public void checkSwitchers(final Set switchers) { - if (logger.isDebugEnabled()) { - logger.debug(String.format("switchers: %s", switchers)); - } + logger.debug("switchers: {}", switchers); final SwitchersCheck response = this.clientRemote.checkSwitchers(switchers); if (response.getNotFound() != null && response.getNotFound().length > 0) { diff --git a/src/main/java/com/github/switcherapi/client/service/validators/Validator.java b/src/main/java/com/github/switcherapi/client/service/validators/Validator.java index 77409fbe..e1cee986 100644 --- a/src/main/java/com/github/switcherapi/client/service/validators/Validator.java +++ b/src/main/java/com/github/switcherapi/client/service/validators/Validator.java @@ -11,17 +11,13 @@ public abstract class Validator { protected static final Logger logger = LogManager.getLogger(Validator.class); - public static final String DEBUG_SWITCHER_INPUT = "switcherInput: %s"; - public static final String DEBUG_STRATEGY = "strategy: %s"; + public static final String DEBUG_SWITCHER_INPUT = "switcherInput: {}"; + public static final String DEBUG_STRATEGY = "strategy: {}"; public boolean execute(final Strategy strategy, final Entry switcherInput) throws SwitcherException { - - if (logger.isDebugEnabled()) { - logger.debug(String.format(DEBUG_STRATEGY, strategy)); - logger.debug(String.format(DEBUG_SWITCHER_INPUT, switcherInput)); - } - + logger.debug(DEBUG_STRATEGY, strategy); + logger.debug(DEBUG_SWITCHER_INPUT, switcherInput); return process(strategy, switcherInput); } diff --git a/src/main/java/com/github/switcherapi/client/utils/SnapshotEventHandler.java b/src/main/java/com/github/switcherapi/client/utils/SnapshotEventHandler.java index 13c0d306..6be834b5 100644 --- a/src/main/java/com/github/switcherapi/client/utils/SnapshotEventHandler.java +++ b/src/main/java/com/github/switcherapi/client/utils/SnapshotEventHandler.java @@ -17,9 +17,7 @@ public class SnapshotEventHandler { private static final Logger logger = LogManager.getLogger(SnapshotEventHandler.class); public void onSuccess() { - if (logger.isDebugEnabled()) { - logger.debug("Snapshot has been changed"); - } + logger.debug("Snapshot has been changed"); } public void onError(SwitcherException exception) { diff --git a/src/main/java/com/github/switcherapi/client/utils/SwitcherUtils.java b/src/main/java/com/github/switcherapi/client/utils/SwitcherUtils.java index 5e476c77..d33b9c86 100644 --- a/src/main/java/com/github/switcherapi/client/utils/SwitcherUtils.java +++ b/src/main/java/com/github/switcherapi/client/utils/SwitcherUtils.java @@ -32,11 +32,11 @@ public class SwitcherUtils { private static final Logger logger = LogManager.getLogger(SwitcherUtils.class); - private static final String LOG_DATE = "date: %s"; + private static final String LOG_DATE = "date: {}"; - private static final String LOG_TME = "time: %s"; + private static final String LOG_TME = "time: {}"; - private static final String LOG_ADDVALUE = "addValue: %s"; + private static final String LOG_ADDVALUE = "addValue: {}"; /** * [0] = s (seconds) [1] = m (minutes) [2] = h (hours) [3] = d (days) @@ -59,10 +59,8 @@ private SwitcherUtils() {} public static Date addTimeDuration(final String addValue, final Date date) throws SwitcherInvalidDateTimeArgumentException { - if (logger.isDebugEnabled()) { - logger.debug(String.format(LOG_ADDVALUE, addValue)); - logger.debug(String.format(LOG_DATE, date)); - } + logger.debug(LOG_ADDVALUE, addValue); + logger.debug(LOG_DATE, date); if (addValue.endsWith(DURATION[0])) { return DateUtils.addSeconds(date, Integer.parseInt(addValue.replace(DURATION[0], StringUtils.EMPTY))); @@ -78,9 +76,7 @@ public static Date addTimeDuration(final String addValue, final Date date) } public static long getMillis(final String time) { - if (logger.isDebugEnabled()) { - logger.debug(String.format(LOG_TME, time)); - } + logger.debug(LOG_TME, time); if (time.endsWith(DURATION[0])) { return (long) (Double.parseDouble(time.replace(DURATION[0], StringUtils.EMPTY)) * 1000L); @@ -92,19 +88,15 @@ public static long getMillis(final String time) { } public static String getFullDate(final String date) { - if (logger.isDebugEnabled()) { - logger.debug(String.format(LOG_DATE, date)); - } + logger.debug(LOG_DATE, date); final String time = RegExUtils.removePattern(date, FULL_DATE_REGEX).trim(); return getFullTime(date, time); } public static String getFullTime(final String date, final String time) { - if (logger.isDebugEnabled()) { - logger.debug(String.format(LOG_DATE, date)); - logger.debug(String.format(LOG_TME, time)); - } + logger.debug(LOG_DATE, date); + logger.debug(LOG_TME, time); if (StringUtils.isBlank(time)) { return String.format("%s 00:00:00", date); From edd1dbcc2af98d959ea154137b18003fa490c1f9 Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Tue, 14 Nov 2023 17:27:46 -0800 Subject: [PATCH 2/3] Bump to 1.4.5-SNAPSHOT --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index c231672a..a0bc9fab 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ com.github.switcherapi switcher-client jar - 1.4.4 + 1.4.5-SNAPSHOT Switcher Client Switcher Client SDK for working with Switcher API From dd16f989d585f8b7cd27b21c50b1c43719357af6 Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Tue, 14 Nov 2023 17:28:43 -0800 Subject: [PATCH 3/3] Upadated sonar scan to use Java 17 and sonar.token --- .github/workflows/master.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/master.yml b/.github/workflows/master.yml index da313230..20991318 100644 --- a/.github/workflows/master.yml +++ b/.github/workflows/master.yml @@ -17,15 +17,15 @@ jobs: with: fetch-depth: 0 - - name: Set up JDK 11 + - name: Set up JDK 17 uses: actions/setup-java@v3 with: - java-version: 11 + java-version: 17 distribution: 'temurin' cache: maven - name: Build/Test & SonarCloud Scan - run: mvn -B clean verify -Pcoverage,sonar -Dsonar.login=${{ secrets.SONAR_TOKEN }} + run: mvn -B clean verify -Pcoverage,sonar -Dsonar.token=${{ secrets.SONAR_TOKEN }} env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}