Solved

java log4j example not working

Posted on 2014-03-26
1
873 Views
Last Modified: 2014-04-24
Hi,

I am running below log4j example.

https://logging.apache.org/log4j/1.2/manual.html

My code looks as below

package test;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("This is the Test Servlet");

		Enumeration headerNames = request.getHeaderNames();
		while (headerNames.hasMoreElements()) {
			String headerName = (String) headerNames.nextElement();
			out.print("<br/>Header Name: <em>" + headerName);
			String headerValue = request.getHeader(headerName);
			out.print("</em>, Header Value: <em>" + headerValue);
			out.println("</em>");
		}
	}

}

Open in new window

and other class is

 package com.foo;
 import org.apache.log4j.Logger;

 public class Bar {
   static Logger logger = Logger.getLogger(Bar.class);

   public void doIt() {
     logger.debug("Did it again!");
   }
 }

Open in new window


I am getting lot of compilation errors as i am not able to get references to
import org.apache.log4j.Logger;
and
 import org.apache.log4j.Logger;
 import org.apache.log4j.BasicConfigurator;

what are jars i need to include to fix these errors.

I am basically using eclipse and created one Dynamic WEb Project(TestLog) and then added above two classes to that project.

I am not sure what all jars i need to add and how to configure them in the eclipse to take effect into the TestLog project. Are there are any better links with more clear detailed description with screenshot on the set up and execution, output.


please advise
Any links resources ideas highly appreciated. Thanks in advance
0
Comment
Question by:gudii9
1 Comment
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 39957836
On the page that you linked to above, there is a menu on the left hand side which has a "Download" link on it. Follow that and then you can download the log4j library, which will ultimately give you a log4j-xxxxx.jar (the xxxx's are the version number) which you can include in the build path of your project.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now