From 0f281bc9e18d56f7ae707f810b88d8d5e4e829ad Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Thu, 4 Dec 2014 18:00:38 +0000 Subject: [PATCH] Initialize log4j to prevent blitz4j from freaking out --- .../EurekaServerInitializerConfiguration.java | 47 +++++++++++++------ .../server/Log4JLoggingSystemTests.java | 13 +---- 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/EurekaServerInitializerConfiguration.java b/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/EurekaServerInitializerConfiguration.java index 5dda2ed4..c60c4790 100644 --- a/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/EurekaServerInitializerConfiguration.java +++ b/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/EurekaServerInitializerConfiguration.java @@ -15,6 +15,7 @@ */ package org.springframework.cloud.netflix.eureka.server; +import java.io.IOException; import java.lang.reflect.Field; import java.lang.reflect.Modifier; @@ -30,6 +31,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.logging.log4j.Log4JLoggingSystem; import org.springframework.cloud.netflix.eureka.DataCenterAwareMarshallingStrategy; import org.springframework.cloud.netflix.eureka.DiscoveryManagerInitializer; import org.springframework.cloud.netflix.eureka.EurekaServerConfigBean; @@ -44,6 +46,7 @@ import org.springframework.context.SmartLifecycle; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.Ordered; +import org.springframework.core.io.ClassPathResource; import org.springframework.util.ReflectionUtils; import org.springframework.web.context.ServletContextAware; @@ -84,27 +87,40 @@ public class EurekaServerInitializerConfiguration implements ServletContextAware this.servletContext = servletContext; } - @Bean - @ConditionalOnMissingBean(DiscoveryManagerInitializer.class) - public DiscoveryManagerInitializer discoveryManagerIntitializer() { - return new DiscoveryManagerInitializer(); - } + @Bean + @ConditionalOnMissingBean(DiscoveryManagerInitializer.class) + public DiscoveryManagerInitializer discoveryManagerIntitializer() { + return new DiscoveryManagerInitializer(); + } @Override public void start() { - discoveryManagerIntitializer().init(); - new Thread(new Runnable() { + discoveryManagerIntitializer().init(); + new Thread(new Runnable() { @Override public void run() { try { new EurekaBootStrap() { @Override protected void initEurekaEnvironment() { + try { + if (System.getProperty("log4j.configuration") == null) { + System.setProperty("log4j.configuration", + new ClassPathResource("log4j.properties", + Log4JLoggingSystem.class).getURL() + .toString()); + } + } + catch (IOException e) { + // ignore + } LoggingConfiguration.getInstance().configure(); EurekaServerConfigurationManager.getInstance() .setConfiguration(eurekaServerConfig); - XmlXStream.getInstance().setMarshallingStrategy(new DataCenterAwareMarshallingStrategy()); - JsonXStream.getInstance().setMarshallingStrategy(new DataCenterAwareMarshallingStrategy()); + XmlXStream.getInstance().setMarshallingStrategy( + new DataCenterAwareMarshallingStrategy()); + JsonXStream.getInstance().setMarshallingStrategy( + new DataCenterAwareMarshallingStrategy()); // PeerAwareInstanceRegistry.getInstance(); applicationContext .publishEvent(new EurekaRegistryAvailableEvent( @@ -229,12 +245,13 @@ public class EurekaServerInitializerConfiguration implements ServletContextAware } /** - * Additional aspect for intercepting method invocations on PeerAwareInstanceRegistry. - * If {@link PeerAwareInstanceRegistry#openForTraffic(int)} is called with a zero + * Additional aspect for intercepting method invocations on + * PeerAwareInstanceRegistry. If + * {@link PeerAwareInstanceRegistry#openForTraffic(int)} is called with a zero * argument, it means that leases are not automatically cancelled if the instance - * hasn't sent any renewals recently. This happens for a standalone server. It seems - * like a bad default, so we set it to the smallest non-zero value we can, so that any - * instances that subsequently register can bump up the threshold. + * hasn't sent any renewals recently. This happens for a standalone server. It + * seems like a bad default, so we set it to the smallest non-zero value we can, + * so that any instances that subsequently register can bump up the threshold. * * @author Dave Syer * @@ -246,7 +263,7 @@ public class EurekaServerInitializerConfiguration implements ServletContextAware if ("openForTraffic".equals(invocation.getMethod().getName())) { int count = (int) invocation.getArguments()[0]; ReflectionUtils.invokeMethod(invocation.getMethod(), - invocation.getThis(), count==0 ? 1 : count); + invocation.getThis(), count == 0 ? 1 : count); return null; } return invocation.proceed(); diff --git a/spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/Log4JLoggingSystemTests.java b/spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/Log4JLoggingSystemTests.java index 3468db7f..25c8c92c 100644 --- a/spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/Log4JLoggingSystemTests.java +++ b/spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/Log4JLoggingSystemTests.java @@ -50,8 +50,8 @@ public class Log4JLoggingSystemTests { private Logger logger; @Before - public void setup() { - System.setProperty("log4j.configuration", getPackagedConfigFile("log4j.properties")); + public void setup() throws IOException { + System.setProperty("log4j.configuration", new ClassPathResource("log4j.properties", Log4JLoggingSystem.class).getURL().toString()); logger = Logger.getLogger(getClass()); LoggingConfiguration.getInstance().configure(); } @@ -68,13 +68,4 @@ public class Log4JLoggingSystemTests { equalTo(1)); } - - private final String getPackagedConfigFile(String fileName) { - try { - return new ClassPathResource(fileName, Log4JLoggingSystem.class).getURL().toString(); - } - catch (IOException e) { - throw new IllegalStateException("Cannot create URL", e); - } - } }