You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by ce...@apache.org on 2005/01/07 21:47:38 UTC
cvs commit: logging-log4j/src/java/org/apache/log4j/selector ContextJNDISelector.java
ceki 2005/01/07 12:47:38
Modified: src/java/org/apache/log4j/spi DefaultRepositorySelector.java
RepositorySelector.java
src/java/org/apache/log4j LogManager.java
src/java/org/apache/log4j/selector ContextJNDISelector.java
Log:
- Changing the order of doing things in the static initializer of LogManager (changes abundantly commented in the code).
- Simplified the LoggerRepository interface. The getDefaultLR and setDefaultLR methods are no longer needed.
(This is still untested work in progress.)
Revision Changes Path
1.9 +3 -14 logging-log4j/src/java/org/apache/log4j/spi/DefaultRepositorySelector.java
Index: DefaultRepositorySelector.java
===================================================================
RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/spi/DefaultRepositorySelector.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- DefaultRepositorySelector.java 30 Nov 2004 18:52:17 -0000 1.8
+++ DefaultRepositorySelector.java 7 Jan 2005 20:47:38 -0000 1.9
@@ -19,32 +19,21 @@
public class DefaultRepositorySelector implements RepositorySelector {
LoggerRepository repository;
- public DefaultRepositorySelector() {
+ public DefaultRepositorySelector(LoggerRepository repository) {
+ this.repository = repository;
}
public LoggerRepository getLoggerRepository() {
return repository;
}
- public LoggerRepository getDefaultRepository() {
- return repository ;
- }
-
- public void setDefaultRepository(LoggerRepository dr) {
- if(repository == null) {
- repository = dr;
- } else {
- throw new IllegalStateException("default repository already set.");
- }
- }
-
/**
* Does nothing.
*
* @return Always null
*/
public LoggerRepository detachRepository(String contextName) {
- // do nothing as the default reposiory cannot be removed
+ // do nothing, as the default repository cannot be removed
return null;
}
}
1.12 +20 -37 logging-log4j/src/java/org/apache/log4j/spi/RepositorySelector.java
Index: RepositorySelector.java
===================================================================
RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/spi/RepositorySelector.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- RepositorySelector.java 4 Dec 2004 07:47:04 -0000 1.11
+++ RepositorySelector.java 7 Jan 2005 20:47:38 -0000 1.12
@@ -18,56 +18,39 @@
/**
-
- The <code>LogManager</code> uses one (and only one)
- <code>RepositorySelector</code> implementation to select the
- {@link LoggerRepository} for a particular application context.
-
- <p>It is the responsability of the <code>RepositorySelector</code>
- implementation to track the application context. Log4j makes no
- assumptions about the application context or on its management.
-
- <p>See also {@link org.apache.log4j.LogManager LogManager}.
-
- @author Ceki Gülcü
- @since 1.2
-
- */
+ * The <code>LogManager</code> uses one (and only one)
+ * <code>RepositorySelector</code> implementation to select the
+ * {@link LoggerRepository} for a particular application context.
+ *
+ * <p>It is the responsability of the <code>RepositorySelector</code>
+ * implementation to track the application context. Log4j makes no assumptions
+ * about the application context or on its management.
+ *
+ * <p>See also {@link org.apache.log4j.LogManager LogManager}.
+ *
+ * @author Ceki Gülcü
+ * @since 1.2
+ * */
public interface RepositorySelector {
/**
- Returns a {@link LoggerRepository} depending on the
- context. Implementors must make sure that a valid (non-null)
- LoggerRepository is returned.
+ * Returns a {@link LoggerRepository} depending on the context. Implementors
+ * must make sure that under all circumstances a valid (non-null)
+ * LoggerRepository is returned.
*/
public LoggerRepository getLoggerRepository();
/**
- * Sets the default repository
- * @since 1.3
- */
- public void setDefaultRepository(LoggerRepository def);
-
- /**
- * Gets the default repository. In the initial phases of configration, the
- * returned value may be null. However, after the RepositorySelector is
- * properly registered with LogManager, the returned value should never
- * be null.
- *
- * @since 1.3
- */
- public LoggerRepository getDefaultRepository();
-
- /**
* Remove the repository with the given context name from the list maintained
* by the respository selector.
*
- * When applications are stopped or recycled, this method should be called to
- * ensure that the associated repository is recycled as well.
+ * <p>When applications are stopped or recycled, this method should be called
+ * to ensure that the associated repository is recycled as well.
*
- * If more than one application share the same logging context, then the
+ * <p>If more than one application share the same logging context, then the
* applications need to coordinate their actions.
*
* @return The LoggerRepository instance that was detached.
+ * @since 1.3
*/
public LoggerRepository detachRepository(String contextName);
}
1.34 +45 -35 logging-log4j/src/java/org/apache/log4j/LogManager.java
Index: LogManager.java
===================================================================
RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/LogManager.java,v
retrieving revision 1.33
retrieving revision 1.34
diff -u -r1.33 -r1.34
--- LogManager.java 6 Jan 2005 23:16:50 -0000 1.33
+++ LogManager.java 7 Jan 2005 20:47:38 -0000 1.34
@@ -48,16 +48,56 @@
private static Scheduler schedulerInstance = null;
/**
+ * The default LoggerRepository instance created by LogManager. This instance
+ * is provided for the convenience of the {@link RepositorySelector} instance.
+ * The selector, if it choses, may ignore this default repository.
+ */
+ public final static LoggerRepository defaultLoggerRepository;
+
+ /**
* Log4j components resort to this instance of {@link SimpleLoggerFA} in case
* an appropriate LoggerRepository was not set or could not be found. It is
* used only in exceptional cases.
*/
public final static SimpleLoggerFA SIMPLE_LOGGER_FA = new SimpleLoggerFA();
+ // The following static initializer gets invoked immediately after a call to
+ // Logger.getLogger() is made. Here is a description of the static initializer.
+ //
+ // create defaultLoggerRepository
+ // configure(defaultLoggerRepository) depending on system properties
+ // during the configuration of defaultLoggerRepository a temporary repository
+ // selector is used.
+ //
+ //
static {
//System.out.println("**Start of LogManager static initializer");
- Hierarchy defaultHierarchy = new Hierarchy(new RootLogger(Level.DEBUG));
- defaultHierarchy.setName("default");
+ defaultLoggerRepository = new Hierarchy(new RootLogger(Level.DEBUG));
+ defaultLoggerRepository.setName("default");
+
+ // temporary repository
+ repositorySelector = new DefaultRepositorySelector(defaultLoggerRepository);
+
+ // Attempt to perform automatic configuration of the default repository
+ String configuratorClassName =
+ OptionConverter.getSystemProperty(Constants.CONFIGURATOR_CLASS_KEY, null);
+ String configurationOptionStr =
+ OptionConverter.getSystemProperty(Constants.DEFAULT_CONFIGURATION_KEY, null);
+
+ if (configurationOptionStr == null) {
+ if (Loader.getResource(Constants.DEFAULT_XML_CONFIGURATION_FILE) != null) {
+ configurationOptionStr = Constants.DEFAULT_XML_CONFIGURATION_FILE;
+ } else if (
+ Loader.getResource(Constants.DEFAULT_CONFIGURATION_FILE) != null) {
+ configurationOptionStr = Constants.DEFAULT_CONFIGURATION_FILE;
+ }
+ }
+
+ System.out.println("*** configurationOptionStr=" + configurationOptionStr);
+
+ IntializationUtil.initialConfiguration(
+ defaultLoggerRepository, configurationOptionStr, configuratorClassName);
+
OptionConverter oc = new OptionConverter();
// No point in setting the repository this early in the game
@@ -67,9 +107,7 @@
OptionConverter.getSystemProperty("log4j.repositorySelectorClass", null);
if (repositorySelectorStr == null) {
- // By default we use a DefaultRepositorySelector which always returns
- // the defaultHierarchy.
- repositorySelector = new DefaultRepositorySelector();
+ // NOTHING TO DO, the default repository has been configured already
} else if (repositorySelectorStr.equalsIgnoreCase("JNDI")) {
System.out.println("*** Will use ContextJNDISelector **");
repositorySelector = new ContextJNDISelector();
@@ -90,39 +128,11 @@
"*** Could not insantiate [" + repositorySelectorStr
+ "] as repository selector.");
System.out.println("*** Using default repository selector");
- repositorySelector = new DefaultRepositorySelector();
+ repositorySelector = new DefaultRepositorySelector(defaultLoggerRepository);
}
}
- // at this stage 'repositorySelector' should point to a valid selector.
- // Set the default repository for the selector, but only if it has not
- // have one already.
- if(repositorySelector.getDefaultRepository() == null) {
- repositorySelector.setDefaultRepository(defaultHierarchy);
- }
-
- // Attempt to perform automatic configuration of the default hierarchy
- String configuratorClassName =
- OptionConverter.getSystemProperty(Constants.CONFIGURATOR_CLASS_KEY, null);
- String configurationOptionStr =
- OptionConverter.getSystemProperty(Constants.DEFAULT_CONFIGURATION_KEY, null);
-
- if (configurationOptionStr == null) {
- if (Loader.getResource(Constants.DEFAULT_XML_CONFIGURATION_FILE) != null) {
- configurationOptionStr = Constants.DEFAULT_XML_CONFIGURATION_FILE;
- } else if (
- Loader.getResource(Constants.DEFAULT_CONFIGURATION_FILE) != null) {
- configurationOptionStr = Constants.DEFAULT_CONFIGURATION_FILE;
- }
- }
-
- System.out.println("*** configurationOptionStr=" + configurationOptionStr);
-
- IntializationUtil.initialConfiguration(
- defaultHierarchy, configurationOptionStr, configuratorClassName);
-
-
- //System.out.println("** End of LogManager static initializer");
+ System.out.println("** End of LogManager static initializer");
}
/**
1.16 +2 -20 logging-log4j/src/java/org/apache/log4j/selector/ContextJNDISelector.java
Index: ContextJNDISelector.java
===================================================================
RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/selector/ContextJNDISelector.java,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -r1.15 -r1.16
--- ContextJNDISelector.java 8 Dec 2004 14:28:25 -0000 1.15
+++ ContextJNDISelector.java 7 Jan 2005 20:47:38 -0000 1.16
@@ -18,6 +18,7 @@
import org.apache.log4j.Hierarchy;
import org.apache.log4j.Level;
+import org.apache.log4j.LogManager;
import org.apache.log4j.helpers.Constants;
import org.apache.log4j.helpers.IntializationUtil;
import org.apache.log4j.helpers.JNDIUtil;
@@ -130,31 +131,12 @@
private final Map hierMap;
/**
- * default hierarchy used in case the JNDI lookup
- * fails to return a non-null value
- */
- private LoggerRepository defaultRepository;
-
- /**
* public no-args constructor
*/
public ContextJNDISelector() {
hierMap = Collections.synchronizedMap(new HashMap());
}
- public void setDefaultRepository(LoggerRepository dh) {
- if (defaultRepository == null) {
- defaultRepository = dh;
- } else {
- throw new IllegalStateException(
- "default hierarchy has been already set.");
- }
- }
-
- public LoggerRepository getDefaultRepository() {
- return defaultRepository ;
- }
-
/**
* Return the repoistory selector based on the current JNDI environment.
*
@@ -175,7 +157,7 @@
}
if (loggingContextName == null) {
- return defaultRepository;
+ return LogManager.defaultLoggerRepository;
} else {
//System.out.println("loggingContextName is ["+loggingContextName+"]");
Hierarchy hierarchy = (Hierarchy) hierMap.get(loggingContextName);
---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org