Skip to content

Commit f1e741c

Browse files
committed
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.
1 parent e0bea23 commit f1e741c

File tree

6 files changed

+29
-22
lines changed

6 files changed

+29
-22
lines changed

spring-vault-core/src/main/java/org/springframework/vault/authentication/MacAddressUserId.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
*/
4242
public class MacAddressUserId implements AppIdUserIdMechanism {
4343

44-
private final Log log = LogFactory.getLog(MacAddressUserId.class);
44+
private final Log logger = LogFactory.getLog(MacAddressUserId.class);
4545

4646
private final String networkInterfaceHint;
4747

@@ -100,7 +100,7 @@ public String createUserId() {
100100
if (!networkInterface.isPresent()) {
101101

102102
if (StringUtils.hasText(this.networkInterfaceHint)) {
103-
this.log.warn(String.format("Did not find a NetworkInterface applying hint %s",
103+
this.logger.warn(String.format("Did not find a NetworkInterface applying hint %s",
104104
this.networkInterfaceHint));
105105
}
106106

spring-vault-core/src/main/java/org/springframework/vault/client/ClientHttpRequestFactoryFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@
8383
*/
8484
public class ClientHttpRequestFactoryFactory {
8585

86-
private static final Log logger = LogFactory.getLog(ClientHttpRequestFactoryFactory.class);
86+
@SuppressWarnings("FieldMayBeFinal") // allow setting via reflection.
87+
private static Log logger = LogFactory.getLog(ClientHttpRequestFactoryFactory.class);
8788

8889
private static final boolean HTTP_COMPONENTS_PRESENT = isPresent("org.apache.http.client.HttpClient");
8990

spring-vault-core/src/main/java/org/springframework/vault/core/env/LeaseAwareVaultPropertySource.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@
5656
*/
5757
public class LeaseAwareVaultPropertySource extends EnumerablePropertySource<VaultOperations> {
5858

59-
private static final Log logger = LogFactory.getLog(LeaseAwareVaultPropertySource.class);
59+
@SuppressWarnings("FieldMayBeFinal") // allow setting via reflection.
60+
private static Log logger = LogFactory.getLog(LeaseAwareVaultPropertySource.class);
6061

6162
private final SecretLeaseContainer secretLeaseContainer;
6263

spring-vault-core/src/main/java/org/springframework/vault/core/env/VaultPropertySource.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
*/
4848
public class VaultPropertySource extends EnumerablePropertySource<VaultOperations> {
4949

50-
private static final Log logger = LogFactory.getLog(VaultPropertySource.class);
50+
@SuppressWarnings("FieldMayBeFinal") // allow setting via reflection.
51+
private static Log logger = LogFactory.getLog(VaultPropertySource.class);
5152

5253
private final String path;
5354

spring-vault-core/src/main/java/org/springframework/vault/core/lease/SecretLeaseContainer.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ public class SecretLeaseContainer extends SecretLeaseEventPublisher implements I
132132

133133
private static final int STATUS_DESTROYED = 2;
134134

135-
private static final Log log = LogFactory.getLog(SecretLeaseContainer.class);
135+
@SuppressWarnings("FieldMayBeFinal") // allow setting via reflection.
136+
private static Log logger = LogFactory.getLog(SecretLeaseContainer.class);
136137

137138
private final List<RequestedSecret> requestedSecrets = new CopyOnWriteArrayList<>();
138139

@@ -608,14 +609,14 @@ private LeaseRenewalScheduler getRenewalSchedulder(RequestedSecret secret) {
608609

609610
private static void logRenewalCandidate(RequestedSecret requestedSecret, Lease lease, String action) {
610611

611-
if (log.isDebugEnabled()) {
612+
if (logger.isDebugEnabled()) {
612613

613614
if (lease.hasLeaseId()) {
614-
log.debug(String.format("Secret %s with Lease %s qualified for %s", requestedSecret.getPath(),
615+
logger.debug(String.format("Secret %s with Lease %s qualified for %s", requestedSecret.getPath(),
615616
lease.getLeaseId(), action));
616617
}
617618
else {
618-
log.debug(String.format("Secret %s with cache hint is qualified for %s", requestedSecret.getPath(),
619+
logger.debug(String.format("Secret %s with cache hint is qualified for %s", requestedSecret.getPath(),
619620
action));
620621
}
621622
}
@@ -784,7 +785,8 @@ protected void doRevokeLease(RequestedSecret requestedSecret, Lease lease) {
784785
*/
785786
static class LeaseRenewalScheduler {
786787

787-
private static final Log log = org.apache.commons.logging.LogFactory.getLog(LeaseRenewalScheduler.class);
788+
@SuppressWarnings("FieldMayBeFinal") // allow setting via reflection.
789+
private static Log logger = LogFactory.getLog(LeaseRenewalScheduler.class);
788790

789791
private final TaskScheduler taskScheduler;
790792

@@ -812,13 +814,13 @@ static class LeaseRenewalScheduler {
812814
void scheduleRenewal(RequestedSecret requestedSecret, RenewLease renewLease, Lease lease, Duration minRenewal,
813815
Duration expiryThreshold) {
814816

815-
if (log.isDebugEnabled()) {
817+
if (logger.isDebugEnabled()) {
816818
if (lease.hasLeaseId()) {
817-
log.debug(String.format("Scheduling renewal for secret %s with lease %s, lease duration %d",
819+
logger.debug(String.format("Scheduling renewal for secret %s with lease %s, lease duration %d",
818820
requestedSecret.getPath(), lease.getLeaseId(), lease.getLeaseDuration().getSeconds()));
819821
}
820822
else {
821-
log.debug(String.format("Scheduling renewal for secret %s, with cache hint duration %d",
823+
logger.debug(String.format("Scheduling renewal for secret %s, with cache hint duration %d",
822824
requestedSecret.getPath(), lease.getLeaseDuration().getSeconds()));
823825
}
824826
}
@@ -838,17 +840,17 @@ public void run() {
838840
LeaseRenewalScheduler.this.schedules.remove(lease);
839841

840842
if (LeaseRenewalScheduler.this.currentLeaseRef.get() != lease) {
841-
log.debug("Current lease has changed. Skipping renewal");
843+
logger.debug("Current lease has changed. Skipping renewal");
842844
return;
843845
}
844846

845-
if (log.isDebugEnabled()) {
847+
if (logger.isDebugEnabled()) {
846848
if (lease.hasLeaseId()) {
847-
log.debug(String.format("Renewing lease %s for secret %s", lease.getLeaseId(),
849+
logger.debug(String.format("Renewing lease %s for secret %s", lease.getLeaseId(),
848850
requestedSecret.getPath()));
849851
}
850852
else {
851-
log.debug(String.format("Renewing secret without lease %s", requestedSecret.getPath()));
853+
logger.debug(String.format("Renewing secret without lease %s", requestedSecret.getPath()));
852854
}
853855
}
854856

@@ -860,7 +862,7 @@ public void run() {
860862
LeaseRenewalScheduler.this.currentLeaseRef.compareAndSet(lease, renewLease.renewLease(lease));
861863
}
862864
catch (Exception e) {
863-
log.error(String.format("Cannot renew lease %s", lease.getLeaseId()), e);
865+
logger.error(String.format("Cannot renew lease %s", lease.getLeaseId()), e);
864866
}
865867
}
866868
};
@@ -876,8 +878,8 @@ private void cancelSchedule(Lease lease) {
876878
ScheduledFuture<?> scheduledFuture = this.schedules.get(lease);
877879
if (scheduledFuture != null) {
878880

879-
if (log.isDebugEnabled()) {
880-
log.debug(
881+
if (logger.isDebugEnabled()) {
882+
logger.debug(
881883
String.format("Canceling previously registered schedule for lease %s", lease.getLeaseId()));
882884
}
883885

spring-vault-core/src/main/java/org/springframework/vault/core/lease/SecretLeaseEventPublisher.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,13 @@ public enum LoggingErrorListener implements LeaseErrorListener {
224224

225225
INSTANCE;
226226

227-
private static final Log log = LogFactory.getLog(LoggingErrorListener.class);
227+
@SuppressWarnings("FieldMayBeFinal") // allow setting via reflection.
228+
private static Log logger = LogFactory.getLog(LoggingErrorListener.class);
228229

229230
@Override
230231
public void onLeaseError(SecretLeaseEvent leaseEvent, Exception exception) {
231-
log.warn(String.format("[%s] %s %s", leaseEvent.getSource(), leaseEvent.getLease(), exception.getMessage()),
232+
logger.warn(
233+
String.format("[%s] %s %s", leaseEvent.getSource(), leaseEvent.getLease(), exception.getMessage()),
232234
exception);
233235
}
234236

0 commit comments

Comments
 (0)