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 dist\lib\ 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.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>TestWeb</title> </head> <body> <%="Hello World"%> <% PropertyConfigurator.configure("log4j.properties");//This is best added to your startup page Logger logger = Logger.getRootLogger(); logger.info("Hello World"); %> </body> </html>
Back to JSP
Add new attachment
Only authorized users are allowed to upload new attachments.
«
This page (revision-1) was last changed on 24-May-2017 15:30 by UnknownAuthor