From 287eaa1e6ab97939a2b8089556ad64917cc7be07 Mon Sep 17 00:00:00 2001 From: Karthik Ranganathan Date: Tue, 5 Feb 2013 15:42:41 -0800 Subject: [PATCH] Fixed a problem where log4j was getting initialized during archaius initialization under a different factory than blitz4j - because archaius starts up before blitz4j configures everything. --- .../netflix/blitz4j/LoggingConfiguration.java | 86 ++++++++++--------- .../logging/messaging/MessageBatcher.java | 13 +-- 2 files changed, 53 insertions(+), 46 deletions(-) diff --git a/src/main/java/com/netflix/blitz4j/LoggingConfiguration.java b/src/main/java/com/netflix/blitz4j/LoggingConfiguration.java index d9bbcf3..c7c9b3b 100644 --- a/src/main/java/com/netflix/blitz4j/LoggingConfiguration.java +++ b/src/main/java/com/netflix/blitz4j/LoggingConfiguration.java @@ -24,6 +24,7 @@ import java.util.HashMap; import java.util.Map; import java.util.Properties; +import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ExecutorService; import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.SynchronousQueue; @@ -101,7 +102,7 @@ private LoggingConfiguration() { .setDaemon(false).setNameFormat("DynamicLog4jListener").build(); this.executorPool = new ThreadPoolExecutor(0, 1, 15 * 60, - TimeUnit.SECONDS, new SynchronousQueue(), threadFactory); + TimeUnit.SECONDS, new ArrayBlockingQueue(100), threadFactory); } /** @@ -122,16 +123,50 @@ public void configure(Properties props) { // First try to load the log4j configuration file from the classpath String log4jConfigurationFile = System .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) { loadLog4jConfigurationFile(log4jConfigurationFile); - + // First configure without async so that we can capture the output + // of dependent libraries + PropertyConfigurator.configure(this.props); } - else if (blitz4jConfig.shouldLoadLog4jPropertiesFromClassPath()) { - - InputStream in = null; + + this.blitz4jConfig = new DefaultBlitz4jConfig(props); + + if ((log4jConfigurationFile == null) + && (blitz4jConfig.shouldLoadLog4jPropertiesFromClassPath())) { + + InputStream in = null; try { URL url = Loader.getResource(LOG4J_PROPERTIES); if (url != null) { @@ -139,7 +174,7 @@ else if (blitz4jConfig.shouldLoadLog4jPropertiesFromClassPath()) { this.props.load(in); } } catch (Throwable t) { - + } finally { if (in != null) { @@ -150,7 +185,7 @@ else if (blitz4jConfig.shouldLoadLog4jPropertiesFromClassPath()) { } } } - + } if (props != null) { Enumeration enumeration = props.propertyNames(); @@ -161,38 +196,7 @@ else if (blitz4jConfig.shouldLoadLog4jPropertiesFromClassPath()) { } } 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(); if (asyncAppenderArray == null) { diff --git a/src/main/java/com/netflix/logging/messaging/MessageBatcher.java b/src/main/java/com/netflix/logging/messaging/MessageBatcher.java index 497807a..f2fe192 100644 --- a/src/main/java/com/netflix/logging/messaging/MessageBatcher.java +++ b/src/main/java/com/netflix/logging/messaging/MessageBatcher.java @@ -185,12 +185,15 @@ public synchronized void setBatchMaxDelay(double maxDelaySec) { maxDelay = (long) (maxDelaySec * 1000000000); } - /** Set the number of threads that process batches. */ - void setProcessorMaxThreads(int threads) { - if (processor.getCorePoolSize() > threads) { - processor.setCorePoolSize(threads); + /** + * Set the max threads for the processors + * @param maxThreads - max threads that can be launched for processing + */ + public void setProcessorMaxThreads(int maxThreads) { + if (processor.getCorePoolSize() > maxThreads) { + processor.setCorePoolSize(maxThreads); } - processor.setMaximumPoolSize(threads); + processor.setMaximumPoolSize(maxThreads); } /**