B.4. Configuring Log4J with a Configuration FileThe InitServlet uses the Log4jConfigurator to set up Log4J with our Log4J Configuration file, jbossatwork-log4j.xml. Example B-9 shows the Log4jConfigurator class. Example B-9. Log4jConfigurator.javapackage com.jbossatwork.util; import java.net.*; import org.apache.log4j.BasicConfigurator; import org.apache.log4j.xml.DOMConfigurator; public class Log4jConfigurator { /** * Making the default (no arg) constructor private * ensures that this class cannnot be instantiated. */ private Log4jConfigurator( ) { } /** * Configures Log4J for an application using the specified Log4J XML * configuration file. * * @param log4jXmlFileName The specified Log4J XML configuration file. */ public static void setup(String log4jXmlFileName) { URL url = ResourceLoader.getAsUrl(log4jXmlFileName); if (url != null) { // An URL (from the CLASSPATH) that points to the Log4J XML // configuration file that was provided, so use Log4J's // DOMConfigurator with the URL to initialize Log4J with the // contents of the Log4J XML configuration file. DOMConfigurator.configure(url); } else { // An URL that points to the Log4J XML configuration file wasn't // provided, so use Log4J's BasicConfigurator to initialize Log4J. BasicConfigurator.configure( ); } } } The Log4jConfigurator.setup( ) method configures Log4J for the JAW Motors application. The ResourceLoader.getAsUrl( ) method retrieves the Log4J configuration file from the CLASSPATH. If the configuration file is found, the Log4jConfigurator.setup( ) method uses the Log4J DOMConfigurator.setup( ) method to configure Log4J with our Log4J configuration file. Otherwise, the Log4jConfigurator.setup( ) method uses Log4J's BasicConfigurator.configure( ) method to configure Log4J with default settings. |