*
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
Author

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";
DOMConfigurator.configure(path);


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
Sheriff

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");
DOMConfigurator.configure(url);

I know the file has loaded because I get warnings about invalid parameters to RollingFileAppender. Thanks.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: initializing log4j
 
Similar Threads
log4j: log4j:WARN No appenders could be found for logger (org.apache.commons.digester
How to read log4j.xml file path at runtime without hardcoding the path in Java Class...?
struts & Log4J
configuring log4j with jboss
About file path