[ Team LiB ] |
![]() ![]() |
Recipe 14.2 Setting Up Log4jProblemYou want to set up log4j for use in your web application. SolutionDownload the log4j distribution from the Apache Jakarta project and place the accompanying log4j-1.2.8.jar file (the name will be different for different log4j versions) in the WEB-INF/lib directory of your web application. DiscussionThe log4j package is available for use under the Apache Software License, which is included with the distribution. Here are the steps for setting up log4j for your web application:
Example 14-2. Importing log4j-related packagesimport org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; import javax.servlet.*; import javax.servlet.http.*; public class LoggerSkel extends HttpServlet { private Logger log; public void init( ){ //log4j will find the log4j.properties file //in WEB-INF/classes log = Logger.getLogger(LoggerSkel.class); //Just an example of using the logger log.debug("Instance created of: " + getClass( ).getName( )); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException { //do logging here if necessary } //doGet } As long as log4j-1.2.8.jar is located in WEB-INF/lib, your servlet can use the necessary classes from the org.apache.log4j.* packages. See AlsoRecipe 14.3-Recipe 14.8 on using log4j to design your own custom logging mechanism; the log4j download site: http://jakarta.apache.org/log4j/docs/download.html; the log4j project documentation page: http://jakarta.apache.org/log4j/docs/documentation.html. ![]() |
[ Team LiB ] |
![]() ![]() |