hoju 2004/12/14 22:30:10 Modified: . build.xml src/java/org/apache/log4j/filter MDCMatchFilter.java src/java/org/apache/log4j/servlet InitShutdownController.java ServletContextLogAppender.java Removed: src/java/org/apache/log4j/selector ContextClassLoaderSelector.java package.html Log: Changes to make sandbox compatible with Log4j-1.3. Got rid of ContextClassLoaderSelector because it is just problematic. Jake Revision Changes Path 1.16 +3 -14 logging-log4j-sandbox/build.xml Index: build.xml =================================================================== RCS file: /home/cvs/logging-log4j-sandbox/build.xml,v retrieving revision 1.15 retrieving revision 1.16 diff -u -r1.15 -r1.16 --- build.xml 2 Jul 2004 21:29:37 -0000 1.15 +++ build.xml 15 Dec 2004 06:30:10 -0000 1.16 @@ -16,7 +16,7 @@ <property file="build.properties"/> <property file="build.properties.sample"/> - <property name="version" value="0.3alpha"/> + <property name="version" value="0.4alpha"/> <!-- The base directory relative to which most targets are built --> <property name="base" value="."/> @@ -114,11 +114,8 @@ <include name="**/*.java"/> <exclude name="**/servlet/**/*.java"/> <!--build excludes below via chainsaw-build.xml--> - <exclude name="**/chainsaw/**/*.java"/> <exclude name="**/jdbc/**/*.java"/> <exclude name="**/net/**/*.java"/> - <exclude name="**/spi/**/*.java"/> - <exclude name="**/xml/**/*.java"/> <classpath refid="compile.classpath"/> </javac> </target> @@ -158,9 +155,7 @@ <!-- ================================================================= --> <fileset dir="${java.source.dir}" id="styled_files"> <include name="**/filter/*.java"/> - <include name="**/selector/*.java"/> <include name="**/servlet/*.java"/> - <include name="**/helpers/*.java"/> </fileset> <!-- ================================================================= --> @@ -251,15 +246,9 @@ <javadoc sourcepath="${java.source.dir}" destdir="${javadoc.dest}" - packagenames="org.apache.log4j, - org.apache.log4j.chainsaw, - org.apache.log4j.filter, - org.apache.log4j.helpers, + packagenames="org.apache.log4j.filter, org.apache.log4j.net, - org.apache.log4j.plugins, - org.apache.log4j.selector, - org.apache.log4j.servlet, - org.apache.log4j.xml" + org.apache.log4j.servlet" version="true" protected="true" author="true" 1.4 +3 -1 logging-log4j-sandbox/src/java/org/apache/log4j/filter/MDCMatchFilter.java Index: MDCMatchFilter.java =================================================================== RCS file: /home/cvs/logging-log4j-sandbox/src/java/org/apache/log4j/filter/MDCMatchFilter.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- MDCMatchFilter.java 28 Feb 2004 13:55:04 -0000 1.3 +++ MDCMatchFilter.java 15 Dec 2004 06:30:10 -0000 1.4 @@ -17,6 +17,7 @@ package org.apache.log4j.filter; import org.apache.log4j.spi.LoggingEvent; +import org.apache.log4j.MDC; /** @@ -138,7 +139,8 @@ protected boolean match(LoggingEvent event) { // get the mdc value for the key from the event // use the toString() value of the value object - Object mdcObject = event.getMDC(keyToMatch); + //Object mdcObject = event.getMDC(keyToMatch); //removed in Log4j-1.3 + Object mdcObject = MDC.get(keyToMatch); String mdcValue; if (mdcObject != null) { 1.5 +48 -71 logging-log4j-sandbox/src/java/org/apache/log4j/servlet/InitShutdownController.java Index: InitShutdownController.java =================================================================== RCS file: /home/cvs/logging-log4j-sandbox/src/java/org/apache/log4j/servlet/InitShutdownController.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- InitShutdownController.java 15 May 2004 15:41:12 -0000 1.4 +++ InitShutdownController.java 15 Dec 2004 06:30:10 -0000 1.5 @@ -19,6 +19,7 @@ import org.apache.log4j.LogManager; import org.apache.log4j.PropertyConfigurator; import org.apache.log4j.helpers.LogLog; +import org.apache.log4j.joran.JoranConfigurator; import org.apache.log4j.spi.RepositorySelector; import org.apache.log4j.xml.DOMConfigurator; @@ -60,13 +61,6 @@ public static final String PARAM_LOG4J_CONFIG_PATH = "log4j-config"; /** - * config file re-reading specified in milliseconds config param. - * Maps to web.xml <context-param> with - * <param-name>log4j-cron</param-name> - */ - public static final String PARAM_LOG4J_WATCH_INTERVAL = "log4j-cron"; - - /** * path to be read from a log4j xml config file as a system property * config param. Maps to web.xml <context-param> with * <param-name>log4j-log-home</param-name> @@ -97,7 +91,7 @@ "Cleaning up Log4j resources for context: " + context.getServletContextName() + "..."); context.log("Shutting down all loggers and appenders..."); - org.apache.log4j.LogManager.shutdown(); + LogManager.shutdown(); context.log("Log4j cleaned up."); } @@ -109,10 +103,10 @@ * @param context the current servlet context */ public static void initializeLog4j(final ServletContext context) { - ClassLoader savedClassLoader = null; + //ClassLoader savedClassLoader = null; try { - savedClassLoader = Thread.currentThread().getContextClassLoader(); + //savedClassLoader = Thread.currentThread().getContextClassLoader(); //attempt to set the thread context class loader to the current class //loader to avoid ClassCastExceptions when this library is in a parent @@ -121,8 +115,8 @@ //log4j.jar). //based on the following article... //http://www-106.ibm.com/developerworks/websphere/library/techarticles/0310_searle/searle.html - Thread.currentThread().setContextClassLoader( - InitShutdownController.class.getClassLoader()); + //Thread.currentThread().setContextClassLoader( + // InitShutdownController.class.getClassLoader()); } catch (SecurityException se) { //oh, well, we tried. Security is turned on and we aren't allowed to //tamper with the thread context class loader. You're on your own! @@ -152,12 +146,12 @@ displayConfigNotFoundMessage(); } } finally { - if (savedClassLoader != null) { + //if (savedClassLoader != null) { //reset the thread context class loader to the original saved class //loader now that our purpose (avoiding class cast exceptions) has been //served - Thread.currentThread().setContextClassLoader(savedClassLoader); - } + //Thread.currentThread().setContextClassLoader(savedClassLoader); + //} } } @@ -238,14 +232,16 @@ if (log4jURL != null) { context.log("Configuring Log4j from URL at path: /" + configPath); - if (isXMLConfigFile) { + if (isXMLConfigFile) { try { - DOMConfigurator.configure(log4jURL); - - //catch (javax.xml.parsers.FactoryConfigurationError fce) {} + if (joranConfiguratorAvailable()) { + new JoranConfigurator().doConfigure(log4jURL, LogManager.getLoggerRepository()); + } else { + DOMConfigurator.configure(log4jURL); + } } catch (Exception e) { //report errors to server logs - LogLog.error(e.getMessage()); + System.err.println(e.getMessage()); //TODO(Jake) - use Log4 internal logging } } else { Properties log4jProps = new Properties(); @@ -257,7 +253,7 @@ //catch (java.io.IOException ioe) {} } catch (Exception e) { //report errors to server logs - LogLog.error(e.getMessage()); + System.err.println(e.getMessage()); //TODO(Jake) - use Log4 internal logging } } } else { @@ -277,8 +273,7 @@ private static void configureLog4jFromFile( final String configPath, final ServletContext context) { //The webapp is deployed directly off the filesystem, not from a .war file - //so we *can* do File IO. This means we can use configureAndWatch() to - //re-read the the config file at defined intervals. + //so we *can* do File IO. boolean isXMLConfigFile = (configPath.endsWith(".xml")) ? true : false; String contextPath = context.getRealPath("/"); @@ -289,27 +284,16 @@ if (log4jFile.canRead()) { log4jFile = null; - long timerIntervalVal = getTimerIntervalFromContext(context); - context.log( - "Configuring Log4j from File: " + contextPath + systemConfigPath); + context.log("Configuring Log4j from File: " + contextPath + systemConfigPath); - if (timerIntervalVal > 0) { - context.log( - "Configuring Log4j with watch interval: " + timerIntervalVal + "ms"); - - if (isXMLConfigFile) { - DOMConfigurator.configureAndWatch( - contextPath + systemConfigPath, timerIntervalVal); + if (isXMLConfigFile) { + if (joranConfiguratorAvailable()) { + new JoranConfigurator().doConfigure(contextPath + systemConfigPath, LogManager.getLoggerRepository()); } else { - PropertyConfigurator.configureAndWatch( - contextPath + systemConfigPath, timerIntervalVal); - } - } else { - if (isXMLConfigFile) { DOMConfigurator.configure(contextPath + systemConfigPath); - } else { - PropertyConfigurator.configure(contextPath + systemConfigPath); } + } else { + PropertyConfigurator.configure(contextPath + systemConfigPath); } } else { // The given configPath does not exist. So, let's just @@ -320,33 +304,11 @@ } /** - * Retrieves the timer interval from the servlet context. - * - * @param context the current servlet context - */ - private static long getTimerIntervalFromContext( - final ServletContext context) { - String timerInterval = - context.getInitParameter(PARAM_LOG4J_WATCH_INTERVAL); - long timerIntervalVal = 0L; - - if (timerInterval != null) { - try { - timerIntervalVal = Integer.valueOf(timerInterval).longValue(); - } catch (NumberFormatException nfe) { - //ignore...we just won't use configureAndWatch if there is no valid int - ; - } - } - - return timerIntervalVal; - } - - /** * standard configuration not found message */ private static void displayConfigNotFoundMessage() { - LogLog.warn( + //TODO(Jake) - use Log4 internal logging + System.out.println( "No Log4j configuration file found at given path. " + "Falling back to Log4j auto-configuration."); } @@ -423,10 +385,12 @@ * @param context the current servlet context */ private static void setSelector(final ServletContext context) { + //TODO(Jake) - use Log4 internal logging + String selector = context.getInitParameter(PARAM_LOG4J_PREF_SELECTOR); if (selector == null) { - LogLog.warn( + System.out.println( "No preferred selector supplied. Using default repository selector..."); return; @@ -438,23 +402,36 @@ Class clazz = Class.forName( selector, true, Thread.currentThread().getContextClassLoader()); - LogManager.setRepositorySelector( - (RepositorySelector) clazz.newInstance(), guard); + RepositorySelector repositorySelector = (RepositorySelector) clazz.newInstance(); + //System.out.println("current logger repository is: " + LogManager.getLoggerRepository()); + repositorySelector.setDefaultRepository(LogManager.getLoggerRepository()); //Note: without this step, we'd take down the entire server with null pointers happening in LogManager.getLogger(). Do no remove! + LogManager.setRepositorySelector(repositorySelector, guard); } catch (ClassNotFoundException cnfe) { - LogLog.warn( + System.err.println( "Preferred selector not found. Using default repository selector..."); } catch (InstantiationException ie) { - LogLog.warn( + System.err.println( "Error in instantiation of preferred selector. Using default " + "repository selector..."); } catch (IllegalAccessException iae) { - LogLog.warn( + System.err.println( "Unable to access preferred selector. Using default repository " + "selector..."); } catch (IllegalArgumentException iae) { - LogLog.warn( + System.err.println( "Preferred repository selector not installed because one has already " + "exists. No problem, using existing selector..."); } } + + private static boolean joranConfiguratorAvailable() { + boolean joranAvailable = true; + try { + Class.forName("org.apache.joran.Interpreter", false, Thread.currentThread().getContextClassLoader()); + } catch (Exception e) { + joranAvailable = false; + } + return joranAvailable; + } + } 1.4 +1 -1 logging-log4j-sandbox/src/java/org/apache/log4j/servlet/ServletContextLogAppender.java Index: ServletContextLogAppender.java =================================================================== RCS file: /home/cvs/logging-log4j-sandbox/src/java/org/apache/log4j/servlet/ServletContextLogAppender.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- ServletContextLogAppender.java 2 Jul 2004 21:32:47 -0000 1.3 +++ ServletContextLogAppender.java 15 Dec 2004 06:30:10 -0000 1.4 @@ -126,7 +126,7 @@ if (ti == null) { servletContext.log(s); } else { - servletContext.log(s, ti.getThrowable()); + servletContext.log(s + "\n" + ti.getThrowableStrRep()); } return;
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]