How do I initialize Log4J in a web application?
Author: Deron Eriksson
Description: This Java tutorial how to initialize Log4J with a servlet in a web application.
Tutorial created using:
Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1 || Tomcat 5.5.20
(Continued from page 1) If we start up the project in EclipseSW with the TomcatSW bootstrap, we see the following console output: Console OutputMar 17, 2007 3:37:53 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent INFO: The Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\jdk1.5.0_09\jre\bin;.;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\QuickTime\QTSystem\;C:\jdk1.5.0_09\bin;C:\maven-2.0.4\bin;C:\jadnt158;C:\mysql-essential-5.0.27\bin Mar 17, 2007 3:37:53 AM org.apache.coyote.http11.Http11BaseProtocol init INFO: Initializing Coyote HTTP/1.1 on http-8080 Mar 17, 2007 3:37:53 AM org.apache.catalina.startup.Catalina load INFO: Initialization processed in 906 ms Mar 17, 2007 3:37:53 AM org.apache.catalina.core.StandardService start INFO: Starting service Catalina Mar 17, 2007 3:37:54 AM org.apache.catalina.core.StandardEngine start INFO: Starting Servlet Engine: Apache Tomcat/5.5.20 Mar 17, 2007 3:37:54 AM org.apache.catalina.core.StandardHost start INFO: XML validation disabled Log4JInitServlet is initializing log4j Initializing log4j with: C:\projects\workspace\log4j-webapp-demo\web\WEB-INF/log4j.properties Mar 17, 2007 3:37:54 AM org.apache.catalina.core.ApplicationContext log INFO: org.apache.webapp.balancer.BalancerFilter: init(): ruleChain: [org.apache.webapp.balancer.RuleChain: [org.apache.webapp.balancer.rules.URLStringMatchRule: Target string: News / Redirect URL: http://www.cnn.com], [org.apache.webapp.balancer.rules.RequestParameterRule: Target param name: paramName / Target param value: paramValue / Redirect URL: http://www.yahoo.com], [org.apache.webapp.balancer.rules.AcceptEverythingRule: Redirect URL: http://jakarta.apache.org]] Mar 17, 2007 3:37:55 AM org.apache.catalina.core.ApplicationContext log INFO: ContextListener: contextInitialized() Mar 17, 2007 3:37:55 AM org.apache.catalina.core.ApplicationContext log INFO: SessionListener: contextInitialized() Mar 17, 2007 3:37:55 AM org.apache.catalina.core.ApplicationContext log INFO: ContextListener: contextInitialized() Mar 17, 2007 3:37:55 AM org.apache.catalina.core.ApplicationContext log INFO: SessionListener: contextInitialized() Mar 17, 2007 3:37:55 AM org.apache.coyote.http11.Http11BaseProtocol start INFO: Starting Coyote HTTP/1.1 on http-8080 Mar 17, 2007 3:37:55 AM org.apache.jk.common.ChannelSocket init INFO: JK: ajp13 listening on /0.0.0.0:8009 Mar 17, 2007 3:37:56 AM org.apache.jk.server.JkMain start INFO: Jk running ID=0 time=0/94 config=null Mar 17, 2007 3:37:56 AM org.apache.catalina.storeconfig.StoreLoader load INFO: Find registry server-registry.xml at classpath resource Mar 17, 2007 3:37:56 AM org.apache.catalina.startup.Catalina start INFO: Server startup in 2375 ms In the console output, notice that, among the Tomcat startup messages, our log4j gets initialized, as evidenced by the following lines taken from the above console output: Log4JInitServlet is initializing log4j Initializing log4j with: C:\projects\workspace\log4j-webapp-demo\web\WEB-INF/log4j.properties If we hit our test servletW in a browser window, we see the 'Howdy' message as expected. If we now check our console window, we can see the logging messages that were output in the doGet() method of our test servlet. The output is formatted as we specified in the log4j.properties file. Notice that the log.debug() message in doGet() isn't displayed to the console, since our log4j.properties file specified that the log level is INFO. Initializing log4j via a servlet that loads on web application start-up is a handy technique for initializing log4j. Related Tutorials:
|