Category: JSP

How to integrate Log4J into a Java application

Its is best to log all messages to a individual log file instead of using the standard tomcat log files. Below is a quick tutorial to setup very simply logging using a JSP and a properties file. It is best to read the documentation available on the Log4J website to find out which is the best way to setup logging for your application.

  • Download Log4J from the web [http://logging.apache.org/site/binindex.cgi
  • Place the log4j****.jar into your WEB-INF/lib directory. This is found in distlib in the downloaded archive file.
  • Create a property file as listed below named “log4j.properties”
# Log4j configuration file.
 log4j.rootCategory=DEBUG, A1
 # Available levels are DEBUG, INFO, WARN, ERROR, FATAL
 log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender
 log4j.appender.A1.file=logfile.log
 log4j.appender.A1.datePattern='.'yyyy-MM-dd
 log4j.appender.A1.append=true
 log4j.appender.A1.layout=org.apache.log4j.PatternLayout
 log4j.appender.A1.layout.ConversionPattern=%-5p %d{ISO8601} [%t - %m%n
  • Create simple JSP, ensure to import the correct packages
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" import="org.apache.log4j.*" %>



<%="Hello World"%>
<% PropertyConfigurator.configure("log4j.properties");//This is best added to your startup page Logger logger = Logger.getRootLogger(); logger.info("Hello World"); %>


How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.