Included is a patch for log4j 1.2.8. The patch is for two things.
1) log4j 1.2.8 PropertyConfigurator & DOMConfigurator both have a small bug in the
void doConfigure(URL url, LoggerRepository repository) methods. They open a stream on
the URL, but do not close it. This means the file cannot be edited while the
application is running because log4j has it locked. This is easy to fix by closing
the stream when log4j has finished reading it.
2) Added a new method to LogManager called refreshConfiguration(). This method will
call a newly modified Hierarchy.resetConfiguration. The old signature was
resetConfiguration() and the new one is
resetConfiguration(boolean warnNoEmit). If the parameter is true, log4j will emit a
"Please initialize the log4j system properly." message if any thread attempts to log
before the configuration is reloaded. If false, the message will never be output.
The LogManager.refreshConfiguration() will then re-load the configuration file that
was used when the app first started.
Log messages that occur between the reset and reload are lost. If the new
configuration file fails, log4j will output a message to stdout, and no logging will
take place until the error in the file is corrected and
LogManager.refreshConfiguration() is called again. If the file is ok at that point,
logging will resume using the the settings in the new configuration.
Let me know if this patch gets accepted, or other modifications to it are needed.
Later
Rob
Only in org/apache/log4j: Appender.java
Only in org/apache/log4j: AppenderSkeleton.java
Only in org/apache/log4j: AsyncAppender.java
Only in org/apache/log4j: BasicConfigurator.java
Only in org/apache/log4j: Category.java
Only in org/apache/log4j: CategoryKey.java
Only in org/apache/log4j: ConsoleAppender.java
Only in org/apache/log4j: DailyRollingFileAppender.java
Only in org/apache/log4j: DefaultCategoryFactory.java
Only in org/apache/log4j: FileAppender.java
Only in org/apache/log4j: HTMLLayout.java
diff -ur org/apache/log4j/Hierarchy.java patch/org/apache/log4j/Hierarchy.java
--- org/apache/log4j/Hierarchy.java 2003-02-19 20:07:38.000000000 -0500
+++ patch/org/apache/log4j/Hierarchy.java 2004-01-21 16:30:00.000000000 -0500
@@ -362,10 +362,16 @@
<p>This method should be used sparingly and with care as it will
block all logging until it is completed.</p>
+
+ @param warnNoEmit - if false do not output warning message about
+ no appenders. Useful when refreshing configuration.
@since 0.8.5 */
public
- void resetConfiguration() {
+ void resetConfiguration(boolean warnNoEmit) {
+
+ if(!warnNoEmit)
+ this.emittedNoAppenderWarning = true;
getRootLogger().setLevel((Level) Level.DEBUG);
root.setResourceBundle(null);
Only in org/apache/log4j: Layout.java
Only in org/apache/log4j: Level.java
diff -ur org/apache/log4j/LogManager.java patch/org/apache/log4j/LogManager.java
--- org/apache/log4j/LogManager.java 2003-02-19 20:07:40.000000000 -0500
+++ patch/org/apache/log4j/LogManager.java 2004-01-21 16:49:56.000000000 -0500
@@ -63,6 +63,9 @@
static private Object guard = null;
static private RepositorySelector repositorySelector;
+ static private URL url = null;
+ static private String configuratorClassName = null;
+ static private String configurationOptionStr = null;
static {
// By default we use a DefaultRepositorySelector which always returns 'h'.
@@ -77,16 +80,14 @@
// specified by the user or the default config file.
if(override == null || "false".equalsIgnoreCase(override)) {
- String configurationOptionStr = OptionConverter.getSystemProperty(
+ configurationOptionStr = OptionConverter.getSystemProperty(
DEFAULT_CONFIGURATION_KEY,
null);
- String configuratorClassName = OptionConverter.getSystemProperty(
+ configuratorClassName = OptionConverter.getSystemProperty(
CONFIGURATOR_CLASS_KEY,
null);
- URL url = null;
-
// if the user has not specified the log4j.configuration
// property, we search first for the file "log4j.xml" and then
// "log4j.properties"
@@ -105,16 +106,7 @@
}
}
- // If we have a non-null url, then delegate the rest of the
- // configuration to the OptionConverter.selectAndConfigure
- // method.
- if(url != null) {
- LogLog.debug("Using URL ["+url+"] for automatic log4j configuration.");
- OptionConverter.selectAndConfigure(url, configuratorClassName,
- LogManager.getLoggerRepository());
- } else {
- LogLog.debug("Could not find resource: ["+configurationOptionStr+"].");
- }
+ loadConfiguration();
}
}
@@ -221,7 +213,54 @@
public
static
void resetConfiguration() {
- repositorySelector.getLoggerRepository().resetConfiguration();
+ repositorySelector.getLoggerRepository().resetConfiguration(true);
+ }
+
+ /**
+ Refreshes the configuration from the file found during the
+ initialization process. First will dump all existing appenders, and
+ then create new ones as specified in the newly loaded configuration file.
+
+ <p>This method should be used sparingly as it <B>WILL LOSE</B> log messages.
+ and may block logging on other threads.</p>
+
+ <p> In the period of time between dumping existing appenders and creating
+ new ones log messages written by other threads will be lost.</p>
+
+ <p> If loading of the new configuration file fails log4j will output an
+ error message to stdout. Your application will continue to run, but will
+ not log any output. Fix the problem in the configuration file, and then
+ call refreshConfiguration again. If the configuration file is ok, logging
+ will begin again using the new configuration.
+ */
+ public
+ static
+ void refreshConfiguration() {
+ repositorySelector.getLoggerRepository().resetConfiguration(false);
+ loadConfiguration();
+ }
+
+ /**
+ Performs the final step of loading the configuration when using the
+ default initialization procedure. Can be called again later to reload
+ the configuration file. It will attempt to reload the same file that
+ was found during the first initialization search. i.e. the search will
+ NOT be performed again, only the file will be reloaded.
+
+ */
+ private
+ static
+ void loadConfiguration(){
+ // If we have a non-null url, then delegate the rest of the
+ // configuration to the OptionConverter.selectAndConfigure
+ // method.
+ if(url != null) {
+ LogLog.debug("Using URL ["+url+"] for automatic log4j configuration.");
+ OptionConverter.selectAndConfigure(url, configuratorClassName,
+ LogManager.getLoggerRepository());
+ } else {
+ LogLog.debug("Could not find resource: ["+configurationOptionStr+"].");
+ }
}
}
Only in org/apache/log4j: Logger.java
Only in org/apache/log4j: MDC.java
Only in org/apache/log4j: NDC.java
Only in org/apache/log4j: PatternLayout.java
Only in org/apache/log4j: Priority.java
diff -ur org/apache/log4j/PropertyConfigurator.java
patch/org/apache/log4j/PropertyConfigurator.java
--- org/apache/log4j/PropertyConfigurator.java 2003-02-19 20:07:32.000000000 -0500
+++ patch/org/apache/log4j/PropertyConfigurator.java 2004-01-21 16:23:00.000000000
-0500
@@ -28,6 +28,7 @@
import java.util.Enumeration;
import java.util.Properties;
import java.io.FileInputStream;
+import java.io.InputStream;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.Hashtable;
@@ -421,7 +422,9 @@
Properties props = new Properties();
LogLog.debug("Reading configuration from URL " + configURL);
try {
- props.load(configURL.openStream());
+ InputStream in = configURL.openStream();
+ props.load(in);
+ in.close();
}
catch (java.io.IOException e) {
LogLog.error("Could not read configuration file from URL [" + configURL
Only in org/apache/log4j: ProvisionNode.java
Only in org/apache/log4j: RollingFileAppender.java
Only in org/apache/log4j: SimpleLayout.java
Only in org/apache/log4j: TTCCLayout.java
Only in org/apache/log4j: WriterAppender.java
Only in org/apache/log4j: chainsaw
Only in org/apache/log4j: config
Only in org/apache/log4j: helpers
Only in org/apache/log4j: jdbc
Only in org/apache/log4j: jmx
Only in org/apache/log4j: lf5
Only in org/apache/log4j: net
Only in org/apache/log4j: nt
Only in org/apache/log4j: or
Only in org/apache/log4j: package.html
Only in org/apache/log4j: performance
Only in org/apache/log4j/spi: AppenderAttachable.java
Only in org/apache/log4j/spi: Configurator.java
Only in org/apache/log4j/spi: DefaultRepositorySelector.java
Only in org/apache/log4j/spi: ErrorCode.java
Only in org/apache/log4j/spi: ErrorHandler.java
Only in org/apache/log4j/spi: Filter.java
Only in org/apache/log4j/spi: HierarchyEventListener.java
Only in org/apache/log4j/spi: LocationInfo.java
Only in org/apache/log4j/spi: LoggerFactory.java
diff -ur org/apache/log4j/spi/LoggerRepository.java
patch/org/apache/log4j/spi/LoggerRepository.java
--- org/apache/log4j/spi/LoggerRepository.java 2003-02-19 20:07:38.000000000 -0500
+++ patch/org/apache/log4j/spi/LoggerRepository.java 2004-01-21 16:05:02.000000000
-0500
@@ -90,6 +90,6 @@
public
abstract
- void resetConfiguration();
+ void resetConfiguration(boolean warnNoEmit);
}
Only in org/apache/log4j/spi: LoggingEvent.java
Only in org/apache/log4j/spi: OptionHandler.java
Only in org/apache/log4j/spi: RendererSupport.java
Only in org/apache/log4j/spi: RepositorySelector.java
Only in org/apache/log4j/spi: RootCategory.java
Only in org/apache/log4j/spi: ThrowableInformation.java
Only in org/apache/log4j/spi: TriggeringEventEvaluator.java
Only in org/apache/log4j/spi: package.html
Only in org/apache/log4j: varia
diff -ur org/apache/log4j/xml/DOMConfigurator.java
patch/org/apache/log4j/xml/DOMConfigurator.java
--- org/apache/log4j/xml/DOMConfigurator.java 2003-02-19 20:07:40.000000000 -0500
+++ patch/org/apache/log4j/xml/DOMConfigurator.java 2004-01-21 16:44:47.000000000
-0500
@@ -9,8 +9,6 @@
import java.util.*;
-import java.net.URL;
-
import org.w3c.dom.*;
import java.lang.reflect.Method;
import org.apache.log4j.*;
@@ -599,7 +597,9 @@
public
void doConfigure(URL url, LoggerRepository repository) {
try {
- doConfigure(url.openStream(), repository);
+ InputStream in = url.openStream();
+ doConfigure(in, repository);
+ in.close();
} catch(IOException e) {
LogLog.error("Could not open ["+url+"].", e);
}
Only in org/apache/log4j/xml: Log4jEntityResolver.java
Only in org/apache/log4j/xml: SAXErrorHandler.java
Only in org/apache/log4j/xml: XMLLayout.java
Only in org/apache/log4j/xml: examples
Only in org/apache/log4j/xml: log4j.dtd
Only in org/apache/log4j/xml: package.html
Only in org/apache/log4j/xml: test
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]