Hi All,
I
need help with this one. What I am trying to do is set up a default
initialization servlet as explained in the manual
http://logging.apache.org/log4j/1.2/manual.html (see below)
What
I would like to do is determine in the Log4jInit servlet's init method
the hostname of the server and load the property file accordingly. For
example if the hostname is localhost I would load the property file
that writes to a console and if the hostname is my production url load
the property file that writes to a lof file....
I have been searching for a couple of days on this now without luck. Anyone
have any ideas on how I can do this?
Initialization servlet
It is also possible to use a special servlet for log4j
initialization. Here is an example,
package com.foo;
import org.apache.log4j.PropertyConfigurator;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.io.IOException;
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) {
}
}
Define the following servlet in the web.xml file for your web-application.
<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.lcf</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Writing an initialization servlet is the most flexible way for
initializing log4j. There are no constraints on the code you can place
in the init() method of the servlet.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
email: [email protected]