Skip to content

Commit

Permalink
Fixed a problem where log4j was getting initialized during archaius i…
Browse files Browse the repository at this point in the history
…nitialization under a different factory than blitz4j - because archaius starts up before blitz4j configures everything.
  • Loading branch information
Karthik Ranganathan committed Feb 5, 2013
1 parent 79f01a7 commit 287eaa1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 46 deletions.
86 changes: 45 additions & 41 deletions src/main/java/com/netflix/blitz4j/LoggingConfiguration.java
Expand Up @@ -24,6 +24,7 @@
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.SynchronousQueue; import java.util.concurrent.SynchronousQueue;
Expand Down Expand Up @@ -101,7 +102,7 @@ private LoggingConfiguration() {
.setDaemon(false).setNameFormat("DynamicLog4jListener").build(); .setDaemon(false).setNameFormat("DynamicLog4jListener").build();


this.executorPool = new ThreadPoolExecutor(0, 1, 15 * 60, this.executorPool = new ThreadPoolExecutor(0, 1, 15 * 60,
TimeUnit.SECONDS, new SynchronousQueue(), threadFactory); TimeUnit.SECONDS, new ArrayBlockingQueue(100), threadFactory);
} }


/** /**
Expand All @@ -122,24 +123,58 @@ public void configure(Properties props) {
// First try to load the log4j configuration file from the classpath // First try to load the log4j configuration file from the classpath
String log4jConfigurationFile = System String log4jConfigurationFile = System
.getProperty(PROP_LOG4J_CONFIGURATION); .getProperty(PROP_LOG4J_CONFIGURATION);

this.blitz4jConfig = new DefaultBlitz4jConfig(props);


NFHierarchy nfHierarchy = null;
// Make log4j use blitz4j implementations
if ((!NFHierarchy.class.equals(LogManager.getLoggerRepository()
.getClass()))) {
nfHierarchy = new NFHierarchy(new NFRootLogger(
org.apache.log4j.Level.INFO));
org.apache.log4j.LogManager.setRepositorySelector(
new NFRepositorySelector(nfHierarchy), guard);
}
String log4jLoggerFactory = System
.getProperty(PROP_LOG4J_LOGGER_FACTORY);
if (log4jLoggerFactory != null) {
this.props.setProperty(PROP_LOG4J_LOGGER_FACTORY,
log4jLoggerFactory);
if (nfHierarchy != null) {
try {
LoggerFactory loggerFactory = (LoggerFactory) Class
.forName(log4jLoggerFactory).newInstance();
nfHierarchy.setLoggerFactory(loggerFactory);
} catch (Throwable e) {
System.err
.println("Cannot set the logger factory. Hence reverting to default.");
e.printStackTrace();
}
}
} else {
this.props.setProperty(PROP_LOG4J_LOGGER_FACTORY,
BLITZ_LOGGER_FACTORY);

}
if (log4jConfigurationFile != null) { if (log4jConfigurationFile != null) {
loadLog4jConfigurationFile(log4jConfigurationFile); loadLog4jConfigurationFile(log4jConfigurationFile);

// First configure without async so that we can capture the output
// of dependent libraries
PropertyConfigurator.configure(this.props);
} }
else if (blitz4jConfig.shouldLoadLog4jPropertiesFromClassPath()) {

this.blitz4jConfig = new DefaultBlitz4jConfig(props);
InputStream in = null;
if ((log4jConfigurationFile == null)
&& (blitz4jConfig.shouldLoadLog4jPropertiesFromClassPath())) {

InputStream in = null;
try { try {
URL url = Loader.getResource(LOG4J_PROPERTIES); URL url = Loader.getResource(LOG4J_PROPERTIES);
if (url != null) { if (url != null) {
in = url.openStream(); in = url.openStream();
this.props.load(in); this.props.load(in);
} }
} catch (Throwable t) { } catch (Throwable t) {

} finally { } finally {


if (in != null) { if (in != null) {
Expand All @@ -150,7 +185,7 @@ else if (blitz4jConfig.shouldLoadLog4jPropertiesFromClassPath()) {
} }
} }
} }

} }
if (props != null) { if (props != null) {
Enumeration enumeration = props.propertyNames(); Enumeration enumeration = props.propertyNames();
Expand All @@ -161,38 +196,7 @@ else if (blitz4jConfig.shouldLoadLog4jPropertiesFromClassPath()) {
} }
} }
this.blitz4jConfig = new DefaultBlitz4jConfig(this.props); this.blitz4jConfig = new DefaultBlitz4jConfig(this.props);
NFHierarchy nfHierarchy = null;
// Make log4j use blitz4j implementations
if (blitz4jConfig.shouldUseLockFree()
&& (!NFHierarchy.class.equals(LogManager.getLoggerRepository()
.getClass()))) {
nfHierarchy = new NFHierarchy(new NFRootLogger(
org.apache.log4j.Level.INFO));
org.apache.log4j.LogManager.setRepositorySelector(
new NFRepositorySelector(nfHierarchy), guard);
}
String log4jLoggerFactory = System
.getProperty(PROP_LOG4J_LOGGER_FACTORY);
if (log4jLoggerFactory != null) {
this.props.setProperty(PROP_LOG4J_LOGGER_FACTORY,
log4jLoggerFactory);
if (nfHierarchy != null) {
try {
LoggerFactory loggerFactory = (LoggerFactory) Class
.forName(log4jLoggerFactory).newInstance();
nfHierarchy.setLoggerFactory(loggerFactory);
} catch (Throwable e) {
System.err
.println("Cannot set the logger factory. Hence reverting to default.");
e.printStackTrace();
}
}
} else {
if (blitz4jConfig.shouldUseLockFree()) {
this.props.setProperty(PROP_LOG4J_LOGGER_FACTORY,
BLITZ_LOGGER_FACTORY);
}
}


String[] asyncAppenderArray = blitz4jConfig.getAsyncAppenders(); String[] asyncAppenderArray = blitz4jConfig.getAsyncAppenders();
if (asyncAppenderArray == null) { if (asyncAppenderArray == null) {
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/com/netflix/logging/messaging/MessageBatcher.java
Expand Up @@ -185,12 +185,15 @@ public synchronized void setBatchMaxDelay(double maxDelaySec) {
maxDelay = (long) (maxDelaySec * 1000000000); maxDelay = (long) (maxDelaySec * 1000000000);
} }


/** Set the number of threads that process batches. */ /**
void setProcessorMaxThreads(int threads) { * Set the max threads for the processors
if (processor.getCorePoolSize() > threads) { * @param maxThreads - max threads that can be launched for processing
processor.setCorePoolSize(threads); */
public void setProcessorMaxThreads(int maxThreads) {
if (processor.getCorePoolSize() > maxThreads) {
processor.setCorePoolSize(maxThreads);
} }
processor.setMaximumPoolSize(threads); processor.setMaximumPoolSize(maxThreads);
} }


/** /**
Expand Down

0 comments on commit 287eaa1

Please sign in to comment.