You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by bi...@apache.org on 2003/04/07 02:29:23 UTC
cvs commit: jakarta-tomcat/proposals/Log4jHelper/WEB-INF/classes/org/apache/tomcat/modules/loggers/log4j CRS.java Log4jHelper.java
billbarker 2003/04/06 17:29:23
Added: proposals/Log4jHelper build.xml
proposals/Log4jHelper/WEB-INF interceptors.xml
proposals/Log4jHelper/WEB-INF/classes/org/apache/tomcat/modules/loggers/log4j
CRS.java Log4jHelper.java
Log:
Adding optional support for log4j seperation by context.
Revision Changes Path
1.1 jakarta-tomcat/proposals/Log4jHelper/build.xml
Index: build.xml
===================================================================
<?xml version="1.0"?>
<project name="Log4jHelper" default="main" basedir=".">
<!-- Compilation properties -->
<property name="optimize" value="false"/>
<property name="debug" value="on"/>
<!-- Directories -->
<property name="ws" value="../../.."/>
<property name="work.dir" value="${ws}/jakarta-tomcat"/>
<property name="tomcat.build" value="${ws}/jakarta-tomcat/build/tomcat"/>
<property name="tomcat.dist" value="${ws}/jakarta-tomcat/dist/tomcat"/>
<property name="tomcat.build.modules" value="${tomcat.build}/modules"/>
<property name="tomcat.dist.modules" value="${tomcat.dist}/modules"/>
<property name="log4j.jar" location="${ws}/log4j/log4j.jar" />
<property name="tomcat.classes.dir" location="${tomcat.build}/classes"/>
<property name="tomcat.classes" value="${tomcat.classes.dir}"/>
<!-- ==================== Initialization - check if build and dist present ========== -->
<target name="init">
<available property="build.modules.available" file="${tomcat.build.modules}"/>
<available property="dist.modules.available" file="${tomcat.dist.modules}"/>
</target>
<!-- ==================== Preparation ==================== -->
<target name="prepare" depends="init">
</target>
<target name="build.modules.check" depends="prepare" unless="build.modules.available">
<echo message="You must build Tomcat first!"/>
</target>
<target name="dist.modules.check" depends="prepare" unless="dist.modules.available">
<echo message="You must build the Tomcat distribution first!"/>
</target>
<!-- ==================== Build the Password Prompter sample add-on ================== -->
<target name="log4j.support" depends="prepare" if="build.modules.available">
<mkdir dir="${tomcat.build.modules}/Log4jHelper"/>
<copy todir="${tomcat.build.modules}/Log4jHelper">
<fileset dir="${work.dir}/proposals/Log4jHelper" excludes="build.xml"/>
</copy>
<javac srcdir="${tomcat.build.modules}/Log4jHelper/WEB-INF/classes"
optimize="${optimize}"
debug="${debug}"
destdir="${tomcat.build.modules}/Log4jHelper/WEB-INF/classes">
<classpath path="${tomcat.classes}"/>
<classpath location="${log4j.jar}"/>
</javac>
<mkdir dir="${tomcat.build.modules}/Log4jHelper/WEB-INF/lib"/>
<jar jarFile="${tomcat.build.modules}/Log4jHelper/WEB-INF/lib/Log4jHelper.jar"
basedir="${tomcat.build.modules}/Log4jHelper/WEB-INF/classes"/>
<delete dir="${tomcat.build.modules}/Log4jHelper/WEB-INF/classes"/>
</target>
<!-- ==================== Build the Password Prompter sample add-on WAR ================== -->
<target name="log4j.support.war"
depends="build.modules.check,dist.modules.check,log4j.support"
if="dist.modules.available">
<jar jarFile="${tomcat.dist.modules}/Log4jHelper.war"
baseDir="${tomcat.build.modules}/Log4jHelper"/>
</target>
<!-- ==================== Admin & agreagate ==================== -->
<target name="clean" depends="init">
<delete dir="${tomcat.build.modules}/Log4jHelper"/>
<delete dir="${tomcat.dist.modules}/Log4jHelper"/>
<delete file="${tomcat.dist.modules}/Log4jHelper.war"/>
</target>
<target name="main" depends="build.modules.check,log4j.support"/>
</project>
1.1 jakarta-tomcat/proposals/Log4jHelper/WEB-INF/interceptors.xml
Index: interceptors.xml
===================================================================
<?xml version="1.0" encoding="UTF-8"?>
<Server>
<module name="Log4jHelper"
javaClass="org.apache.tomcat.modules.loggers.log4j.Log4jHelper" />
<ContextManager>
<Log4jHelper defaultLevel="debug" />
</ContextManager>
</Server>
1.1 jakarta-tomcat/proposals/Log4jHelper/WEB-INF/classes/org/apache/tomcat/modules/loggers/log4j/CRS.java
Index: CRS.java
===================================================================
/*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.tomcat.modules.loggers.log4j;
import java.util.*;
import org.apache.tomcat.core.*;
import org.apache.log4j.*;
import org.apache.log4j.spi.*;
/**
* Implimentation to add context-seperation support for log4j logging.
* Code taken from <a href="http://www.qos.ch/logging/sc.html">
* Support for log4j in Servlet Containers</a> by Ceki G�lc�.
* @author Ceki G�lc�.
*/
public class CRS implements RepositorySelector {
private Hashtable ht = new Hashtable();
private Level defLevel=Level.DEBUG;
public void setDefaultLevel(Level lv) {
if(lv != null)
defLevel = lv;
}
public Level getDefaultLevel() {
return defLevel;
}
void addRepository(ClassLoader cl) {
if( ht.get(cl) != null ) {
throw new IllegalStateException("ClassLoader " + cl + " already registered");
}
ht.put(cl, new Hierarchy(new RootCategory( defLevel)));
}
// the returned value is guaranteed to be non-null
public LoggerRepository getLoggerRepository() {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
Hierarchy hierarchy = (Hierarchy) ht.get(cl);
if(hierarchy == null) {
hierarchy = new Hierarchy(new RootCategory(defLevel));
ht.put(cl, hierarchy);
}
return hierarchy;
}
/**
* The Container should remove the entry when the web-application
* is removed or restarted.
* */
public void remove(ClassLoader cl) {
ht.remove(cl);
}
}
1.1 jakarta-tomcat/proposals/Log4jHelper/WEB-INF/classes/org/apache/tomcat/modules/loggers/log4j/Log4jHelper.java
Index: Log4jHelper.java
===================================================================
/*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.tomcat.modules.loggers.log4j;
import org.apache.tomcat.core.*;
import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
/**
* Interceptor to add context-seperation support for log4j logging.
* Based on ideas from <a href="http://www.qos.ch/logging/sc.html">
* Support for log4j in Servlet Containers</a> by Ceki G�lc�.
*/
public class Log4jHelper extends BaseInterceptor {
private CRS crs = new CRS();
public Log4jHelper() {}
public void setDefaultLevel(String level) {
Level value = null;
if("all".equalsIgnoreCase(level))
value = Level.ALL;
else if("debug".equalsIgnoreCase(level))
value = Level.DEBUG;
else if("error".equalsIgnoreCase(level))
value =Level.ERROR;
else if("fatal".equalsIgnoreCase(level))
value = Level.FATAL;
else if("info".equalsIgnoreCase(level))
value = Level.INFO;
else if("off".equalsIgnoreCase(level))
value = Level.OFF;
else if("warn".equalsIgnoreCase(level))
value = Level.WARN;
crs.setDefaultLevel(value);
}
public String getDefaultLevel() {
String value = null;
Level level = crs.getDefaultLevel();
if(level == Level.ALL)
value = "all";
else if(level == Level.DEBUG)
value = "debug";
else if(level == Level.ERROR)
value = "error";
else if(level == Level.FATAL)
value = "fatal";
else if(level == Level.INFO)
value = "info";
else if(level == Level.OFF)
value = "off";
else if(level == Level.WARN)
value = "warn";
return value;
}
public void contextInit( Context ctx )
throws TomcatException {
crs.addRepository(ctx.getClassLoader());
}
public void contextShutdown( Context ctx )
throws TomcatException {
crs.remove(ctx.getClassLoader());
}
public void reload(Request req, Context ctx) throws TomcatException {
ClassLoader oldCl = (ClassLoader)req.getNote("oldLoader");
if(oldCl != null) {
crs.remove(oldCl);
}
crs.addRepository(ctx.getClassLoader());
}
public void copyContext(Request req, Context oldCtx, Context newCtx)
throws TomcatException {
crs.remove(oldCtx.getClassLoader());
crs.addRepository(newCtx.getClassLoader());
}
public void engineInit(ContextManager cm) throws TomcatException {
LogManager.setRepositorySelector(crs, this);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-dev-help@jakarta.apache.org