Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

initializing log4j

 
John Eric Hamacher
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
John Eric Hamacher
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic