• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Error using log4j in webapp

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all, I'm a beginner to web application
I tried to create a simple web application, namely WebTest. It only contains one servlet, like this:


package solmit;

import java.io.IOException;
import java.io.PrintWriter;

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

import org.apache.log4j.Logger;;

public class WebTest extends HttpServlet {
private static Logger log = Logger.getLogger(WebTest.class);

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
log.debug("anas: about to enter doGet");
response.setContentType("text/html");
PrintWriter writer = response.getWriter();

writer.println("<html><head><title>Simple Web Application</title></head>");
writer.println("<body><h1>A Simple Web Application</h1>");
writer.println("Here's some text...<br>...and a bit more text");
writer.println("</body></html>");
log.debug("anas: about to leave doGet");
}
}


My web.xml file looks like this:


I also created log4j.properties file in <web-app>/WEB-INF/classes directory. It contains the following code:

I packaged this application into WebTest.war.
When I dropped this package into my Tomcat (Tomcat 5.5), and tried to access web app using this url :http://localhost:8080/WebTest/start-web-test
I got error messages on my stderr_20080522 log:


Oh, I've also dropped log4j-1.2.13.jar and commons-logging-1.0.4.jar on Tomcat/common/lib
I've read tomcat documentation on logging.. but it seems that it doesn't give me some clue to use log4j logging in my application ("http://localhost:8080/tomcat-docs/logging.html")
I've also already read the documentation about log4j on "http://www.laliluna.de/log4j-tutorial.html"
Could someone give an insight on this ??
Any help would be appreciated
Spec:
Tomcat 5.5
Jre 1.5.0_15
Windows XP Prof edition
Thanks
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"anas java",
Please check your private messages.
-Ben
 
Anas Nashroel
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ben,
Thanks for the reply. After I changed as what you said, I managed to display the output from the servlet. But, the error log4j configuration still remains..and no debug messages appear on my log file.
Error from stderr_20080522.log:


log4j:WARN No appenders could be found for logger (org.apache.catalina.startup.Embedded).
log4j:WARN Please initialize the log4j system properly.



How do we actually setup log4j in our web apps ??
Does anyone have the same experience ??
Thanks
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where did you put your log4j.properties file?
 
Anas Nashroel
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I put it in <my-web-app>/WEB-INF/classes on my web application
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post your log4j.properties file?
 
Anas Nashroel
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's my log4j.properties file:


log4j.logger.solmit=ALL, A
log4j.logger.org.apache=OFF, A

log4j.appender.A=org.apache.log4j.ConsoleAppender
log4j.appender.A.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n



The problem is already solved, Ben...
Actually, I followed the instruction on log4j docs..
I created initialization servlet, Log4jInit.class, that initializes log4j and loaded on startup...
This initialization class looks like this:


public class Log4jInit extends HttpServlet {

public
void init() {
String prefix = getServletContext().getRealPath("/");
String file = getInitParameter("log4j-init-file");
// if the log4j-init-file is not set, then no point in trying
if(file != null) {
PropertyConfigurator.configure(prefix+file);
}
}

public
void doGet(HttpServletRequest req, HttpServletResponse res) {
}
}



and adding this entry to web.xml file:


<servlet>
<servlet-name>log4j-init</servlet-name>
<servlet-class>com.foo.Log4jInit</servlet-class>

<init-param>
<param-name>log4j-init-file</param-name>
<param-value>WEB-INF/classes/log4j.properties</param-value>
</init-param>

<load-on-startup>1</load-on-startup>
</servlet>




In the class above, the static method configure(java.util.Properties properties) of PropertyConfigurator class is called to initialize log4j

When the tomcat starts, the logs are printed to stderr_20080522.log
 
reply
    Bookmark Topic Watch Topic
  • New Topic