If you've followed Part 4 of this tutorial, we're about to setup logging for the ModelingAgency webapp in order to learn how to use MySQL, MyBatis and Tomcat togather.
If you're familiar with Log4J you can use that library. However for the ModelingAgency webapp we're going to setup the Logback library, which is the new Log4J.
You have probably already downloaded the Logback jars in Part 1. If not, you can download all of the jars required for the ModelingAgency app here.
Configuring Logback
Configuring Logback with a Java web application is relatively simple. Once you have the required jars in /WEB-INF/lib, create a file called logback.xml and put it under your project's /src folder.
Here is the logback.xml file we we will use for this project:
<?xml version="1.0" encoding="UTF-8"?> <configuration scan="true" debug="false" > <!-- set debug to true to dump logback status info when library is loaded --> <contextName>ModelingAgency</contextName> <!-- APPENDERS --> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder by default --> <encoder> <pattern>[%contextName] %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <appender name="SQLDUMP" class="ch.qos.logback.core.FileAppender"> <file>sql.log</file> <append>false</append> <encoder> <!-- <pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern> --> <pattern>[%contextName] [%c] [%d{dd MMM yyyy - hh:mm:ss}] %5p - %m %n</pattern> </encoder> </appender> <appender name="FILE" class="ch.qos.logback.core.FileAppender"> <file>application.log</file> <encoder> <!-- <pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern> --> <pattern>[%contextName] [%c] [%d{dd MMM yyyy - hh:mm:ss}] %5p - %m %n</pattern> </encoder> </appender> <appender name="OUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>[%contextName] [%c] [%d{dd MMM yyyy - hh:mm:ss}] %5p - %m %n</pattern> </encoder> </appender> <appender name="OUT_SIMPLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>[%c] %5p - %m %n</pattern> </encoder> </appender> <!-- LOGGERS --> <!-- loggers inherit the appender from their parent ( the root logger ) --> <logger name="java.sql" level="DEBUG" additivity="false"> <appender-ref ref="SQLDUMP" /> </logger> <logger name="org.apache.ibatis.datasource" level="WARN"/> <!-- removes datasoure debug info from logs --> <root level="debug"> <!-- can set level to OFF--> <appender-ref ref="OUT" /> </root> </configuration>
Adding logging to TestServlet
To make sure logback works properly, let's add some simple logging code to the TestServlet we created in Part 4
Add the following logging code to the TestServlet.java file:
package com.modelingagency.servlets; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class TestServlet extends HttpServlet{ private static Logger log = LoggerFactory.getLogger(TestServlet.class); protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); ServletContext sc = getServletContext(); request.setAttribute("message","This is a message from the TestServlet. It works!"); log.info("The message attribute has been set"); RequestDispatcher view = request.getRequestDispatcher("index.jsp"); view.forward(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } }
Restart your project now, by right clicking on the index.jsp file and selecting Run on server.
If you access the TestServlet's url on your local server now, http://localhost:8080/ModelingAgency/test.do you should get an output like this:
[ModelingAgency] [com.modelingagency.servlets.TestServlet] [03 Feb 2011 - 10:38:26] INFO - The message attribute has been set
If you got this far, it means you have setup Logback logging for the ModelingAgency webapp.
Now the fun part,