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

196,815

Solution 1

you can add it any where you want, when you run your project, configure the classpath and add the location of the log4j.properties files by clicking on: Run->Run Configuration -> [classpath tab] -> click on user Entries -> Advanced -> Select Add Folder -> select the location of your log4j.properties file

and then -> OK -> run

and it should get loaded

Solution 2

The safest way IMO is to point at the file in your run/debug config

-Dlog4j.configuration=file:mylogging.properties

! Be aware: when using the eclipse launch configurations the specification of the file: protocol is mandatory.

In this way the logger will not catch any logging.properties that come before in the classpath nor the default one in the JDK.

Also, consider actually use the log4j.xml which has a richer expression syntax and will allow more things (log4j.xml tahe precedence over log4j.properties.

Solution 3

Add the log4j.properties file to the runtime class path of the project. Some people add this to the root of the source tree (so that it gets copied to the root of the compiled classes).

Edit: If your project is a maven project, you can put the log4j.properties in the src/main/resources folder (and the src/test/resources for your unit tests).

If you have multiple environments (for example development and production), want different logging for each environment, and want to deploy the same jar (or war, or ear) file to each environment (as in one build for all environments) then store the log4j.properties file outside of the jar file and put it in the class path for each environment (configurable by environment). Historically, I would include some known directory in each environment in the classpath and deploy environment specific stuff there. For example, ~tomcat_user/localclasspath where ~tomcat_user is the home directory of the user that will be running the tomcat instance to which my war file will be deployed.

Solution 4

The best way is to create special source folder named resources and use it for all resource including log4j.properties. So, just put it there.

On the Java Resources folder that was automatically created by the Dynamic Web Project, right click and add a new Source Folder and name it 'resources'. Files here will then be exported to the war file to the classes directory

Solution 5

If you have a library and you want to append the log4j:

  1. Create a folder named "resources" in your projet.
  2. Create a .properties file named log4j
  3. Set properties in log4j.properties file
  4. Push right button in the project and go to properties->Java Build Path and, finally, go to the "Source" tab.
  5. Push Add folder and search the "resources" folder created in step 1.
  6. Finish.

(I have assumed that you have the log4j library added.)

PD: Sorry for my english.

Share:
196,815
Srinivas Reddy
Author by

Srinivas Reddy

Updated on December 22, 2021

Comments

  • Srinivas Reddy
    Srinivas Reddy over 2 years

    Where in my Eclipse project should I add the log4j.properties file so that it will work as intended?