Procedure: Using Log4j in Eclipse

1 About this document

Purpose
This document outlines a few unofficial steps on implementing log4j in Eclipse. In this case we are on a Unix based OS.
Audience
Java developers using Eclipse
Requirements
Eclipse IDE
Background
http://logging.apache.org/log4j/1.2/manual.html
This version date
02/20/09 12:45:16 PM

2 Downloading the software

  1. Download log4j tar.gz from the following URL

    http://logging.apache.org/log4j/1.2/download.html

3 Unpacking and setting it up

  1. Change to your $JAVA_HOME directory and unpack the .tar.gz

    cd $JAVA_HOME
    tar -zxvf apache-log4j-1.2.15.tar.gz
  2. Go into the apache-log4j-1.2.15 directory and copy the log4j Jar file to the WEB-INF/lib directory in your project.

  3. Right click on your src folder and create a new file called log4j.properties ( this is the default place that log4j looks for its properties file ) if you are using J2EE view in Eclipse you won’t see the src folder, change to the plain Java view instead for a while.

  4. graphics1

  5. Paste the following code into the log4j.properties file that you just created

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE}%5p%6.6r[%t]%x - %C.%M(%F:%L) - %m%n
log4j.rootLogger=debug, stdout

4 Java Code using Servlets

  1. Insert this code in the .java file that you wish to log (where servlet is the name of the servlet where you are placing this code)

    static Logger log = Logger.getLogger("your.package.name.servlet.class");
  2. When you want to log enter any the following

log.trace(message);

log.debug(“message”);

log.info(“message”);

log.warn(“message”);

log.error(“message”);

log.fatal(“message”);

5 Java Code using JSP

<%org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger("index.jsp");%>  
<% log.info("This is a log"); %>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s