Here's an update on the logger issue.  It too me long to figure out because I 
ran into an issue about privileged servlets and had to write my own 
log4j.properties parser.

The original code posted earlier in this thread works in that it creates a 
different logger for web application, and allows the user to stop and restart 
the application.

   public static Logger getLogger()
   {
      ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
      URL log4j_properties = classLoader.getResource("log4j_app.properties");
      String loggerName = log4j_properties.toString(); // throws 
NullPointerException if log4j_app.properties does not exist
      synchronized (MyLog.class)
      {
         Logger logger = 
Logger.getRootLogger().getLoggerRepository().exists(loggerName);
         if (logger == null)
         {
            logger = Logger.getLogger(loggerName);
            PropertyConfigurator.configure(log4j_properties);
            System.out.println("Created logger: loggerName=" + loggerName);
         }
         return logger;
      }
   }

One bug with the above is the line

            PropertyConfigurator.configure(log4j_properties);


The above re-configures the root logger with the new log4j.properties, so 
basically the new configuration applies to all loggers.  What would be nice is

            PropertyConfigurator.configure(logger, log4j_properties);

that configures only the specified logger with the log4j_properties.  
Unfortunately, log4j does not have such a function, so I wrote my own (but it 
only supports the properties used in my log4j_app.properties files).

Now, if web application 1 calls MyLog.getLogger() or calls common code that 
calls MyLog.getLogger() then the first logger will be used, and similarly for 
web application 2.

Finally, I create a servlet 

   import org.apache.catalina.manager.ManagerServlet;

   public class TomcatManagerServlet extends ManagerServlet

The servlet is called for "/reload", and it calls super.doGet() after doing 
some checks.

It turns out that only privileged web applications can instantiate 
ManagerServlet, so one has to have the following in context.xml:

   <Context privileged="true">

I'm not sure what is the practical impact of setting privileged="true", though 
the docs said something about the catalina and shared class loaders.

So upon calling

http://localhost/mywebservice/reload?path=/

the ROOT application is stopped and started.  After the application restart, 
all users are logged out and they have to log in again, but the logger 
continues to work.  A call to MyLog.getLogger() does not re-create the logger.

The logger is really owned by the common code in ${catalina.home}/lib.  The 
code 

      URL log4j_properties = classLoader.getResource("log4j_app.properties");

just gets the URL of the log4j.properties file.  However, there is no pointer 
to anything in the webapp, so it's possible to stop the webapp.  And the line

            PropertyConfigurator.configure(log4j_properties);

opens the URL (by calling log4j_properties.getInputStream()), reads it, and 
closes it.  So no stream in the webapp is open, and it's possible to stop the 
webapp.



--- On Thu, 1/15/09, Caldarale, Charles R <chuck.caldar...@unisys.com> wrote:

> From: Caldarale, Charles R <chuck.caldar...@unisys.com>
> Subject: RE: How to have multiple loggers with shared code also using the     
> logger?
> To: "Tomcat Users List" <users@tomcat.apache.org>
> Date: Thursday, January 15, 2009, 11:28 AM
> > From: removeps-gro...@yahoo.com
> [mailto:removeps-gro...@yahoo.com]
> > Subject: RE: How to have multiple loggers with shared
> code
> > also using the logger?
> >
> > How does one redploy a single application?  Strange, I
> > could not find any information on this seemingly basic
> > task on google.
> 
> Read the real docs first, then Google only if needed.
> http://tomcat.apache.org/tomcat-6.0-doc/deployer-howto.html#Deploying%20on%20a%20running%20Tomcat%20server
> http://tomcat.apache.org/tomcat-6.0-doc/manager-howto.html
> 
>  - Chuck
> 
> 
> THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR
> OTHERWISE PROPRIETARY MATERIAL and is thus for use only by
> the intended recipient. If you received this in error,
> please contact the sender and delete the e-mail and its
> attachments from all computers.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
> For additional commands, e-mail:
> users-h...@tomcat.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to