hello
I am new to Log4j and i am having a problem with using log4j in
servlets.
Below i am giving the code:
_____________________________________________________
SetupServlet.java
import java.io.File;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.log4j.*;
public class SetupServlet extends HttpServlet
{
public void init(ServletConfig config)throws ServletException
{
super.init(config);
String props=config.getInitParameter("props");
if(props==null||props.length()==0||!(new File(props).isFile()))
{
System.err.println("ERROR: cannot read the configuration file"+
"Please check the path of the config init parameterin web.xml");
throw new ServletException();
}
// look up another init parameter that tells whether to watch this
// configuration file for changes.
String watch=config.getInitParameter("watch");
// use the props file to load up configuration parameters for log4j
if(watch!=null && watch.equalsIgnoreCase("true"))
{
PropertyConfigurator.configureAndWatch(props);
}
else
{
PropertyConfigurator.configure(props);
}
}
public void destroy()
{
super.destroy();
}
}
______________________________________________________
log4j.properties
log4j.logger.foo=ERROR, A0
log4j.logger.deleted_log=INFO, A1
log4j.appender.A0=org.apache.log4j.FileAppender
log4j.appender.A0.File=D:\Program Files\Apache Software Foundation\Tomcat 5.0\logs\foo.log
log4j.appender.A1=org.apache.log4j.FileAppender
log4j.appender.A1.File=D:\Program Files\Apache Software Foundation\Tomcat 5.0\logs\deleted_log.log
# Define appenders for your logging. These will show which messages to log.
log4j.appender.A0.layout=org.apache.log4j.SimpleLayout
log4j.appender.A1.layout=org.apache.log4j.SimpleLayout
__________________________________________________________________
web.xml
<servlet>
<servlet-name>setup</servlet-name>
<servlet-class>SetupServlet</servlet-class>
<init-param>
<param-name>props</param-name>
<param-value>D:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\logforj\WEB-INF\log4j.properties</param-value>
</init-param>
<init-param>
<param-name>watch</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup/>
</servlet>
<servlet>
<servlet-name>configure</servlet-name>
<servlet-class>LogServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>configure</servlet-name>
<url-pattern>/ls</url-pattern>
</servlet-mapping>
_______________________________________________________________
i have copied my log4j-1.2.8.jar file in
F:\logforj\WEB-INF\lib\log4j-1.2.8.jar
I am using
tomcat container.
I am not understanding how to proceed further with the example.....ie i would like to write another servlet which writes the messages to the log files specified in the log4j.properties file.
i have tried writing the servlet as shown below
___________________________________________
LogServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.log4j.*;
public class LogServlet extends HttpServlet
{
public void service(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
Logger foo_log = Logger.getLogger("foo");
foo_log.info("Inside whatever method");
}
}
_____________________________
I am lost........I think the code of LogServlet.java is wrong.please can anyone guide me thru the coding that has to be written in the second servlet ie.LogServlet.java............