Loose Ends
There are a couple more loose ends:
First, you'd ideally use ExtendedLoggerorg.apache.log4j.LoggerExtendedLoggerLogger
com.holub.util.ExtendedLogger log1 = com.holub.util.ExtendedLogger.getLogger( "loggerName" );
org.apache.log4j.Logger log2 = org.apache.log4j.Logger.getLogger(
"loggerName" );
log1.debug("hello");
log2.debug("world");
Then log2log1ExtendedLogger
Reversing the previous calls actually has no impact. The ExtendedLoggerLogger.getLogger()ExtendedLoggerlog()
If the two calls are in different files, however, there's no telling what might happen with the configuration.
The ExtendedLogger.getLogger()Logger.getLogger()log()ExtendedLogger.configure()main()ServletContextListener
package com.holub.myServlet;
import javax.servlet.ServletContextEvent;
import javax.servlet.annotation.*;
import com.holub.util.ExtendedLogger;
@WebListener
public class ContextInitializer implements
javax.servlet.ServletContextListener
{
public void contextInitialized(ServletContextEvent sce)
{
ExtendedLogger.configure();
}
public void contextDestroyed(ServletContextEvent sce){ /*nothing to do*/ }
}
The @WebListener
<listener>
<listener-class>com.holub.myServlet.contextInitializer</listener-class>
</listener>
(Other annotations let you do pretty much everything else that you'd do in the XML, so the web.xml file is optional under Tomcat 7.)
Unfortunately,
you can never guarantee that a call to
ExtendedLogger.configure()
The final loose end has to do with the "multiple-deployments-of-a-single-app" problem I discussed earlier.
Several instances of a given Web application might be running simultaneously (with different names) in a single
container, and each of those Web applications might need a different configuration. I usually solve that problem
in my ContextInitializer
//...
@WebListener
class ContextInitializer implements javax.servlet.ServletContextListener
{
@Override public void contextInitialized(ServletContextEvent sce)
{
ServletContext context = sce.getServletContext();
String applicationPath = context.getRealPath("/");
// root dir of the Web app
if( applicationPath.endsWith("/") )
// cut the trailing slash if necessary
applicationPath =
applicationPath.substring(0, applicationPath.length() -1 );
System.setProperty("CONFIG", applicationPath + ".config" );
Places.reset();
ExtendedLogger.configure();
}
@Override public void contextDestroyed(ServletContextEvent sce){
/* nothing to do */ }
}
The getRealPath()".config" /usr/tomcat7/Web apps/myApp.config.
I then set the Places.CONFIGCONFIG.config
Coming Up
Next month, I'll finish up the topic of configuration by looking at a file-based configuration system.


