[jboss-user] [Beginners Corner] - Re: Web Application Context Logging (log4j) Question
sstacha
do-not-reply at jboss.com
Wed May 6 09:29:41 EDT 2009
Yes, I include log4j-1.2.8 in each webapp's WEB-INF/lib folder. Let me go ahead and post the background code and such then.
First, I was trying to load a context event listener with the following code:
package biz.ormia.util.logging;
/**
* User: sstacha
* Date: May 3, 2009
* Time: 1:22:11 PM
* Attempt to do logging without much effort on the devleopers part.
* Developers will need to add the following the web.xml file
*
* <listener-class>biz.ormia.util.logging.ContextRepositoryListener</listener-class>
*
*
*/
import org.apache.log4j.spi.WebAppRepositorySelector;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.Enumeration;
public class ContextRepositoryListener implements ServletContextListener
{
public void contextInitialized(ServletContextEvent contextEvent)
{
try
{
System.out.println("Attempting to add a context to the repositorySelector: " + contextEvent.getServletContext().getServletContextName());
Enumeration names=contextEvent.getServletContext().getAttributeNames();
System.out.println("----- attributes -----");
String name;
while (names.hasMoreElements())
{
name = (String)names.nextElement();
System.out.println(" " + name + " - " + contextEvent.getServletContext().getAttribute(name));
}
names=contextEvent.getServletContext().getInitParameterNames();
System.out.println("----- init params -----");
while (names.hasMoreElements())
System.out.println(" " + names.nextElement());
WebAppRepositorySelector.init(contextEvent.getServletContext());
//ContextRepositorySelector.init(contextEvent.getServletContext());
}
catch (Exception ex) {System.err.println(ex);}
}
public void contextDestroyed(ServletContextEvent contextEvent)
{
System.out.println("Attempting to remove a context from the repositorySelector: " + contextEvent.getServletContext());
ContextRepositorySelector.removeContext();
}
}
As you can see mostly I was trying to figure out what I had available to me and make sure it was being called. The only real thing in init is to call the
WebAppRepositorySelector.init(contextEvent.getServletContext());
WebAppRepositorySelector is my implementation which simply tries to take the default respository that jboss sets up and then tries to add a new one for each webapp deployment.
package org.apache.log4j.spi;
import org.apache.log4j.LogManager;
import org.apache.log4j.Hierarchy;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import java.util.Map;
import java.util.HashMap;
/**
* User: sstacha
* Date: May 5, 2009
* Time: 2:58:16 PM
* To change this template use File | Settings | File Templates.
*/
public class WebAppRepositorySelector implements RepositorySelector
{
private static Map<ClassLoader, LoggerRepository> repositories = new HashMap<ClassLoader, LoggerRepository>();
private static LoggerRepository defaultRepository;
public static void init(ServletContext context) throws ServletException
{
System.out.println("in addContext...");
System.out.println("current thread: " + Thread.currentThread().getName());
System.out.println("classloader: " + Thread.currentThread().getContextClassLoader().toString());
System.out.println("context: " + context.getServletContextName());
if (defaultRepository == null)
{
defaultRepository = LogManager.getLoggerRepository();
Object guard = LogManager.getRootLogger();
LogManager.setRepositorySelector(new WebAppRepositorySelector(), guard);
}
System.out.println("defaultRepository: " + defaultRepository.toString());
org.apache.log4j.Logger rootLogger = new RootLogger(org.apache.log4j.Level.DEBUG);
System.out.println("rootLogger: " + rootLogger);
// org.apache.log4j.Hierarchy hierarchy = new Hierarchy(rootLogger);
//
// try {repositories.put(Thread.currentThread().getContextClassLoader(), hierarchy);}
// catch (Exception ex) {System.out.println("Exception while creating new heiarchy for repository: " + ex);}
// if (defaultRepository == null)
// {
// defaultRepository = LogManager.getLoggerRepository();
// Object guard = LogManager.getRootLogger();
// LogManager.setRepositorySelector(new CTXRepositorySelector(), guard);
// }
// // add our thread instance in for lookup later
// Hierarchy hierarchy = new Hierarchy(new RootLogger(Level.DEBUG));
// try {repositories.put(Thread.currentThread().getContextClassLoader(), hierarchy);}
// catch (Exception ex) {System.out.println("Exception while creating new heiarchy for repository: " + ex);}
}
public static synchronized void removeContext()
{
System.out.println("in removeContext...");
System.out.println("current thread: " + Thread.currentThread().getName());
System.out.println("classloader: " + Thread.currentThread().getContextClassLoader().toString());
}
private WebAppRepositorySelector() { }
public LoggerRepository getLoggerRepository()
{
System.out.println("in getLoggerRepository...");
System.out.println("current thread: " + Thread.currentThread().getName());
System.out.println("classloader: " + Thread.currentThread().getContextClassLoader().toString());
ClassLoader cl = Thread.currentThread().getContextClassLoader();
// Hierarchy hierarchy = (Hierarchy) repositories.get(cl);
//
// if (hierarchy == null) {
// hierarchy = new Hierarchy(new RootLogger(org.apache.log4j.Level.DEBUG));
// hierMap.put(cl, hierarchy);
// }
//
// return hierarchy;
LoggerRepository repository = null;
try
{
repository = repositories.get(Thread.currentThread().getContextClassLoader());
}
catch (Exception ex) {System.out.println("Exception in getLoggerRepository getting the repository from the class loader: " + ex);}
if (repository == null)
{
System.out.println("respository is null using default...");
return defaultRepository;
}
else
{
System.out.println("Using repository: " + repository);
return repository;
}
// if (repository == null)
// return defaultRepository;
// return repository;
// if (defaultRepository != null)
// return defaultRepository;
// return null;
}
}
Note that this works as I have it commented here but I am not doing anything aside from saving off the original jboss repository and then resetting the selector to ourselves. If I uncomment the next line:
org.apache.log4j.Hierarchy hierarchy = new Hierarchy(rootLogger);
Then I get the error mentioned above. If I don't put my Selector in the log4j...spi package then I get a generic error:
[ERROR] - Exception sending context initialized event to listener instance of class biz.ormia.util.logging.ContextRepositoryListener
java.lang.VerifyError: (class: biz/ormia/util/logging/ContextRepositorySelector, method: init signature: (Ljavax/servlet/ServletContext;)V) Incompatible argument to function
View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4229010#4229010
Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=4229010
More information about the jboss-user
mailing list