File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes initializing log4j Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "initializing log4j" Watch "initializing log4j" New topic

initializing log4j

John Eric Hamacher
Ranch Hand

Joined: Apr 25, 2007
Posts: 230
I know this question is asked many times but I can't get a handle on this . . . I am trying to load the file "A3-log4j.xml" as below:

String path = "A3-log4j.xml";

My file is in /WEB-INF/classes where it should be and other classes are loading correctly. Why do I get FileNotFoundExceptions here? (Yes, I want to use DOMConfigurator so my app is portable).
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

String path = "A3-log4j.xml";

This says that the file is in the current working directory, which, chances are, isn't your WEB-INF/classes directory.

I don't explicitly initialize log4j.
I just put the config files in that directory and let it find them on it's own by traversing the classpath.

If you need to initialize it yourself, look at either getRealPath (which sometimes will return the path of a resource within your web app) or, better, look into getResourceAsStream and learn how to stream the file to log4j.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
John Eric Hamacher
Ranch Hand

Joined: Apr 25, 2007
Posts: 230
I accomplished loading the file by:

URL url = context.getResource("/WEB-INF/config/A3-log4j.xml");

I know the file has loaded because I get warnings about invalid parameters to RollingFileAppender. Thanks.
I agree. Here's the link:
subject: initializing log4j
It's not a secret anymore!