This example shows how to configure and use Logging in Servlet using Apache Log4j library in Java web application.
Apache Log4j is a Java-based logging utility.
What is Logging in Java ? What is the use of Logger in Java ?
Logger or Logging in Java is used to write important information about program execution during runtime such as object values, exception or error messages in the file or console, which can be later used for analysis purpose.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import javax.servlet.*; import javax.servlet.http.*; import java.io.PrintWriter; import java.io.IOException; import org.apache.log4j.*; public class LoggingServlet extends HttpServlet { private static Logger logger = Logger.getLogger(LoggingServlet.class); public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { logger.info("invoked the LoggingServlet..."); PrintWriter writer = res.getWriter(); writer.println("Check your web server console..."); writer.flush(); writer.close(); } } |