You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "Dr. Evil" <dr...@sidereal.kz> on 2001/10/20 13:09:37 UTC

Logging solution is complicated (was Re: Debugging in Tomcat 4)

Ok, I figured out how to get log4j to work with Tomcat in a
reasonable, although complicated, way:

Download the log4j files, and copy the .jar files into
TOMCAT_HOME/libs, otherwise servlets can't find them.

First, compile a servlet called startlogging.java, in the classes
directory:

----------------------
// start logging functions
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.log4j.PropertyConfigurator;
 
public class startlogging extends HttpServlet {
 
    public void init() throws ServletException {
        PropertyConfigurator.configure("/usr/local/jakarta-tomcat-4.0/webapps/myapp/WEB-INF/classes/log4j.properties");
        // or wherever your properties file is
    }
 
}
----------------------

Then, put this in the web.xml file:

----------------------
   <servlet>
      <servlet-name>startlogging</servlet-name>
      <servlet-class>startlogging</servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>
----------------------

I put it as my first servlet entry in the file.  Order is important in
that file.

Then, create the log4j.properties file, in the classes directory:

----------------------
# Set root category priority to DEBUG and its only appender to A1.
log4j.rootCategory=DEBUG, A1
 
# A1 is set to be a ConsoleAppender.
#log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1=org.apache.log4j.FileAppender
 
# set up the filename - change as appropriate
log4j.appender.A1.File=/tmp/test.log
 
# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
----------------------

Finally, in classes that you want to log from:

----------------------
class myclass {

    static Category cat = Category.getInstance(userinfo.class);   

    public void mymethod() {
    .....
    cat.info("This is an info log entry.");
----------------------

and then it will log to the file you specified in the log4j.properties
file.  That's a lot of complexity to get a line into a file, but
whatever, it's better than trying to figure out what's going on by
looking at html output from a servlet...

The other problem here is that it uses the FileAppender method from
the log4j package.  I'm not sure what's wrong with this method, but
they say it is depracated, slated for removal, and there doesn't seem
to be any replacement for it.  Sometimes all you want is to put a line
in a file.  When you can't figure out what's going on, nothing is
better than falling back on a good old printf("we're here\n");.  I am
glad to here that java 1.4 is getting an assert() facility.  Maybe a
log() facility would be another good addition to that.