I followed some examples on the web to use RepositorySelector to implement
separate logging for 2 applications deployed on the same instance of
weblogic server. I have the Lgo4j.properties file under Web-Inf/ folder,
This is loaded through web.xml file through a startup servlet. Problem is
logging is taking place either in Log4j.log file or other applications log
file. In each java class file, I use..
        private static Logger log = Logger.getLogger(LoginFilter.class);
and call the log .info ..accordingly. In log4j properties file, only
difference is file name for 2 applications.

I am using weblogic 8.1.6 and Log4j 1.2.8 versions

thanks for the help
Sohan

web.xml entry...

        <servlet>
                <servlet-name>log4j-init</servlet-name>
                <servlet-class>pcs.common.util.Log4jInit</servlet-class>
           <init-param>
                   <param-name>Log4JProperties</param-name>
                   <param-value>/WEB-INF/log4j-dev.properties</param-value>
           </init-param>
           <init-param>
                   <param-name>LogToFileOnly</param-name>
                   <param-value>false</param-value>
           </init-param>
                <load-on-startup>1</load-on-startup>
        </servlet>

public class Log4jInit extends HttpServlet {
        static Logger log = Logger.getLogger(Log4jInit.class);
        public void init() throws ServletException {    
                
System.out.println("\n\n---------------Log4jInit---------------\n\n");          
        
                MyRepositorySelector.init(this.getServletConfig());
                Logger log = Logger.getLogger(this.getClass());
                log.info("Log message from Log4jInit servlet");
                System.out.println("\n\n---------------Log4jInit:
Complete---------------\n\n");

        }

}

public class MyRepositorySelector implements RepositorySelector
{
   private static boolean initialized = false;
   private static Object guard = LogManager.getRootLogger();
   
   private static Map repositories = new HashMap();
   private static LoggerRepository defaultRepository;

   public static synchronized void init(ServletConfig config) 
        throws ServletException {
      if( !initialized ) // set the global RepositorySelector
      {
         defaultRepository = LogManager.getLoggerRepository();
         RepositorySelector theSelector = new MyRepositorySelector();
         LogManager.setRepositorySelector(theSelector, guard);
         initialized = true;
      }
      
      Hierarchy hierarchy = new Hierarchy(new RootCategory(Level.DEBUG));
      loadLog4JConfig(config, hierarchy);
      ClassLoader loader = Thread.currentThread().getContextClassLoader();
      repositories.put(loader, hierarchy);
   }
   
   public static synchronized void removeFromRepository() {
       repositories.remove(Thread.currentThread().getContextClassLoader());
   }


   private static void loadLog4JConfig(ServletConfig config, 
                                       Hierarchy hierarchy) 
                                            throws ServletException {
        try {
                
                String strLogProperties =
                        
config.getInitParameter(GlobalDictionary.LOG_PROPERTIES);
                System.out.println("strLogProperties: " + strLogProperties);
                Properties logProperties = new Properties();
                try {
                
logProperties.load(config.getServletContext().getResourceAsStream(strLogProperties));
                } catch (Exception e) {
                        System.out.println("ERROR: Loading log4j properties: " +
e.getMessage());
                        //e.printStackTrace();
                }
                PropertyConfigurator propConf = new PropertyConfigurator();
            propConf.doConfigure(logProperties, hierarchy);
                        } catch (Exception e) {
            throw new ServletException(e);
        }
    }

   private MyRepositorySelector() {
   }

   public LoggerRepository getLoggerRepository() {
      ClassLoader loader = Thread.currentThread().getContextClassLoader();
      LoggerRepository repository =
(LoggerRepository)repositories.get(loader);
      
      if (repository == null) {
          return defaultRepository;
      } else {
          return repository;
      }
   }
}
-- 
View this message in context: 
http://www.nabble.com/separate-log-files-for-application%27s-on-weblogic-with-log4j-using-RepositorySelector-tf3549324.html#a9908677
Sent from the Log4j - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to