0

Where do I put my log4j.properties file in my Eclipse Project Structure so that its found during this operation:

PropertyConfigurator.configure("log4j.properties");  

I found the following link:

Where is the correct location to put Log4j.properties in an Eclipse project?

But as per the answer (selected) ,I think the setting is for the server itself and would be
applied to all projects.But I want this .properties file for my concerned project only.

My project structure is:

AccountCeation //project name  
    Java Resources: src  
        com.alw.controllers //packages  
        .....  
        log4j.properties  

I always get an FileNotFoundException for it.

NOTE: I am developing a Dynamic Web Application with Spring Framework.

Please guide me where I am missing ?

Community
  • 1
  • 1
mukund
  • 2,866
  • 5
  • 31
  • 41

1 Answers1

0

I was able to find the solution to this problem running a Eclipse Dynamic Web Project in Apache Tomcat 6. There may be a more elegant way in Spring to access the context and load the resource.

Two basic steps

(1) Get the log4j.properties file into the "class directory" of the war file.

(2) Read the log4j properties file out of the current context. I found the best way to do this is to access the current thread's context and work from there.

For the first step above, alter the Eclipse build process to add an additional directory that will eventually load into the WEB-INF/classes directory in the war file. Specifically....

(1) In Eclipse, right click your project in the project explorer, select 'New'->'Folder'. You can name the folder anything, but the standard in this case is 'resources'. The new folder should appear at the root level of your project.

(2) Move the log4j.properties file into this new folder.

(3) Right click the project again, and select 'Build-Path'->'Configure Build Path'. Select the 'Sources' tab. Click the 'Add Folder' button. Browse to find your new folder you created in step (1) above. Select 'OK'.

(4) Once back to the eclipse Project Explorer view, note that the folder has now moved to the 'Java Resources' area (ie it's no longer at the root due to eclipse presentation abstraction).

(5) Clean build your project.

(6) To validate that the .properties file now exists in WEB-INF/classes in your war file, export a war file to an easy location (right click Project -> Export -> War file) and checkout the contents. Note that the log4j.properties file now appears in the WEB-INF/classes.

Now for the second step above, accessing the context to read the file. Add the following code where attempting to read the file. Note that this reads this out of the war file context, so this 'should' work as the war file moves from server to server.

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
PropertyConfigurator.configure(classLoader.getResourceAsStream("log4j.properties") );
Robert Bender
  • 2,639
  • 1
  • 12
  • 8