Allow external injection of Loggers into SecretLeaseContainer and PropertySources.

Several types are now enabled for reflective injection of the Logger to allow logger re-routing if needed. Especially for Spring Cloud Vault we reconfigure loggers so that components can log their failures during the bootstrap phase while the actual logging framework is not yet configured.

Closes gh-636.
This commit is contained in:
Mark Paluch
2021-03-10 15:14:41 +01:00
parent e0bea23965
commit f1e741c017
6 changed files with 29 additions and 22 deletions

View File

@@ -41,7 +41,7 @@ import org.springframework.util.StringUtils;
*/
public class MacAddressUserId implements AppIdUserIdMechanism {
private final Log log = LogFactory.getLog(MacAddressUserId.class);
private final Log logger = LogFactory.getLog(MacAddressUserId.class);
private final String networkInterfaceHint;
@@ -100,7 +100,7 @@ public class MacAddressUserId implements AppIdUserIdMechanism {
if (!networkInterface.isPresent()) {
if (StringUtils.hasText(this.networkInterfaceHint)) {
this.log.warn(String.format("Did not find a NetworkInterface applying hint %s",
this.logger.warn(String.format("Did not find a NetworkInterface applying hint %s",
this.networkInterfaceHint));
}

View File

@@ -83,7 +83,8 @@ import static org.springframework.vault.support.SslConfiguration.KeyConfiguratio
*/
public class ClientHttpRequestFactoryFactory {
private static final Log logger = LogFactory.getLog(ClientHttpRequestFactoryFactory.class);
@SuppressWarnings("FieldMayBeFinal") // allow setting via reflection.
private static Log logger = LogFactory.getLog(ClientHttpRequestFactoryFactory.class);
private static final boolean HTTP_COMPONENTS_PRESENT = isPresent("org.apache.http.client.HttpClient");

View File

@@ -56,7 +56,8 @@ import org.springframework.vault.support.JsonMapFlattener;
*/
public class LeaseAwareVaultPropertySource extends EnumerablePropertySource<VaultOperations> {
private static final Log logger = LogFactory.getLog(LeaseAwareVaultPropertySource.class);
@SuppressWarnings("FieldMayBeFinal") // allow setting via reflection.
private static Log logger = LogFactory.getLog(LeaseAwareVaultPropertySource.class);
private final SecretLeaseContainer secretLeaseContainer;

View File

@@ -47,7 +47,8 @@ import org.springframework.vault.support.VaultResponse;
*/
public class VaultPropertySource extends EnumerablePropertySource<VaultOperations> {
private static final Log logger = LogFactory.getLog(VaultPropertySource.class);
@SuppressWarnings("FieldMayBeFinal") // allow setting via reflection.
private static Log logger = LogFactory.getLog(VaultPropertySource.class);
private final String path;

View File

@@ -132,7 +132,8 @@ public class SecretLeaseContainer extends SecretLeaseEventPublisher implements I
private static final int STATUS_DESTROYED = 2;
private static final Log log = LogFactory.getLog(SecretLeaseContainer.class);
@SuppressWarnings("FieldMayBeFinal") // allow setting via reflection.
private static Log logger = LogFactory.getLog(SecretLeaseContainer.class);
private final List<RequestedSecret> requestedSecrets = new CopyOnWriteArrayList<>();
@@ -608,14 +609,14 @@ public class SecretLeaseContainer extends SecretLeaseEventPublisher implements I
private static void logRenewalCandidate(RequestedSecret requestedSecret, Lease lease, String action) {
if (log.isDebugEnabled()) {
if (logger.isDebugEnabled()) {
if (lease.hasLeaseId()) {
log.debug(String.format("Secret %s with Lease %s qualified for %s", requestedSecret.getPath(),
logger.debug(String.format("Secret %s with Lease %s qualified for %s", requestedSecret.getPath(),
lease.getLeaseId(), action));
}
else {
log.debug(String.format("Secret %s with cache hint is qualified for %s", requestedSecret.getPath(),
logger.debug(String.format("Secret %s with cache hint is qualified for %s", requestedSecret.getPath(),
action));
}
}
@@ -784,7 +785,8 @@ public class SecretLeaseContainer extends SecretLeaseEventPublisher implements I
*/
static class LeaseRenewalScheduler {
private static final Log log = org.apache.commons.logging.LogFactory.getLog(LeaseRenewalScheduler.class);
@SuppressWarnings("FieldMayBeFinal") // allow setting via reflection.
private static Log logger = LogFactory.getLog(LeaseRenewalScheduler.class);
private final TaskScheduler taskScheduler;
@@ -812,13 +814,13 @@ public class SecretLeaseContainer extends SecretLeaseEventPublisher implements I
void scheduleRenewal(RequestedSecret requestedSecret, RenewLease renewLease, Lease lease, Duration minRenewal,
Duration expiryThreshold) {
if (log.isDebugEnabled()) {
if (logger.isDebugEnabled()) {
if (lease.hasLeaseId()) {
log.debug(String.format("Scheduling renewal for secret %s with lease %s, lease duration %d",
logger.debug(String.format("Scheduling renewal for secret %s with lease %s, lease duration %d",
requestedSecret.getPath(), lease.getLeaseId(), lease.getLeaseDuration().getSeconds()));
}
else {
log.debug(String.format("Scheduling renewal for secret %s, with cache hint duration %d",
logger.debug(String.format("Scheduling renewal for secret %s, with cache hint duration %d",
requestedSecret.getPath(), lease.getLeaseDuration().getSeconds()));
}
}
@@ -838,17 +840,17 @@ public class SecretLeaseContainer extends SecretLeaseEventPublisher implements I
LeaseRenewalScheduler.this.schedules.remove(lease);
if (LeaseRenewalScheduler.this.currentLeaseRef.get() != lease) {
log.debug("Current lease has changed. Skipping renewal");
logger.debug("Current lease has changed. Skipping renewal");
return;
}
if (log.isDebugEnabled()) {
if (logger.isDebugEnabled()) {
if (lease.hasLeaseId()) {
log.debug(String.format("Renewing lease %s for secret %s", lease.getLeaseId(),
logger.debug(String.format("Renewing lease %s for secret %s", lease.getLeaseId(),
requestedSecret.getPath()));
}
else {
log.debug(String.format("Renewing secret without lease %s", requestedSecret.getPath()));
logger.debug(String.format("Renewing secret without lease %s", requestedSecret.getPath()));
}
}
@@ -860,7 +862,7 @@ public class SecretLeaseContainer extends SecretLeaseEventPublisher implements I
LeaseRenewalScheduler.this.currentLeaseRef.compareAndSet(lease, renewLease.renewLease(lease));
}
catch (Exception e) {
log.error(String.format("Cannot renew lease %s", lease.getLeaseId()), e);
logger.error(String.format("Cannot renew lease %s", lease.getLeaseId()), e);
}
}
};
@@ -876,8 +878,8 @@ public class SecretLeaseContainer extends SecretLeaseEventPublisher implements I
ScheduledFuture<?> scheduledFuture = this.schedules.get(lease);
if (scheduledFuture != null) {
if (log.isDebugEnabled()) {
log.debug(
if (logger.isDebugEnabled()) {
logger.debug(
String.format("Canceling previously registered schedule for lease %s", lease.getLeaseId()));
}

View File

@@ -224,11 +224,13 @@ public class SecretLeaseEventPublisher implements InitializingBean {
INSTANCE;
private static final Log log = LogFactory.getLog(LoggingErrorListener.class);
@SuppressWarnings("FieldMayBeFinal") // allow setting via reflection.
private static Log logger = LogFactory.getLog(LoggingErrorListener.class);
@Override
public void onLeaseError(SecretLeaseEvent leaseEvent, Exception exception) {
log.warn(String.format("[%s] %s %s", leaseEvent.getSource(), leaseEvent.getLease(), exception.getMessage()),
logger.warn(
String.format("[%s] %s %s", leaseEvent.getSource(), leaseEvent.getLease(), exception.getMessage()),
exception);
}