diff --git a/pom.xml b/pom.xml index 631d44ae..472bfdb1 100644 --- a/pom.xml +++ b/pom.xml @@ -59,7 +59,7 @@ 3.17.0 3.11.1 - 2.24.1 + 2.0.16 5.0.0-alpha.14 @@ -123,9 +123,9 @@ ${commons-net.version} - org.apache.logging.log4j - log4j-core - ${log4j.version} + org.slf4j + slf4j-api + ${slf4j-api.version} @@ -159,6 +159,12 @@ ${junit-pioneer.version} test + + org.slf4j + slf4j-simple + ${slf4j-api.version} + test + diff --git a/src/main/java/com/github/switcherapi/client/SnapshotCallback.java b/src/main/java/com/github/switcherapi/client/SnapshotCallback.java index 071f0e2f..f804fc32 100644 --- a/src/main/java/com/github/switcherapi/client/SnapshotCallback.java +++ b/src/main/java/com/github/switcherapi/client/SnapshotCallback.java @@ -1,7 +1,7 @@ package com.github.switcherapi.client; import com.github.switcherapi.client.utils.SwitcherUtils; -import org.apache.logging.log4j.LogManager; +import org.slf4j.LoggerFactory; public interface SnapshotCallback { @@ -11,7 +11,7 @@ public interface SnapshotCallback { * @param version of the new snapshot */ default void onSnapshotUpdate(long version) { - SwitcherUtils.debug(LogManager.getLogger(SnapshotCallback.class), "Snapshot updated: {}", version); + SwitcherUtils.debug(LoggerFactory.getLogger(SnapshotCallback.class), "Snapshot updated: {}", version); } /** @@ -20,6 +20,6 @@ default void onSnapshotUpdate(long version) { * @param e Exception */ default void onSnapshotUpdateError(Exception e) { - SwitcherUtils.debug(LogManager.getLogger(SnapshotCallback.class), "Failed to update snapshot: {}", e.getMessage()); + SwitcherUtils.debug(LoggerFactory.getLogger(SnapshotCallback.class), "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 834e7fa0..58c56b73 100644 --- a/src/main/java/com/github/switcherapi/client/SwitcherContextBase.java +++ b/src/main/java/com/github/switcherapi/client/SwitcherContextBase.java @@ -13,8 +13,8 @@ import com.github.switcherapi.client.utils.SnapshotWatcher; import com.github.switcherapi.client.utils.SwitcherUtils; import org.apache.commons.lang3.StringUtils; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.IOException; import java.io.InputStream; @@ -62,7 +62,7 @@ */ public abstract class SwitcherContextBase { - protected static final Logger logger = LogManager.getLogger(SwitcherContextBase.class); + protected static final Logger logger = LoggerFactory.getLogger(SwitcherContextBase.class); protected static SwitcherProperties switcherProperties; protected static Set switcherKeys; @@ -189,7 +189,7 @@ public static boolean scheduleSnapshotAutoUpdate(String intervalValue, SnapshotC callbackFinal.onSnapshotUpdate(instance.getSnapshotVersion()); } } catch (Exception e) { - logger.error(e.getMessage()); + logger.error(e.getMessage(), e); callbackFinal.onSnapshotUpdateError(e); } }; diff --git a/src/main/java/com/github/switcherapi/client/SwitcherExecutor.java b/src/main/java/com/github/switcherapi/client/SwitcherExecutor.java index d79d03fd..5c2d9197 100644 --- a/src/main/java/com/github/switcherapi/client/SwitcherExecutor.java +++ b/src/main/java/com/github/switcherapi/client/SwitcherExecutor.java @@ -12,8 +12,8 @@ import com.github.switcherapi.client.utils.SwitcherUtils; import com.google.gson.Gson; import org.apache.commons.lang3.StringUtils; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.HashMap; import java.util.Map; @@ -28,7 +28,7 @@ */ public abstract class SwitcherExecutor { - private static final Logger logger = LogManager.getLogger(SwitcherExecutor.class); + private static final Logger logger = LoggerFactory.getLogger(SwitcherExecutor.class); private static final Map bypass = new HashMap<>(); @@ -73,23 +73,19 @@ protected boolean checkSnapshotVersion(ClientRemote clientRemote, final Domain d return clientRemote.checkSnapshotVersion(domain.getVersion()); } - protected Domain initializeSnapshotFromAPI(ClientRemote clientRemote) { + protected Domain initializeSnapshotFromAPI(ClientRemote clientRemote) + throws SwitcherRemoteException, SwitcherSnapshotWriteException { final String environment = SwitcherContextBase.contextStr(ContextKey.ENVIRONMENT); SwitcherUtils.debug(logger, "initializing snapshot from API - environment: {}", environment); - - try { - final Snapshot snapshot = clientRemote.resolveSnapshot(); - final String snapshotLocation = SwitcherContextBase.contextStr(ContextKey.SNAPSHOT_LOCATION); - if (snapshotLocation != null) { - SnapshotLoader.saveSnapshot(snapshot, snapshotLocation, environment); - } - - return snapshot.getDomain(); - } catch (SwitcherRemoteException | SwitcherSnapshotWriteException e) { - logger.error(e); - throw e; + final Snapshot snapshot = clientRemote.resolveSnapshot(); + final String snapshotLocation = SwitcherContextBase.contextStr(ContextKey.SNAPSHOT_LOCATION); + + if (snapshotLocation != null) { + SnapshotLoader.saveSnapshot(snapshot, snapshotLocation, environment); } + + return snapshot.getDomain(); } public boolean isLocalEnabled() { 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 f31e6e49..441697ff 100644 --- a/src/main/java/com/github/switcherapi/client/model/AsyncSwitcher.java +++ b/src/main/java/com/github/switcherapi/client/model/AsyncSwitcher.java @@ -1,11 +1,11 @@ package com.github.switcherapi.client.model; import com.github.switcherapi.client.utils.SwitcherUtils; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; import com.github.switcherapi.client.exception.SwitcherException; import com.github.switcherapi.client.model.response.CriteriaResponse; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -20,7 +20,7 @@ */ public class AsyncSwitcher implements Runnable { - private static final Logger logger = LogManager.getLogger(AsyncSwitcher.class); + private static final Logger logger = LoggerFactory.getLogger(AsyncSwitcher.class); private final ExecutorService executorService; @@ -62,7 +62,7 @@ public void run() { switcherInterface.getHistoryExecution().add(response); } catch (SwitcherException e) { - logger.error(e); + logger.error(e.getMessage(), e); } } 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 eb7d72aa..eebee309 100644 --- a/src/main/java/com/github/switcherapi/client/remote/ClientWSImpl.java +++ b/src/main/java/com/github/switcherapi/client/remote/ClientWSImpl.java @@ -10,9 +10,6 @@ import com.github.switcherapi.client.model.response.AuthResponse; import com.github.switcherapi.client.model.response.CriteriaResponse; import com.github.switcherapi.client.model.response.SnapshotVersionResponse; -import com.github.switcherapi.client.utils.SwitcherUtils; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; import javax.ws.rs.client.Client; import javax.ws.rs.client.Entity; @@ -29,9 +26,7 @@ */ public class ClientWSImpl implements ClientWS { - private static final Logger logger = LogManager.getLogger(ClientWSImpl.class); - - public static final String QUERY = + public static final String QUERY = "{\"query\":\"{ domain(name: \\\"%s\\\", environment: \\\"%s\\\", _component: \\\"%s\\\") { " + "name version description activated " + "group { name description activated " + @@ -51,8 +46,6 @@ public ClientWSImpl() { @Override public CriteriaResponse executeCriteriaService(final Switcher switcher, final String token) { - SwitcherUtils.debug(logger, "switcher: {}", switcher); - final String url = SwitcherContextBase.contextStr(ContextKey.URL); final WebTarget myResource = client.target(String.format(CRITERIA_URL, url)) .queryParam(Switcher.KEY, switcher.getSwitcherKey()) 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 4cd7b7be..0baecc92 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 @@ -12,8 +12,8 @@ import com.github.switcherapi.client.service.ValidatorService; import com.github.switcherapi.client.utils.SwitcherUtils; import org.apache.commons.lang3.ArrayUtils; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.Arrays; @@ -29,7 +29,7 @@ */ public class ClientLocalService { - private static final Logger logger = LogManager.getLogger(ClientLocalService.class); + private static final Logger logger = LoggerFactory.getLogger(ClientLocalService.class); public static final String DISABLED_DOMAIN = "Domain disabled"; public static final String DISABLED_GROUP = "Group disabled"; @@ -38,6 +38,8 @@ public class ClientLocalService { private static final String STRATEGY_FAIL_PATTERN = "Strategy %s does not agree"; private static final String STRATEGY_FAIL_NO_INPUT_PATTERN = "Strategy %s did not receive any input"; + private static final String LOG_PROCESS_OP_TEMPLATE = "processOperation: configStrategies: %s"; + private final ValidatorService validatorService; public ClientLocalService() { @@ -117,8 +119,7 @@ private Config findConfigInGroup(final Group group, final String switcherKey) { */ private CriteriaResponse processOperation(final Strategy[] configStrategies, final List input, final Switcher switcher) { - SwitcherUtils.debugSupplier(logger, "configStrategies: {}", Arrays.toString(configStrategies)); - SwitcherUtils.debugSupplier(logger, "input: {}", Arrays.toString(input != null ? input.toArray() : ArrayUtils.EMPTY_STRING_ARRAY)); + SwitcherUtils.debug(logger, LOG_PROCESS_OP_TEMPLATE, Arrays.toString(configStrategies)); for (final Strategy strategy : configStrategies) { if (!strategy.isActivated()) { 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 580a1c1e..1a5fb25c 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 @@ -13,8 +13,8 @@ import com.github.switcherapi.client.utils.SnapshotLoader; import com.github.switcherapi.client.utils.SwitcherUtils; import org.apache.commons.lang3.StringUtils; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.IOException; import java.util.List; @@ -26,7 +26,7 @@ */ public class SwitcherLocalService extends SwitcherExecutor { - private static final Logger logger = LogManager.getLogger(SwitcherLocalService.class); + private static final Logger logger = LoggerFactory.getLogger(SwitcherLocalService.class); private final ClientRemote clientRemote; @@ -84,7 +84,7 @@ public boolean notifyChange(final String snapshotFile, SnapshotEventHandler hand } } catch (SwitcherSnapshotLoadException | IOException e) { handler.onError(new SwitcherException(e.getMessage(), e)); - logger.error(e); + logger.error(e.getMessage(), e); return false; } @@ -103,7 +103,7 @@ public boolean notifyChange(final String snapshotFile) { @Override public CriteriaResponse executeCriteria(final Switcher switcher) { - SwitcherUtils.debug(logger, "switcher: {}", switcher); + SwitcherUtils.debug(logger, "[Local] request: {}", switcher); CriteriaResponse response; try { 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 2e28845f..88d3f5a8 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 @@ -11,8 +11,8 @@ import com.github.switcherapi.client.service.local.SwitcherLocalService; import com.github.switcherapi.client.utils.SwitcherUtils; import org.apache.commons.lang3.StringUtils; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.Arrays; import java.util.Set; @@ -23,7 +23,7 @@ */ public class SwitcherRemoteService extends SwitcherExecutor { - private static final Logger logger = LogManager.getLogger(SwitcherRemoteService.class); + private static final Logger logger = LoggerFactory.getLogger(SwitcherRemoteService.class); private final SwitcherLocalService switcherLocal; @@ -36,7 +36,7 @@ public SwitcherRemoteService() { @Override public CriteriaResponse executeCriteria(final Switcher switcher) { - SwitcherUtils.debug(logger, "switcher: {}", switcher); + SwitcherUtils.debug(logger, "[Remote] request:: {}", switcher); try { final CriteriaResponse response = this.clientRemote.executeCriteria(switcher); @@ -44,7 +44,7 @@ public CriteriaResponse executeCriteria(final Switcher switcher) { return response; } catch (final SwitcherRemoteException e) { - logger.error("Failed to execute criteria - {}\nCause: {}", e.getMessage(), e.getCause()); + logger.error("Failed to execute criteria - Cause: {}", e.getMessage(), e.getCause()); return tryExecuteLocalCriteria(switcher, e); } } diff --git a/src/main/java/com/github/switcherapi/client/service/validators/DateTimeValidator.java b/src/main/java/com/github/switcherapi/client/service/validators/DateTimeValidator.java new file mode 100644 index 00000000..613f40b0 --- /dev/null +++ b/src/main/java/com/github/switcherapi/client/service/validators/DateTimeValidator.java @@ -0,0 +1,45 @@ +package com.github.switcherapi.client.service.validators; + +import com.github.switcherapi.client.utils.SwitcherUtils; +import org.apache.commons.lang3.RegExUtils; +import org.apache.commons.lang3.StringUtils; + +public abstract class DateTimeValidator extends Validator { + + private static final String LOG_DATE = "date: {}"; + private static final String LOG_DATE_TIME = "date time: {} {}"; + private static final String FULL_DATE_REGEX = "([12]\\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\\d|3[01]))"; + + private static final String DATE_FORMAT = "%s 00:00:00"; + private static final String HOUR_FORMAT = "%s %s:00:00"; + private static final String MINUTE_FORMAT = "%s %s:00"; + private static final String TIME_SPLIT = ":"; + private static final String DATE_SPLIT = " "; + + protected String getFullDate(final String date) { + SwitcherUtils.debug(logger, LOG_DATE, date); + + final String time = RegExUtils.removePattern(date, FULL_DATE_REGEX).trim(); + return getFullTime(date, time); + } + + protected String getFullTime(final String date, final String time) { + SwitcherUtils.debug(logger, LOG_DATE_TIME, date, time); + + if (StringUtils.isBlank(time)) { + return String.format(DATE_FORMAT, date); + } + + String[] timeSplit = time.split(TIME_SPLIT); + if (timeSplit.length == 1) { + return String.format(HOUR_FORMAT, date.split(DATE_SPLIT)[0], time); + } + + if (timeSplit.length == 2) { + return String.format(MINUTE_FORMAT, date.split(DATE_SPLIT)[0], time); + } + + return date; + } + +} diff --git a/src/main/java/com/github/switcherapi/client/service/validators/DateValidator.java b/src/main/java/com/github/switcherapi/client/service/validators/DateValidator.java index 8083aa5d..939bd94b 100644 --- a/src/main/java/com/github/switcherapi/client/service/validators/DateValidator.java +++ b/src/main/java/com/github/switcherapi/client/service/validators/DateValidator.java @@ -1,10 +1,5 @@ package com.github.switcherapi.client.service.validators; -import java.text.ParseException; -import java.util.Date; - -import org.apache.commons.lang3.time.DateUtils; - import com.github.switcherapi.client.exception.SwitcherInvalidOperationException; import com.github.switcherapi.client.exception.SwitcherInvalidOperationInputException; import com.github.switcherapi.client.exception.SwitcherInvalidTimeFormat; @@ -12,10 +7,13 @@ import com.github.switcherapi.client.model.EntryOperation; import com.github.switcherapi.client.model.StrategyValidator; import com.github.switcherapi.client.model.criteria.Strategy; -import com.github.switcherapi.client.utils.SwitcherUtils; +import org.apache.commons.lang3.time.DateUtils; + +import java.text.ParseException; +import java.util.Date; @ValidatorComponent(type = StrategyValidator.DATE) -public class DateValidator extends Validator { +public class DateValidator extends DateTimeValidator { public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; @@ -26,7 +24,6 @@ public boolean process(final Strategy strategy, final Entry switcherInput) throw try { return selectDateOperationCase(strategy, switcherInput); } catch (ParseException e) { - logger.error(e); throw new SwitcherInvalidTimeFormat(strategy.getStrategy(), e); } } @@ -38,20 +35,20 @@ private boolean selectDateOperationCase(final Strategy strategy, final Entry swi switch (strategy.getEntryOperation()) { case LOWER: - stgDate = DateUtils.parseDate(SwitcherUtils.getFullDate(strategy.getValues()[0]), DATE_FORMAT); - inputDate = DateUtils.parseDate(SwitcherUtils.getFullDate(switcherInput.getInput()), DATE_FORMAT); + stgDate = DateUtils.parseDate(getFullDate(strategy.getValues()[0]), DATE_FORMAT); + inputDate = DateUtils.parseDate(getFullDate(switcherInput.getInput()), DATE_FORMAT); return inputDate.before(stgDate); case GREATER: - stgDate = DateUtils.parseDate(SwitcherUtils.getFullDate(strategy.getValues()[0]), DATE_FORMAT); - inputDate = DateUtils.parseDate(SwitcherUtils.getFullDate(switcherInput.getInput()), DATE_FORMAT); + stgDate = DateUtils.parseDate(getFullDate(strategy.getValues()[0]), DATE_FORMAT); + inputDate = DateUtils.parseDate(getFullDate(switcherInput.getInput()), DATE_FORMAT); return inputDate.after(stgDate); case BETWEEN: if (strategy.getValues().length == 2) { - stgDate = DateUtils.parseDate(SwitcherUtils.getFullDate(strategy.getValues()[0]), DATE_FORMAT); - stgDate2 = DateUtils.parseDate(SwitcherUtils.getFullDate(strategy.getValues()[1]), DATE_FORMAT); - inputDate = DateUtils.parseDate(SwitcherUtils.getFullDate(switcherInput.getInput()), DATE_FORMAT); + stgDate = DateUtils.parseDate(getFullDate(strategy.getValues()[0]), DATE_FORMAT); + stgDate2 = DateUtils.parseDate(getFullDate(strategy.getValues()[1]), DATE_FORMAT); + inputDate = DateUtils.parseDate(getFullDate(switcherInput.getInput()), DATE_FORMAT); return inputDate.after(stgDate) && inputDate.before(stgDate2); } diff --git a/src/main/java/com/github/switcherapi/client/service/validators/RegexValidatorV8.java b/src/main/java/com/github/switcherapi/client/service/validators/RegexValidatorV8.java index d429b72d..07cc6b77 100644 --- a/src/main/java/com/github/switcherapi/client/service/validators/RegexValidatorV8.java +++ b/src/main/java/com/github/switcherapi/client/service/validators/RegexValidatorV8.java @@ -10,8 +10,8 @@ import com.github.switcherapi.client.model.criteria.Strategy; import com.github.switcherapi.client.service.WorkerName; import org.apache.commons.lang3.tuple.Pair; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.Arrays; import java.util.HashSet; @@ -32,7 +32,7 @@ @ValidatorComponent(type = StrategyValidator.REGEX) public class RegexValidatorV8 extends Validator { - private static final Logger logger = LogManager.getLogger(RegexValidatorV8.class); + private static final Logger logger = LoggerFactory.getLogger(RegexValidatorV8.class); /** * Global flag to interrupt workers. @@ -65,7 +65,7 @@ public boolean process(Strategy strategy, Entry switcherInput) throws SwitcherIn try { return timedMatch(switcherInput.getInput(), val); } catch (TimeoutException | SwitcherValidatorException e) { - logger.error(e); + logger.error(e.getMessage(), e); return false; } }); @@ -74,7 +74,7 @@ public boolean process(Strategy strategy, Entry switcherInput) throws SwitcherIn try { return timedMatch(switcherInput.getInput(), val); } catch (TimeoutException | SwitcherValidatorException e) { - logger.error(e); + logger.error(e.getMessage(), e); return true; } }); @@ -88,7 +88,7 @@ public boolean process(Strategy strategy, Entry switcherInput) throws SwitcherIn throw new SwitcherInvalidOperationException(strategy.getOperation(), strategy.getStrategy()); } } catch (TimeoutException | SwitcherValidatorException e) { - logger.error(e); + logger.error(e.getMessage(), e); return false; } } diff --git a/src/main/java/com/github/switcherapi/client/service/validators/TimeValidator.java b/src/main/java/com/github/switcherapi/client/service/validators/TimeValidator.java index 4b7abdbe..61381c2b 100644 --- a/src/main/java/com/github/switcherapi/client/service/validators/TimeValidator.java +++ b/src/main/java/com/github/switcherapi/client/service/validators/TimeValidator.java @@ -1,11 +1,5 @@ package com.github.switcherapi.client.service.validators; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; - -import org.apache.commons.lang3.time.DateUtils; - import com.github.switcherapi.client.exception.SwitcherInvalidOperationException; import com.github.switcherapi.client.exception.SwitcherInvalidOperationInputException; import com.github.switcherapi.client.exception.SwitcherInvalidTimeFormat; @@ -13,10 +7,14 @@ import com.github.switcherapi.client.model.EntryOperation; import com.github.switcherapi.client.model.StrategyValidator; import com.github.switcherapi.client.model.criteria.Strategy; -import com.github.switcherapi.client.utils.SwitcherUtils; +import org.apache.commons.lang3.time.DateUtils; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; @ValidatorComponent(type = StrategyValidator.TIME) -public class TimeValidator extends Validator { +public class TimeValidator extends DateTimeValidator { public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; @@ -29,7 +27,6 @@ public boolean process(Strategy strategy, Entry switcherInput) throws SwitcherIn final String today = format.format(new Date()); return selectTimeOperationCase(strategy, switcherInput, today); } catch (ParseException e) { - logger.error(e); throw new SwitcherInvalidTimeFormat(strategy.getStrategy(), e); } @@ -43,20 +40,20 @@ private boolean selectTimeOperationCase(final Strategy strategy, final Entry swi switch (strategy.getEntryOperation()) { case LOWER: - stgDate = DateUtils.parseDate(SwitcherUtils.getFullTime(today, strategy.getValues()[0]), DATE_FORMAT); - inputDate = DateUtils.parseDate(SwitcherUtils.getFullTime(today, switcherInput.getInput()), DATE_FORMAT); + stgDate = DateUtils.parseDate(getFullTime(today, strategy.getValues()[0]), DATE_FORMAT); + inputDate = DateUtils.parseDate(getFullTime(today, switcherInput.getInput()), DATE_FORMAT); return inputDate.before(stgDate); case GREATER: - stgDate = DateUtils.parseDate(SwitcherUtils.getFullTime(today, strategy.getValues()[0]), DATE_FORMAT); - inputDate = DateUtils.parseDate(SwitcherUtils.getFullTime(today, switcherInput.getInput()), DATE_FORMAT); + stgDate = DateUtils.parseDate(getFullTime(today, strategy.getValues()[0]), DATE_FORMAT); + inputDate = DateUtils.parseDate(getFullTime(today, switcherInput.getInput()), DATE_FORMAT); return inputDate.after(stgDate); case BETWEEN: if (strategy.getValues().length == 2) { - stgDate = DateUtils.parseDate(SwitcherUtils.getFullTime(today, strategy.getValues()[0]), DATE_FORMAT); - stgDate2 = DateUtils.parseDate(SwitcherUtils.getFullTime(today, strategy.getValues()[1]), DATE_FORMAT); - inputDate = DateUtils.parseDate(SwitcherUtils.getFullTime(today, switcherInput.getInput()), + stgDate = DateUtils.parseDate(getFullTime(today, strategy.getValues()[0]), DATE_FORMAT); + stgDate2 = DateUtils.parseDate(getFullTime(today, strategy.getValues()[1]), DATE_FORMAT); + inputDate = DateUtils.parseDate(getFullTime(today, switcherInput.getInput()), DATE_FORMAT); return inputDate.after(stgDate) && inputDate.before(stgDate2); 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 ef910866..8e601493 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 @@ -1,16 +1,15 @@ package com.github.switcherapi.client.service.validators; import com.github.switcherapi.client.utils.SwitcherUtils; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - import com.github.switcherapi.client.exception.SwitcherException; import com.github.switcherapi.client.model.Entry; import com.github.switcherapi.client.model.criteria.Strategy; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public abstract class Validator { - protected static final Logger logger = LogManager.getLogger(Validator.class); + protected static final Logger logger = LoggerFactory.getLogger(Validator.class); public static final String DEBUG_SWITCHER_INPUT = "switcherInput: {}"; public static final String DEBUG_STRATEGY = "strategy: {}"; 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 6752642f..054d0e5f 100644 --- a/src/main/java/com/github/switcherapi/client/utils/SnapshotEventHandler.java +++ b/src/main/java/com/github/switcherapi/client/utils/SnapshotEventHandler.java @@ -1,7 +1,7 @@ package com.github.switcherapi.client.utils; import com.github.switcherapi.client.exception.SwitcherException; -import org.apache.logging.log4j.LogManager; +import org.slf4j.LoggerFactory; /** * Access snapshot event handler when a file is modified.
@@ -16,7 +16,7 @@ public interface SnapshotEventHandler { * Callback method that will be invoked when the snapshot is updated */ default void onSuccess() { - SwitcherUtils.debug(LogManager.getLogger(SnapshotEventHandler.class), "Snapshot has been changed"); + SwitcherUtils.debug(LoggerFactory.getLogger(SnapshotEventHandler.class), "Snapshot has been changed"); } /** @@ -25,7 +25,7 @@ default void onSuccess() { * @param exception Exception */ default void onError(SwitcherException exception) { - LogManager.getLogger(SnapshotEventHandler.class).error(exception); + LoggerFactory.getLogger(SnapshotEventHandler.class).error(exception.getMessage(), exception); } } diff --git a/src/main/java/com/github/switcherapi/client/utils/SnapshotLoader.java b/src/main/java/com/github/switcherapi/client/utils/SnapshotLoader.java index 608b23ba..0b24cd2d 100644 --- a/src/main/java/com/github/switcherapi/client/utils/SnapshotLoader.java +++ b/src/main/java/com/github/switcherapi/client/utils/SnapshotLoader.java @@ -8,8 +8,6 @@ import com.google.gson.GsonBuilder; import com.google.gson.JsonIOException; import com.google.gson.JsonSyntaxException; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; import java.io.*; import java.nio.file.Files; @@ -22,8 +20,6 @@ */ public class SnapshotLoader { - private static final Logger logger = LogManager.getLogger(SnapshotLoader.class); - private static final String SNAPSHOT_FILE_FORMAT = "%s/%s.json"; private static final Gson gson = new Gson(); @@ -42,7 +38,6 @@ public static Domain loadSnapshot(final String snapshotFile) throws SwitcherSnap final Snapshot data = gson.fromJson(fileReader, Snapshot.class); return data.getDomain(); } catch (JsonSyntaxException | JsonIOException | IOException e) { - logger.error(e); throw new SwitcherSnapshotLoadException(snapshotFile, e); } } @@ -62,11 +57,7 @@ public static Domain loadSnapshot(final String snapshotLocation, final String en final Snapshot data = gson.fromJson(fileReader, Snapshot.class); return data.getDomain(); } catch (JsonSyntaxException | JsonIOException e) { - logger.error(e); throw new SwitcherSnapshotLoadException(String.format(SNAPSHOT_FILE_FORMAT, snapshotLocation, environment), e); - } catch (IOException e) { - logger.error(e); - throw e; } } @@ -87,9 +78,8 @@ public static void saveSnapshot(final Snapshot snapshot, final String snapshotLo Path path = Paths.get(snapshotLocation); if (!path.toFile().exists()) Files.createDirectories(path); - } catch (Exception ioe) { - logger.error(ioe); - throw new SwitcherSnapshotWriteException(String.format(SNAPSHOT_FILE_FORMAT, snapshotLocation, environment), ioe); + } catch (Exception e) { + throw new SwitcherSnapshotWriteException(String.format(SNAPSHOT_FILE_FORMAT, snapshotLocation, environment), e); } try ( @@ -98,7 +88,6 @@ public static void saveSnapshot(final Snapshot snapshot, final String snapshotLo final PrintWriter wr = new PrintWriter(bw)) { wr.write(gson.toJson(snapshot)); } catch (Exception e) { - logger.error(e); throw new SwitcherSnapshotWriteException(String.format(SNAPSHOT_FILE_FORMAT, snapshotLocation, environment), e); } } diff --git a/src/main/java/com/github/switcherapi/client/utils/SnapshotWatcher.java b/src/main/java/com/github/switcherapi/client/utils/SnapshotWatcher.java index 703d8bbd..894fa2cd 100644 --- a/src/main/java/com/github/switcherapi/client/utils/SnapshotWatcher.java +++ b/src/main/java/com/github/switcherapi/client/utils/SnapshotWatcher.java @@ -1,8 +1,8 @@ package com.github.switcherapi.client.utils; import com.github.switcherapi.client.service.local.SwitcherLocalService; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.IOException; import java.nio.file.*; @@ -15,7 +15,7 @@ */ public class SnapshotWatcher implements Runnable { - private static final Logger logger = LogManager.getLogger(SnapshotWatcher.class); + private static final Logger logger = LoggerFactory.getLogger(SnapshotWatcher.class); private final SnapshotEventHandler handler; @@ -74,7 +74,7 @@ public void terminate() { watcher.close(); } } catch (IOException e) { - logger.error(e); + logger.error(e.getMessage(), e); } } 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 d5e1e3b4..62a6b23a 100644 --- a/src/main/java/com/github/switcherapi/client/utils/SwitcherUtils.java +++ b/src/main/java/com/github/switcherapi/client/utils/SwitcherUtils.java @@ -6,12 +6,11 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; -import org.apache.commons.lang3.RegExUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.time.DateUtils; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; import org.glassfish.jersey.internal.guava.Sets; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.Date; import java.util.Map.Entry; @@ -26,7 +25,7 @@ */ public class SwitcherUtils { - private static final Logger logger = LogManager.getLogger(SwitcherUtils.class); + private static final Logger logger = LoggerFactory.getLogger(SwitcherUtils.class); private static final String LOG_DATE = "date: {}"; @@ -39,8 +38,6 @@ public class SwitcherUtils { */ private static final String[] DURATION = { "s", "m", "h", "d" }; - private static final String FULL_DATE_REGEX = "([12]\\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\\d|3[01]))"; - private static final String ENV_VARIABLE_PATTERN = "\\$\\{(\\w+)}"; private static final String ENV_DEFAULT_VARIABLE_PATTERN = "\\$\\{(\\w+):(.+)}"; @@ -79,28 +76,6 @@ public static long getMillis(final String time) { throw new SwitcherInvalidDateTimeArgumentException(time); } - public static String getFullDate(final String date) { - SwitcherUtils.debug(logger, 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) { - SwitcherUtils.debug(logger, LOG_DATE, date); - SwitcherUtils.debug(logger, LOG_TME, time); - - if (StringUtils.isBlank(time)) { - return String.format("%s 00:00:00", date); - } else if (time.split(":").length == 1) { - return String.format("%s %s:00:00", date.split(" ")[0], time); - } else if (time.split(":").length == 2) { - return String.format("%s %s:00", date.split(" ")[0], time); - } - - return date; - } - public static Set payloadReader(String jsonStr, String prevKey) { final JsonElement parser = JsonParser.parseString(jsonStr); final JsonObject jsonObject = parser.getAsJsonObject(); @@ -165,20 +140,6 @@ public static void debug(Logger logger, String message, Object... args) { } } - /** - * Log debug message if logger is enabled. - * Use this method to avoid resource waste when logger is disabled. - * - * @param logger class logger - * @param message to be logged - * @param paramSuppliers parameters to be replaced in the message - */ - public static void debugSupplier(Logger logger, String message, Object paramSuppliers) { - if (logger.isDebugEnabled()) { - logger.debug(message, () -> paramSuppliers); - } - } - /** * Resolve environment variable 'value' and extract its value from either * System environment or default argument. diff --git a/src/main/resources/log4j2.properties b/src/main/resources/log4j2.properties deleted file mode 100644 index cac7c5d6..00000000 --- a/src/main/resources/log4j2.properties +++ /dev/null @@ -1,36 +0,0 @@ -status = info - -appender.console.type = Console -appender.console.name = LogToConsole -appender.console.layout.type = PatternLayout -appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n - -#appender.file.type = File -#appender.file.name = LogToFile -#appender.file.fileName = logs/switcher-client.log -#appender.file.layout.type = PatternLayout -#appender.file.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n - -# Rotate log file -appender.rolling.type = RollingFile -appender.rolling.name = LogToRollingFile -appender.rolling.fileName = logs/switcher-client.log -appender.rolling.filePattern = logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz -appender.rolling.layout.type = PatternLayout -appender.rolling.layout.pattern = %d %p %C{1.} [%t] %m%n -appender.rolling.policies.type = Policies -appender.rolling.policies.time.type = TimeBasedTriggeringPolicy -appender.rolling.policies.size.type = SizeBasedTriggeringPolicy -appender.rolling.policies.size.size = 10MB -appender.rolling.strategy.type = DefaultRolloverStrategy -appender.rolling.strategy.max = 10 - -# Log to console and rolling file -logger.app.name = com.github.switcherapi.client -logger.app.level = debug -logger.app.additivity = false -#logger.app.appenderRef.rolling.ref = LogToRollingFile -logger.app.appenderRef.console.ref = LogToConsole - -rootLogger.level = info -rootLogger.appenderRef.stdout.ref = LogToConsole \ No newline at end of file diff --git a/src/test/java/com/github/switcherapi/client/utils/SnapshotWatcherTest.java b/src/test/java/com/github/switcherapi/client/utils/SnapshotWatcherTest.java index edde8d3d..fab33518 100644 --- a/src/test/java/com/github/switcherapi/client/utils/SnapshotWatcherTest.java +++ b/src/test/java/com/github/switcherapi/client/utils/SnapshotWatcherTest.java @@ -9,12 +9,12 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; import org.apache.commons.lang3.StringUtils; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.BufferedWriter; import java.io.FileWriter; @@ -28,7 +28,7 @@ class SnapshotWatcherTest { - private static final Logger logger = LogManager.getLogger(SnapshotWatcherTest.class); + private static final Logger logger = LoggerFactory.getLogger(SnapshotWatcherTest.class); private static final String SNAPSHOTS_LOCAL = Paths.get(StringUtils.EMPTY).toAbsolutePath() + "/src/test/resources"; @@ -92,7 +92,7 @@ void writeFixture(String content) { final PrintWriter wr = new PrintWriter(bw)) { wr.write(content); } catch (Exception e) { - logger.error(e); + logger.error(e.getMessage(), e); } } diff --git a/src/test/java/com/github/switcherapi/fixture/CountDownHelper.java b/src/test/java/com/github/switcherapi/fixture/CountDownHelper.java index 66936faa..062b3b8b 100644 --- a/src/test/java/com/github/switcherapi/fixture/CountDownHelper.java +++ b/src/test/java/com/github/switcherapi/fixture/CountDownHelper.java @@ -1,14 +1,14 @@ package com.github.switcherapi.fixture; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; public class CountDownHelper { - private static final Logger logger = LogManager.getLogger(CountDownHelper.class); + private static final Logger logger = LoggerFactory.getLogger(CountDownHelper.class); public static void wait(int seconds) { try { @@ -16,7 +16,7 @@ public static void wait(int seconds) { boolean finished = waiter.await(seconds, TimeUnit.SECONDS); if (!finished) { - logger.error("Countdown failed"); + logger.warn("Timeout reached"); } } catch (InterruptedException e) { logger.error(e.getMessage(), e); diff --git a/src/test/java/com/github/switcherapi/playground/ClientPlayground.java b/src/test/java/com/github/switcherapi/playground/ClientPlayground.java index 7f18537a..bf5318ff 100644 --- a/src/test/java/com/github/switcherapi/playground/ClientPlayground.java +++ b/src/test/java/com/github/switcherapi/playground/ClientPlayground.java @@ -2,11 +2,10 @@ import static com.github.switcherapi.playground.Features.*; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - import com.github.switcherapi.client.ContextBuilder; import com.github.switcherapi.client.model.Switcher; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; @@ -14,7 +13,7 @@ public class ClientPlayground { - static final Logger logger = LogManager.getLogger(ClientPlayground.class); + static final Logger logger = LoggerFactory.getLogger(ClientPlayground.class); private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);