logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sivamma <skemp...@yahoo.co.in>
Subject Re: Urgent Please--How to create different log files with different property files in same webapplication
Date Tue, 26 Jun 2007 10:11:32 GMT

Hi I tried to implement the repository selectors.But still problems occurs.
Please find my repository selector below.
However i am not using Servlets to initialize the log4j.
So Even to implement repository selector i am not using servlets.
Our customers requested us to not use servlets.Since we made everything
using Java and jsps.

==============================================================
package com.CK;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.log4j.Hierarchy;
import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.spi.LoggerRepository;
import org.apache.log4j.spi.RepositorySelector;
import org.apache.log4j.spi.RootCategory;
import java.io.File;
import org.apache.log4j.Logger;
import org.apache.log4j.Level;
import org.apache.log4j.PropertyConfigurator;
/**
 * This RepositorySelector is for use with web applications.  
 * It assumes that your log4j.xml file is in the WEB-INF directory.
 * @author  Stan Silvert
 */
public class Log4jSetup implements RepositorySelector
{
   private static boolean initialized = false;
   // This object is used for the guard because it doesn't get
   // recycled when the application is redeployed.
   private static Object guard = LogManager.getRootLogger();
   
   private static Map repositories = new HashMap();
   private static LoggerRepository defaultRepository;
   /**
    * Register your web-app with this repository selector.
    */
 public static String rPath = null;
 public Log4jSetup(String realPath)
 {
  rPath = realPath;
  //ServletConfig config;
  init();
 }
   public static synchronized void init() 
   {
   //String realPath =
getServletConfig().getServletContext().getRealPath("/");
   //rPath = realPath;
      if( !initialized ) // set the global RepositorySelector
      {
         defaultRepository = LogManager.getLoggerRepository();
         RepositorySelector theSelector = new Log4jSetup();
         LogManager.setRepositorySelector(theSelector, guard);
         initialized = true;
      }
      
      Hierarchy hierarchy = new Hierarchy(new
                                RootCategory(Level.DEBUG));
      loadLog4JConfig(hierarchy);
      ClassLoader loader = 
           Thread.currentThread().getContextClassLoader();
      repositories.put(loader, hierarchy);
   }
   // load config.properties from WEB-INF
   private static void loadLog4JConfig(Hierarchy hierarchy) 
   {
    try 
    {
   String filePath =
rPath+File.separator+"WEB-INF"+File.separator+"config.properties";
   String logfilePath =
rPath+File.separator+"WEB-INF"+File.separator+"logs"+File.separator;
   if( !(new File(filePath)).isFile())
   {
    System.err.println("ERROR:Log4jSetUp::Cannot read the Log4J
configuration file. " +
    "Please check the path of the config init param in web.xml");
   }
   System.setProperty("ck.base",logfilePath);
   PropertyConfigurator.configure(filePath);
    }
    catch (Exception e) 
    {
            System.err.println("Throws Exception::"+e.getMessage());
       }
    }
   private Log4jSetup() {
   }
   public LoggerRepository getLoggerRepository() {
      ClassLoader loader = 
             Thread.currentThread().getContextClassLoader();
      LoggerRepository repository = 
            (LoggerRepository)repositories.get(loader);
      
      if (repository == null) {
          return defaultRepository;
      } else {
          return repository;
      }
   }
}
=========================================================
Can you please tell me whats wrong with the code.Is there any other way to
handle this?.
Thanks & Regards,
Sivamma.


Search the mailing list archives.  I have never used them -- I am just
somewhat familiar with what they do.

-- 
View this message in context: http://www.nabble.com/Urgent-Please--How-to-create-different-log-files-with-different-property-files-in-same-webapplication-tf3974952.html#a11302960
Sent from the Log4j - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Mime
View raw message