We use an alternative approach that allows for a more dynamic log4j
configuration approach. In our webapps we have a Servlet that loads on
startup that is responsible for configuring log4j and updating the log4j
configuration when the configuration file changes.
<web.xml-sinppets>
<servlet>
<servlet-name>Log4JConfigurator</servlet-name>
<servlet-class>edu.vanderbilt.mis.log4j.ConfigureLog4jServlet</servlet-class
>
<init-param>
<param-name>reloadDelay</param-name>
<param-value>300000</param-value>
</init-param>
<init-param>
<param-name>propertyFile</param-name>
<param-value>/log4j/fis/config/edog.log4j.properties</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Log4JConfigurator</servlet-name>
<url-pattern>/config</url-pattern>
</servlet-mapping>
</web.xml-sinppets>
<servlet-code>
package edu.vanderbilt.mis.log4j;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import org.apache.log4j.*;
import org.apache.log4j.spi.*;
import org.apache.log4j.helpers.*;
import java.net.URL;
/**
* Handles the initialization of Log4j. On startup, this method with deifne
* the reload frequency of the property file and where the property file is
* located.
* <p>
* This servlet expects two properties to be defined as configuration
* properties. They are as follows...
* <ul>
* <li>reloadDelay - the number of miliseconds to wait before reloading the
property file.</li>
* <li>propertyFile - the property file to be reloaded.</li>
* </ul>
* @author unascribed
* @version 1.0
*/
public class ConfigureLog4jServlet extends HttpServlet
{
public static final String RELOAD_DELAY_PROPERTY_NAME = "reloadDelay";
public static final String PROPERTY_FILE_PROPERTY_NAME = "propertyFile";
/**
* Handles the initialization of Log4j. On startup, this method with
deifne
* the reload frequency of the property file and where the property file
is
* located.
*/
public void init( ServletConfig config ) throws ServletException
{
super.init( config );
long delay = getReloadDelay();
LogLog.debug( "Reload Delay is [" + delay + "]" );
String cfgFile = getPropertyFileLocation();
LogLog.debug( "Log4j configuration file is [" + cfgFile + "]" );
if ( cfgFile == null )
throw new ServletException( "Log4j configuration file was not
specified." );
PropertyConfigurator.configureAndWatch( cfgFile, delay );
}
/**
* Diaplays the reload delay and the location of the configuration file.
*/
public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
{
response.addHeader( "Cache-Control", "no-cache" );
response.addHeader( "Pragma", "no-cache" );
response.addHeader( "Expires", "-1" );
response.setContentType( "text/html" );
PrintWriter out = response.getWriter();
out.println( "<HTML><BODY>" );
out.println( "<B>Log4J Configurator</B><BR><BR>" );
out.println( "Reload Delay is [" + getReloadDelay() + "]
milliseconds<br>" );
out.println( "Configuration File is [" + getPropertyFileLocation() +
"]" );
File logConfigFile = new File( getPropertyFileLocation() );
if ( !logConfigFile.exists() )
{
out.println( "<p>Configuration file does not exist</p>" );
}
else
{
out.println( "<p>Configuration file contents</p>" );
out.println( "<pre>" );
FileReader fReader = new FileReader( logConfigFile );
BufferedReader bReader = new BufferedReader( fReader );
String line = null;
while ( (line = bReader.readLine() ) != null)
{
out.println( line );
}
}
out.println( "</pre>" );
out.println( "</BODY></HTML>" );
out.close();
}
/**
* Returns the reload delay for the Log4J configuration file. This
method
* first checks for the reload delay property in the web.xml. If it is
not
* set in the web.xml the default is used as defined in the FileWatchDog.
*/
public long getReloadDelay()
{
String s = getServletConfig().getInitParameter(
RELOAD_DELAY_PROPERTY_NAME );
try
{
return new Long( s ).longValue();
}
catch ( NumberFormatException nfe )
{
return FileWatchdog.DEFAULT_DELAY;
}
}
/**
* Returns the location of the log4j property file. The property file
location
* should be an init parameter for this servlet.
*/
public String getPropertyFileLocation()
{
return getServletConfig().getInitParameter(
PROPERTY_FILE_PROPERTY_NAME );
}
}
</servlet-code>
--
Norm Deane
MIS Consultant
Vanderbilt University
(615) 322-7855
[EMAIL PROTECTED]
> -----Original Message-----
> From: Geeta Ramani [mailto:[EMAIL PROTECTED]
> Sent: Wednesday, January 28, 2004 6:01 AM
> To: Struts Users Mailing List
> Subject: Re: log4j integration
>
>
> Please forgive this note if it seems too obvious and I have
> misunderstood your question/comments..
>
> If you think of both struts and log4j as just a bunch of Java
> packages/classes, then you will see that "integrating struts
> with log4j" is really nothing unusual. The log4j jar (or
> classes in the jar) needs a log4j.properties file (the name
> of this file is most probably hard-coded in the classes in
> log4j jar, which is why you don't need to personally tie the
> file with log4j) and this file should therefore be in the
> classpath and as you have found out yourself, this is all
> that is required...If for some reason you need a handle to
> this file, I don't see why the usual File class in the
> java.io package won't do the trick..?
>
> As for how to ensure that changes in your log4j.properties is
> made visible to struts, well, I guess you'll have to restart
> tomcat (or your app server)..(:( I don't believe the
> admin/reload.do will work (that works for changes to the
> struts-config.xml and such a blessing *that* is during
> development!!) ..
>
> Hope this helps,
> Geeta
>
> shankarr wrote:
>
> > Hi!
> >
> > In fact, I got two responses for the question as I had sent
> the same
> > question twice. My mailbox was acting funny yesterday, so I had to
> > send it twice.
> >
> > Well, I do have the log4j.properties file under <web-inf>/classes I
> > had thought of using the PropertyConfigurator too. But, when you
> > integrate log4j with struts, we no where mention the
> log4j.properties
> > file specifically, ie, we just create the file and put it under the
> > mentioned location. And when we use it, we do not mention the
> > log4j.properties file anywhere in the files too.
> > So, how to get a handle to the file?
> > Correct me if there are better approaches to the same.
> >
> > Steps by which I integrated are :
> >
> > a)Got the log4j.jar file
> > b)created the log4j.properties file
> > c)created commons-logging.properties file
> > d) used static logger = new logger(this) in the files where this is
> > the class name.
> >
> > TIA,
> >
> > Richie
> >
> > At 02:28 PM 1/27/2004 -0600, you wrote:
> > >Do you mean changes made after the initial configuration
> of log4j?
> > >You can use
> > >
> > > PropertyConfigurator.configureAndWatch( cfgFile, delay )
> > >
> > >This will load your log4j configuration file and create a
> thread that
> > >looks for changes to the config file reloading when necessary.
> > >
> > >--Norm
> > >
> > >--
> > >Norm Deane
> > >MIS Consultant
> > >Vanderbilt University
> > >(615) 322-7855
> > >[EMAIL PROTECTED]
> > >
> > > > -----Original Message-----
> > > > From: shankarr [mailto:[EMAIL PROTECTED]
> > > > Sent: Tuesday, January 27, 2004 9:14 AM
> > > > To: [EMAIL PROTECTED]
> > > > Subject: log4j integration
> > > >
> > > >
> > > > Hi!
> > > > I have integrated log4j with struts.
> > > > I need to know how to ensure that the changes done to
> > > > log4j.properties file is taken into account at run time.
> > > > TIA,
> > > > Richie
> > > >
> > > > "To achieve all that is possible, one must attempt the
> impossible"
> > > >
> > > >
> > > >
> > > >
> ------------------------------------------------------------------
> > > > ---
> > > > To unsubscribe, e-mail:
> [EMAIL PROTECTED]
> > > > For additional commands, e-mail:
> [EMAIL PROTECTED]
> > > >
> > > >
> > >
> > >
> >
> >---------------------------------------------------------------------
> > >To unsubscribe, e-mail: [EMAIL PROTECTED]
> > >For additional commands, e-mail:
> [EMAIL PROTECTED]
> >
> > "To achieve all that is possible, one must attempt the impossible"
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]