[jboss-user] [Beginners Corner] - Log4j Custom Logger with RepositorySelector

arena do-not-reply at jboss.com
Tue Jan 30 20:51:26 EST 2007


I followed the instructions mentioned on the  jboss wiki site for writing custom logger component for your web application i.e implementing the RepositorySelector. It works fine when i deploy only one application. We have two application that needs to be deployed on the same instance. When i deploy both the application, its not writing anything to log files(although some people saying its writing to log files created by the very first deployed application).  I am unable to figure out, Can  one of you please help me with this issue. Your help is highly appreciated. Here is my log4j.xml for both my application and ApplicationLogger which implements RepositorySelector

Thanks

----First Application Log4j.xml ----
log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
   
      
      <param name="DatePattern" value="'.'yyyy-MM-dd"/>
      
      
         
      
   
   
      
      <param name="DatePattern" value="'.'yyyy-MM-dd"/>
      
         
      
   
   
      
      <param name="DatePattern" value="'.'yyyy-MM-dd"/>
      
         
      
   
   
      
      <param name="DatePattern" value="'.'yyyy-MM-dd"/>
      
         
      
   
   <!-- Categories for logging -->
   
      
      <appender-ref ref="GAGE" />
   
   
      
      <appender-ref ref="GAGE" />
   
   
      
      <appender-ref ref="TORQUE" />
   
   
      
      <appender-ref ref="AXIS" />
   
   <!-- Setup the Root category -->
   
      <appender-ref ref="ERROR"/>
   
</log4j:configuration>

----Second Application Log4j.xml ----
log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
   
      
      <param name="DatePattern" value="'.'yyyy-MM-dd"/>
      
      
         
      
   
   
      
      <param name="DatePattern" value="'.'yyyy-MM-dd"/>
      
         
      
   
   
      
      <param name="DatePattern" value="'.'yyyy-MM-dd"/>
      
         
      
   
   
      
      <param name="DatePattern" value="'.'yyyy-MM-dd"/>
      
         
      
   
   <!-- Categories for logging -->
   
      
      <appender-ref ref="ADMIN" />
   
   
      
      <appender-ref ref="ADMIN" />
   
   
      
      <appender-ref ref="ADMIN_TORQUE" />
   
   
      
      <appender-ref ref="ADMIN_AXIS" />
   
   <!-- Setup the Root category -->
   
      <appender-ref ref="ADMIN_ERROR"/>
   
</log4j:configuration>

-------ApplicationLogger ----------------

public class ApplicationLogger implements RepositorySelector {

    private Hashtable repositories;

    private static ApplicationLogger instance;

    public void destroy() {
        Enumeration keys = repositories.elements();
        while (keys.hasMoreElements()) {
            Object keyName = keys.nextElement();
            repositories.remove(keyName);
        }
    }

    // load log4j.xml from WEB-INF
    private void loadLog4JConfig(Hierarchy hierarchy, String log4jxml)
            throws IOException, ParserConfigurationException, SAXException {
        FileInputStream log4JConfig = new FileInputStream(log4jxml);
        Document doc = DocumentBuilderFactory.newInstance()
                .newDocumentBuilder().parse(log4JConfig);
        DOMConfigurator conf = new DOMConfigurator();
        conf.doConfigure(doc.getDocumentElement(), hierarchy);
    }

    /**
     * 
     * @return logger hierarchy for refreshing the logger instance
     */
    public Hashtable getRepositoryTable() {
        return repositories;
    }

    public static ApplicationLogger getInstance(String log4jxml)
            throws IOException, SAXException, ParserConfigurationException {
        if (instance == null) {
            instance = new ApplicationLogger(log4jxml);
        }
        return instance;
    }

    private ApplicationLogger(String log4jxml) throws IOException, SAXException,
            ParserConfigurationException {
        repositories = new Hashtable();
        Hierarchy hierarchy = new Hierarchy(new RootCategory(Level.DEBUG));
        loadLog4JConfig(hierarchy, log4jxml);
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        repositories.put(cl, hierarchy);
    }

    public LoggerRepository getLoggerRepository() {
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        Object repository = repositories.get(loader);
        if (repository == null) {
            repository = new Hierarchy(new RootCategory((Level) Level.DEBUG));
            repositories.put(loader, repository);
        }
        return (Hierarchy) repository;
    }
}









View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4008591#4008591

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4008591



More information about the jboss-user mailing list