diff --git a/config-examples/pom.xml b/config-examples/pom.xml index 3d511e8..31ba262 100644 --- a/config-examples/pom.xml +++ b/config-examples/pom.xml @@ -18,13 +18,17 @@ scalecube-config ${project.version} + + org.slf4j + slf4j-api + org.apache.logging.log4j - log4j-core + log4j-slf4j-impl org.apache.logging.log4j - log4j-jpl + log4j-core diff --git a/config-vault/pom.xml b/config-vault/pom.xml index ba67de3..47e76de 100644 --- a/config-vault/pom.xml +++ b/config-vault/pom.xml @@ -22,6 +22,10 @@ com.bettercloud vault-java-driver + + org.slf4j + slf4j-api + diff --git a/config-vault/src/main/java/io/scalecube/config/vault/VaultClientTokenSupplier.java b/config-vault/src/main/java/io/scalecube/config/vault/VaultClientTokenSupplier.java index 67bbf7c..e487e1c 100644 --- a/config-vault/src/main/java/io/scalecube/config/vault/VaultClientTokenSupplier.java +++ b/config-vault/src/main/java/io/scalecube/config/vault/VaultClientTokenSupplier.java @@ -2,15 +2,15 @@ import com.bettercloud.vault.VaultConfig; import com.bettercloud.vault.VaultException; -import java.lang.System.Logger; -import java.lang.System.Logger.Level; import java.util.Objects; import java.util.concurrent.CompletableFuture; import java.util.concurrent.Future; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class VaultClientTokenSupplier { - private static final Logger LOGGER = System.getLogger(VaultClientTokenSupplier.class.getName()); + private static final Logger LOGGER = LoggerFactory.getLogger(VaultClientTokenSupplier.class); private final String vaultAddress; private final String vaultToken; @@ -74,8 +74,7 @@ private String getToken0() { if (!isNullOrNoneOrEmpty(vaultRole)) { if (!isNullOrNoneOrEmpty(vaultToken)) { - LOGGER.log( - Level.WARNING, + LOGGER.warn( "Taking KubernetesVaultTokenSupplier by precedence rule, " + "ignoring EnvironmentVaultTokenSupplier " + "(specify either vaultToken or vaultRole, not both)"); diff --git a/config-vault/src/main/java/io/scalecube/config/vault/VaultConfigSource.java b/config-vault/src/main/java/io/scalecube/config/vault/VaultConfigSource.java index afb54ff..af89d87 100644 --- a/config-vault/src/main/java/io/scalecube/config/vault/VaultConfigSource.java +++ b/config-vault/src/main/java/io/scalecube/config/vault/VaultConfigSource.java @@ -8,8 +8,6 @@ import io.scalecube.config.ConfigSourceNotAvailableException; import io.scalecube.config.source.ConfigSource; import io.scalecube.config.source.LoadedConfigProperty; -import java.lang.System.Logger; -import java.lang.System.Logger.Level; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -22,6 +20,8 @@ import java.util.function.Function; import java.util.function.UnaryOperator; import java.util.stream.Collectors; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * This class is an implementation of {@link ConfigSource} for Vault. @@ -30,7 +30,7 @@ */ public class VaultConfigSource implements ConfigSource { - private static final Logger LOGGER = System.getLogger(VaultConfigSource.class.getName()); + private static final Logger LOGGER = LoggerFactory.getLogger(VaultConfigSource.class); private static final EnvironmentLoader ENVIRONMENT_LOADER = new EnvironmentLoader(); @@ -58,12 +58,12 @@ public Map loadConfig() { result.putAll(pathProps); } catch (VaultException ex) { if (ex.getHttpStatusCode() == 404) { - LOGGER.log(Level.ERROR, "Unable to load config properties from: " + path); + LOGGER.error("Unable to load config properties from: {}", path); } else { throw new ConfigSourceNotAvailableException(ex); } } catch (Exception ex) { - LOGGER.log(Level.ERROR, "Unable to load config properties from: " + path, ex); + LOGGER.error("Unable to load config properties from: {}", path, ex); throw new ConfigSourceNotAvailableException(ex); } } diff --git a/config-vault/src/main/java/io/scalecube/config/vault/VaultInvoker.java b/config-vault/src/main/java/io/scalecube/config/vault/VaultInvoker.java index 7f91a5a..d158cd4 100644 --- a/config-vault/src/main/java/io/scalecube/config/vault/VaultInvoker.java +++ b/config-vault/src/main/java/io/scalecube/config/vault/VaultInvoker.java @@ -9,8 +9,6 @@ import com.bettercloud.vault.response.VaultResponse; import com.bettercloud.vault.rest.RestResponse; import io.scalecube.config.utils.ThrowableUtil; -import java.lang.System.Logger; -import java.lang.System.Logger.Level; import java.nio.charset.StandardCharsets; import java.util.Objects; import java.util.Optional; @@ -19,10 +17,12 @@ import java.util.concurrent.TimeUnit; import java.util.function.Function; import java.util.function.UnaryOperator; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class VaultInvoker { - private static final Logger LOGGER = System.getLogger(VaultInvoker.class.getName()); + private static final Logger LOGGER = LoggerFactory.getLogger(VaultInvoker.class); private static final int STATUS_CODE_FORBIDDEN = 403; public static final int STATUS_CODE_HEALTH_OK = 200; @@ -58,9 +58,8 @@ public T invoke(VaultCall call) throws VaultExcepti } catch (VaultException e) { // try recreate Vault according to https://www.vaultproject.io/api/overview#http-status-codes if (e.getHttpStatusCode() == STATUS_CODE_FORBIDDEN) { - LOGGER.log( - Level.WARNING, - "Authentication failed (error message: {0}), now trying to recreate vault", + LOGGER.warn( + "Authentication failed (error message: {}), now trying to recreate vault", e.getMessage()); vault = recreateVault(vault); return call.apply(vault); @@ -94,17 +93,13 @@ private synchronized Vault recreateVault(Vault prev) throws VaultException { long delay = TimeUnit.SECONDS.toMillis(suggestedRefreshInterval(ttl)); timer = new Timer("VaultScheduler", true); timer.schedule(new RenewTokenTask(), delay); - LOGGER.log( - Level.INFO, - "Renew token timer was set to {0,number,#}s, (TTL = {1,number,#}s)", - delay, - ttl); + LOGGER.info("Renew token timer was set to {}s, (TTL = {}s)", delay, ttl); } else { - LOGGER.log(Level.WARNING, "Vault token is not renewable"); + LOGGER.warn("Vault token is not renewable"); } this.vault = vault; } catch (VaultException e) { - LOGGER.log(Level.ERROR, "Could not initialize and validate the vault", e); + LOGGER.error("Could not initialize and validate the vault", e); throw e; } return vault; @@ -118,22 +113,22 @@ private void renewToken() throws VaultException { try { AuthResponse response = vault.auth().renewSelf(); long ttl = response.getAuthLeaseDuration(); - LOGGER.log(Level.DEBUG, "Token was successfully renewed (new TTL = {0,number,#}s)", ttl); + LOGGER.debug("Token was successfully renewed (new TTL = {}s)", ttl); if (response.isAuthRenewable()) { if (ttl > 1) { long delay = TimeUnit.SECONDS.toMillis(suggestedRefreshInterval(ttl)); timer.schedule(new RenewTokenTask(), delay); } else { - LOGGER.log(Level.WARNING, "Token TTL ({0,number,#}s) is not enough for scheduling", ttl); + LOGGER.warn("Token TTL ({}s) is not enough for scheduling", ttl); vault = recreateVault(vault); } } else { - LOGGER.log(Level.WARNING, "Vault token is not renewable now"); + LOGGER.warn("Vault token is not renewable now"); } } catch (VaultException e) { // try recreate Vault according to https://www.vaultproject.io/api/overview#http-status-codes if (e.getHttpStatusCode() == STATUS_CODE_FORBIDDEN) { - LOGGER.log(Level.WARNING, "Could not renew the Vault token", e); + LOGGER.warn("Could not renew the Vault token", e); //noinspection UnusedAssignment vault = recreateVault(vault); } @@ -169,7 +164,7 @@ private void checkResponse(RestResponse restResponse) throws VaultException { case STATUS_CODE_RESPONSE_NO_DATA: return; default: - LOGGER.log(Level.WARNING, "Vault responded with code: " + status); + LOGGER.warn("Vault responded with code: {}", status); throw new VaultException(bodyAsString(restResponse), status); } } diff --git a/config/pom.xml b/config/pom.xml index a7d34e2..58aafbc 100644 --- a/config/pom.xml +++ b/config/pom.xml @@ -12,4 +12,11 @@ scalecube-config + + + org.slf4j + slf4j-api + + + diff --git a/config/src/main/java/io/scalecube/config/AbstractConfigProperty.java b/config/src/main/java/io/scalecube/config/AbstractConfigProperty.java index e2e1e18..e2149e2 100644 --- a/config/src/main/java/io/scalecube/config/AbstractConfigProperty.java +++ b/config/src/main/java/io/scalecube/config/AbstractConfigProperty.java @@ -1,8 +1,6 @@ package io.scalecube.config; import io.scalecube.config.source.LoadedConfigProperty; -import java.lang.System.Logger; -import java.lang.System.Logger.Level; import java.util.Collection; import java.util.List; import java.util.Map; @@ -13,6 +11,8 @@ import java.util.function.Function; import java.util.function.Predicate; import java.util.stream.Collectors; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Abstract parent class for config property classes. Holds mutable state fields: {@link #value} the @@ -25,7 +25,7 @@ */ abstract class AbstractConfigProperty { - private static final Logger LOGGER = System.getLogger(AbstractConfigProperty.class.getName()); + private static final Logger LOGGER = LoggerFactory.getLogger(AbstractConfigProperty.class); private static final String ERROR_VALIDATION_FAILED = "Validation failed on config property: %s, failed value: %s"; @@ -133,10 +133,9 @@ private void invokeCallback(BiConsumer callback, T t1, T t2) { try { callback.accept(t1, t2); } catch (Exception e) { - LOGGER.log( - Level.ERROR, + LOGGER.error( "Exception occurred on property-change callback: " - + "{0}, property name: {1}, oldValue: {2}, newValue: {3}", + + "{}, property name: {}, oldValue: {}, newValue: {}", callback, name, t1, diff --git a/config/src/main/java/io/scalecube/config/ConfigRegistryImpl.java b/config/src/main/java/io/scalecube/config/ConfigRegistryImpl.java index 455c160..0afd8e8 100644 --- a/config/src/main/java/io/scalecube/config/ConfigRegistryImpl.java +++ b/config/src/main/java/io/scalecube/config/ConfigRegistryImpl.java @@ -6,8 +6,6 @@ import io.scalecube.config.source.ConfigSourceInfo; import io.scalecube.config.source.LoadedConfigProperty; import io.scalecube.config.utils.ThrowableUtil; -import java.lang.System.Logger; -import java.lang.System.Logger.Level; import java.lang.management.ManagementFactory; import java.lang.reflect.Field; import java.time.Duration; @@ -34,10 +32,12 @@ import java.util.stream.Stream; import javax.management.MBeanServer; import javax.management.ObjectName; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; final class ConfigRegistryImpl implements ConfigRegistry { - private static final Logger LOGGER = System.getLogger(ConfigRegistryImpl.class.getName()); + private static final Logger LOGGER = LoggerFactory.getLogger(ConfigRegistryImpl.class); static final Function STRING_PARSER = str -> str; static final Function DOUBLE_PARSER = Double::parseDouble; @@ -56,8 +56,7 @@ final class ConfigRegistryImpl implements ConfigRegistry { Thread thread = new Thread(r); thread.setDaemon(true); thread.setName("config-registry"); - thread.setUncaughtExceptionHandler( - (t, e) -> LOGGER.log(Level.ERROR, "Exception occurred", e)); + thread.setUncaughtExceptionHandler((t, e) -> LOGGER.error("Exception occurred", e)); return thread; }; reloadExecutor = Executors.newSingleThreadScheduledExecutor(threadFactory); @@ -76,7 +75,7 @@ final class ConfigRegistryImpl implements ConfigRegistry { new ConcurrentHashMap<>(); private final LinkedHashMap recentConfigEvents = - new LinkedHashMap() { + new LinkedHashMap<>() { @Override protected boolean removeEldestEntry(Map.Entry eldest) { return size() > settings.getRecentConfigEventsNum(); @@ -97,7 +96,7 @@ void init() { try { loadAndNotify(); } catch (Exception e) { - LOGGER.log(Level.ERROR, "[loadAndNotify] Exception occurred, cause: {0}", e); + LOGGER.error("[loadAndNotify] Exception occurred", e); } }, settings.getReloadIntervalSec(), @@ -490,12 +489,8 @@ private void reportChanges(Collection events) { try { eventListener.onEvents(configEvents); } catch (Exception e) { - LOGGER.log( - Level.ERROR, - "Exception on configEventListener: {0}, events: {1}", - key, - configEvents, - e); + LOGGER.error( + "Exception on configEventListener: {}, events: {}", key, configEvents, e); } }); } @@ -505,9 +500,9 @@ private void computeConfigLoadStatus(String sourceName, Throwable ex) { Integer status0 = configSourceStatusMap.put(sourceName, status); if (status0 == null || (status0 ^ status) == 1) { if (status == 1) { - LOGGER.log(Level.ERROR, "[loadConfig][{0}] Exception occurred", sourceName, ex); + LOGGER.error("[loadConfig][{}] Exception occurred", sourceName, ex); } else { - LOGGER.log(Level.DEBUG, "[loadConfig][{0}] Loaded config properties", sourceName); + LOGGER.debug("[loadConfig][{}] Loaded config properties", sourceName); } } } diff --git a/config/src/main/java/io/scalecube/config/ObjectPropertyParser.java b/config/src/main/java/io/scalecube/config/ObjectPropertyParser.java index e579151..b1641da 100644 --- a/config/src/main/java/io/scalecube/config/ObjectPropertyParser.java +++ b/config/src/main/java/io/scalecube/config/ObjectPropertyParser.java @@ -9,7 +9,7 @@ /** * Parser for {@link ObjectConfigProperty}. Returns object instance of the given class by the list - * of {@link LoadedConfigProperty}-s and list of {@link ObjectPropertyField}-s. The class must + * of {@link LoadedConfigProperty} objects and list of {@link ObjectPropertyField} . The class must * contain default constructor. */ class ObjectPropertyParser { @@ -31,8 +31,7 @@ static T parseObject( } Map> inputMap = - inputList - .stream() + inputList.stream() .collect( Collectors.toMap(LoadedConfigProperty::name, LoadedConfigProperty::valueAsString)); diff --git a/config/src/main/java/io/scalecube/config/PropertyCallback.java b/config/src/main/java/io/scalecube/config/PropertyCallback.java index 6dbf213..5019de5 100644 --- a/config/src/main/java/io/scalecube/config/PropertyCallback.java +++ b/config/src/main/java/io/scalecube/config/PropertyCallback.java @@ -2,13 +2,13 @@ import io.scalecube.config.audit.ConfigEvent; import io.scalecube.config.source.LoadedConfigProperty; -import java.lang.System.Logger; -import java.lang.System.Logger.Level; import java.util.Collection; import java.util.List; import java.util.concurrent.CopyOnWriteArraySet; import java.util.function.Function; import java.util.stream.Collectors; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Property controller class for config property instances of type {@link T}. Config property @@ -18,12 +18,7 @@ */ class PropertyCallback { - private static final Logger LOGGER = System.getLogger(PropertyCallback.class.getName()); - - private static final String ERROR_EXCEPTION_ON_VALUE_PARSER = - "Exception occurred at valueParser on input: %s, cause: %s"; - private static final String ERROR_EXCEPTION_AT_ACCEPT_VALUE = - "Exception occurred at acceptValue on input: %s, on value: %s, cause: %s"; + private static final Logger LOGGER = LoggerFactory.getLogger(PropertyCallback.class); /** * Value parser function. Converts list of string name-value pairs (input list can be null or @@ -55,7 +50,7 @@ void addConfigProperty(AbstractConfigProperty configProperty) { /** * Computes new value for config property instances (of type {@link T}) from the list of {@link - * ConfigEvent}-s. This method is being called from config registry reload process. + * ConfigEvent} objects. This method is being called from config registry reload process. * * @param events config events computed during config registry reload. */ @@ -78,7 +73,7 @@ void computeValue(List events) { try { value = applyValueParser(inputList); } catch (Exception e) { - LOGGER.log(Level.ERROR, e); + LOGGER.error("Exception occurred", e); return; // return right away if parser failed } @@ -88,16 +83,18 @@ void computeValue(List events) { try { configProperty.acceptValue(newValue, inputList, true /* invokeCallbacks */); } catch (Exception e) { - LOGGER.log( - Level.ERROR, - String.format(ERROR_EXCEPTION_AT_ACCEPT_VALUE, inputList, newValue, e)); + LOGGER.error( + "Exception occurred at acceptValue on input: {}, on value: {}", + inputList, + newValue, + e); } }); } /** * Computes new value for config property instance (passed as second parameter) from the list of - * {@link LoadedConfigProperty}-s. + * {@link LoadedConfigProperty} objects. * * @param inputList an input for {@link #valueParser}. * @param configProperty an instance where attempt to set a new value has to be made. @@ -111,7 +108,7 @@ void computeValue( configProperty.acceptValue(value, inputList, false /* invokeCallbacks */); } catch (Exception e) { throw new IllegalArgumentException( - String.format(ERROR_EXCEPTION_AT_ACCEPT_VALUE, inputList, value, e)); + "Exception occurred at acceptValue on input: " + inputList, e); } } @@ -120,7 +117,7 @@ private T applyValueParser(List inputList) { return valueParser.apply(inputList); } catch (Exception e) { throw new IllegalArgumentException( - String.format(ERROR_EXCEPTION_ON_VALUE_PARSER, inputList, e)); + "Exception occurred at valueParser on input: " + inputList, e); } } } diff --git a/config/src/main/java/io/scalecube/config/audit/LoggingConfigEventListener.java b/config/src/main/java/io/scalecube/config/audit/LoggingConfigEventListener.java index 27cc437..40b644e 100644 --- a/config/src/main/java/io/scalecube/config/audit/LoggingConfigEventListener.java +++ b/config/src/main/java/io/scalecube/config/audit/LoggingConfigEventListener.java @@ -1,14 +1,14 @@ package io.scalecube.config.audit; -import java.lang.System.Logger; -import java.lang.System.Logger.Level; import java.util.Collection; import java.util.Comparator; import java.util.Objects; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class LoggingConfigEventListener implements ConfigEventListener { - private static final Logger LOGGER = System.getLogger(LoggingConfigEventListener.class.getName()); + private static final Logger LOGGER = LoggerFactory.getLogger(LoggingConfigEventListener.class); @Override public void onEvents(Collection events) { @@ -30,7 +30,7 @@ public void onEvents(Collection events) { sb.append(originAsString(event)); }); sb.append("\n").append("]"); - LOGGER.log(Level.INFO, sb.toString()); + LOGGER.info(sb.toString()); } } diff --git a/config/src/main/java/io/scalecube/config/keyvalue/KeyValueConfigSource.java b/config/src/main/java/io/scalecube/config/keyvalue/KeyValueConfigSource.java index 83fb1fc..05c59e7 100644 --- a/config/src/main/java/io/scalecube/config/keyvalue/KeyValueConfigSource.java +++ b/config/src/main/java/io/scalecube/config/keyvalue/KeyValueConfigSource.java @@ -5,8 +5,6 @@ import io.scalecube.config.source.ConfigSource; import io.scalecube.config.source.LoadedConfigProperty; import io.scalecube.config.utils.ThrowableUtil; -import java.lang.System.Logger; -import java.lang.System.Logger.Level; import java.time.Duration; import java.util.ArrayList; import java.util.Arrays; @@ -26,6 +24,8 @@ import java.util.function.Supplier; import java.util.stream.Collector; import java.util.stream.Collectors; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Generic key-value config source. Communicates with concrete config data source (mongodb, redis, @@ -33,7 +33,7 @@ */ public class KeyValueConfigSource implements ConfigSource { - private static final Logger LOGGER = System.getLogger(KeyValueConfigSource.class.getName()); + private static final Logger LOGGER = LoggerFactory.getLogger(KeyValueConfigSource.class); private static final ThreadFactory threadFactory; @@ -43,8 +43,7 @@ public class KeyValueConfigSource implements ConfigSource { Thread thread = new Thread(r); thread.setDaemon(true); thread.setName("keyvalue-config-executor"); - thread.setUncaughtExceptionHandler( - (t, e) -> LOGGER.log(Level.ERROR, "Exception occurred", e)); + thread.setUncaughtExceptionHandler((t, e) -> LOGGER.error("Exception occurred", e)); return thread; }; } @@ -129,12 +128,7 @@ private CompletableFuture> loadConfig(KeyValueConfigN try { result = repository.findAll(configName); } catch (Exception e) { - LOGGER.log( - Level.WARNING, - "Exception at {0}.findAll({1})", - repository.getClass().getSimpleName(), - configName, - e); + LOGGER.warn("[loadConfig] Exception occurred, configName: {}", configName, e); result = Collections.emptyList(); } return result; diff --git a/pom.xml b/pom.xml index dac9118..ef8b4db 100644 --- a/pom.xml +++ b/pom.xml @@ -41,6 +41,7 @@ 5.1.0 + 1.7.36 2.27.0 5.1.1 @@ -48,7 +49,6 @@ 1.20.1 33.3.0-jre 2.20.0 - 1.7.30 https://maven.pkg.github.com/scalecube/scalecube-config @@ -70,12 +70,12 @@ org.apache.logging.log4j - log4j-core + log4j-slf4j-impl ${log4j.version} org.apache.logging.log4j - log4j-jpl + log4j-core ${log4j.version} @@ -150,12 +150,12 @@ org.apache.logging.log4j - log4j-core + log4j-slf4j-impl test org.apache.logging.log4j - log4j-jpl + log4j-core test