Use this....
---------------------------------------------------------------------
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.ActionServlet;
import org.apache.log4j.xml.DOMConfigurator ;
import org.apache.log4j.Category ;
import javax.xml.parsers.FactoryConfigurationError ;
/**
* @servletName action
* @webURLPattern *.do
* @webLoadOnStartup 1
* @servletMapping *.do
* @servletInitParam validate=true
* @servletInitParam config=/WEB-INF/struts-config.xml*/
public class PPUActionServlet extends ActionServlet
{
public void init(ServletConfig config)throws ServletException{
super.init(config);
try
{
DOMConfigurator.configure("C:/Development/log4jConfig.xml") ;
Category cat = Category.getInstance(PPUActionServlet.class) ;
cat.info("Log4j has initialized sucessfully") ;
}catch(FactoryConfigurationError fce)
{
System.err.println("Could not configure log4j: " + fce ) ;
// throw new ServletException("Could not configure log4j ", fce) ;
}
catch(Exception e)
{
System.err.println("Could not configure log4j: " + e ) ;
}
}
public String getServletInfo()
{
return "This ovedrrides the struts ActionServlet initilising log4j";
}
}
-----------------------------------------------
-----Urspr�ngliche Nachricht-----
Von: Alex Colic [mailto:[EMAIL PROTECTED]]
Gesendet am: Wednesday, January 16, 2002 3:10 PM
An: Eric Ma; [EMAIL PROTECTED]
Betreff: RE: How to use Log4j with a startup logging servlet
Hi,
I have used option two.
The code for my servlet is below. It works for me. If you have any
suggestions, enhancements please send me a note.
Alex
package com.sirius.struts.action;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.log4j.*;
public class genericLog4JServlet extends HttpServlet
{
private static final Category
cat=Category.getInstance(genericLog4JServlet.class.getName());
public void init() throws javax.servlet.ServletException
{
ServletContext context=getServletContext();
/**
* Get the Log4J settings and the logging paramters
*/
try
{
String strLogSetting
=(String)this.getInitParameter("Log4JFileSetting");
PropertyConfigurator.configure(context.getResource(strLogSetting));
}
catch(java.net.MalformedURLException e){e.printStackTrace();}
}
}
-----Original Message-----
From: Eric Ma [mailto:[EMAIL PROTECTED]]
Sent: January 15, 2002 12:33 PM
To: [EMAIL PROTECTED]
Subject: How to use Log4j with a startup logging servlet
I want to use Log4J for logging exceptions thrown in a Struts-based web app.
After searching through this list, I found 2 common solutions:
1. Extend the Struts ActionServlet and override the init() method to set up
a Log4J Category. Then in any custom Action class I can just call
servlet.Category.debug() (let's assume Category is a protected or public
variable).
2. Write a separate startup servlet and again set up LOG4J in the init()
method. But my question is, how do I invoke this servlet from my custom
Action class? Do I need to do a RequestDispatcher.forward() to the servlet
when an exception is thrown? Then how do I get back to the page I am on to
display the error message?
Eric Ma
--
This e-mail may contain confidential and/or privileged information. If you
are not the intended recipient (or have received this e-mail in error)
please notify the sender immediately and destroy this e-mail. Any
unauthorized copying, disclosure or distribution of the material in this
e-mail is strictly forbidden.
--
To unsubscribe, e-mail:
<mailto:[EMAIL PROTECTED]>
For additional commands, e-mail:
<mailto:[EMAIL PROTECTED]>
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>