It looks like it should work, although it is bad form to use a servlet init rather than a servlet context listener as well as using getRealPath("/"). Avoid unnecessary File IO in webapps.

You could set log4j.debug=true in the properties file. Also, you could do a System.out.println() to test whether the path the code came up with is really valid.

Jake

Troy Davis wrote:
Hello,

I sent this message last week, but haven't seen any responses. Would anyone please point me in the right direction?

Thank You,
Troy

---

I've been trying to get log4j to load my properties file from somewhere other than WEB-INF/classes, mostly because eclipse clears out this directory when I choose Project -> Clean. I've tried a few different approaches that I found online, including this:

(inside web-app block of web.xml):

<servlet>
  <servlet-name>log4j-init</servlet-name>
  <servlet-class>com.m4.Log4jInit</servlet-class>
  <init-param>
    <param-name>log4j-init-file</param-name>
    <param-value>WEB-INF/log4j.properties</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>


(full text of Log4jInit.java, compiled and included in a jar in WEB-INF/lib):

package com.m4;
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 {
  private static final long serialVersionUID = -5104057715092254401L;
  public void init() {
    String prefix =  getServletContext().getRealPath("/");
    String file = getInitParameter("log4j-init-file");
    System.out.println("Initializing log4j with " + prefix + 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) {
    // Nothing happening here yet.
  }
}

I can see the debug line go by when init() is called in Log4jInit, but I still get an error saying that log4j is not configured properly.

What is the recommended way to load this file from some other location?

Thank You,
Troy

---------------------------------------------------------------------
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]

Reply via email to