Where is the right place to put Log4j.properties in an Eclipse project?

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

+47
java classpath eclipse log4j
Feb 22 '11 at 17:03
source share
10 answers

you can add it wherever you want, when starting a project, setting the class path and adding the location of the log4j.properties files, by clicking on: Run-> Run Configuration → [tabpath tab] → click on user entries → Advanced → Select Add folder → select the location of the log4j.properties file

and then → OK → run

and it must be loaded

+71
Feb 22 '11 at 17:08
source share

The safest IMO way is to specify the file in your run / debug configuration

 -Dlog4j.configuration=file:mylogging.properties 

! Remember: when using eclipse startup configurations, the file: protocol specification is required.

Thus, the logger will not catch any logging.properties parameters that precede the class path or the default in the JDK.

Also consider actually using log4j.xml, which has a richer syntax for expressions and will allow more things (log4j.xml takes precedence over log4j.properties.

+28
Feb 22 '11 at 17:07
source share

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

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

If you have several environments (for example, development and production), you want a different registration for each environment, and want to deploy the same file (or war or ear) in each environment (as in one assembly for all environments) then save the file log4j.properties outside the jar file and put it in the class path for each environment (environment-friendly). Historically, I would include some well-known directory in each environment in the classpath and place environment-specific environments there. For example, ~ tomcat_user / localclasspath where ~ tomcat_user is the home directory of the user in which the tomcat instance will be launched into which my war file will be deployed.

+11
Feb 22 '11 at 17:04
source share

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

In the Java Resources folder, which was automatically created by a dynamic web project, right-click and add a new source folder and name it "resources". Then the files will be exported to the war file in the class directory

+9
Feb 22 '11 at 17:06
source share

Put log4j.properties in the path of the execution path.

http://www.coderanch.com/t/106042/vc/Eclipse-add-log-properties-classpath

+4
Feb 22 '11 at 17:05
source share

This question has already been answered here.

The classpath never includes specific files. It includes directories and jar files. So put this file in the directory that is in your classpath .

Log4j properties are not used (usually) when developing applications (unless you are debugging Eclipse itself!). So, what do you really want to create an executable Java application (application, WAR, EAR or something else) and include the properties of Log4j in the path to the runtime.

+4
Feb 22 '11 at 17:10
source share

If you have a library and you want to add log4j:

  • Create a folder named "resources" in your project.
  • Create a .properties file named log4j
  • Set properties in log4j.properties file
  • Click the right button in the project and go to properties-> Java Build Path and finally go to the "Source" tab.
  • Click "Add Folder" and find the "resources" folder created in step 1.
  • Done.

(I assumed that you added the log4j library.)

PD: Sorry for my English.

+4
Jun 06 '16 at 11:26
source share

In general, I put it in a special "res" or "resources" folder as already mentioned, but after the web application I copy log4j.properties with the ant task to the WEB-INF / classes directory. This is the same as resolving the file in the root src / directory, but usually I prefer to see it in a dedicated folder.

With Maven, the usual place to put is in the src/main/resources folder, as mentioned in this other post . All resources there will fit your assembly in the root path of the path (e.g. target/classes/ )

If you want a powerful logger, you can also take a look at the slf4j library , which is the front of the log and can use the log4j implementation for.

+2
Feb 22 2018-11-21T00:
source share

I will find out that the location of the log4j.properties file depends on the type of Eclipse project.

In particular, for the Eclipse dynamic web project, most of the answers related to adding log4j.properties to the military file do not actually add the properties file to the right place, especially for Tomcat / Apache.

Here are some of my research and my solution to the problem (again specifically for a dynamic web project running on Tomcat / Apache 6.0)

  • See this article on how Tomcat will load classes. It is different from the usual class loader for Java. ( https://www.mulesoft.com/tcat/tomcat-classpath ) Note that it only looks in two places in the war file, WEB-INF / classes and WEB-INF / lib.

  • Please note that using Dynamic Web Project it is not practical to store your .properties file in the build /../ classes directory, since this directory is cleaned up every time you clean up your project.

  • Tomcat does not process .property files in the WEB-INF / lib location.

  • You cannot save the log4j.properties file in the src directory, as Eclipse abstracts this directory from your view.

  • The only way I found to solve this problem is to change the assembly and add an additional directory, which will eventually load into the WEB-INF / classes directory in the war file. In particular....

(1) Right-click your project in the project explorer, select "Create" → "Folder". You can name the folder anything you like, but the standard in this case is "resources." A new folder should appear at the root level of your project.

(2) Move the log4j.properties file to 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. Find your new folder created in step (1) above. Select "OK."

(4) Back to the eclipse Project Explorer Project view, note that the folder has now been moved to the Java Resources area (i.e., it is no longer in the root due to the abstraction of the eclipse presentation).

(5) Clean your project.

(6) To verify that the .properties file exists in WEB-INF / classes in your war file, export the war file to a convenient location (right-click Project → Export → War file) and check the contents. Note that the log4j file .properties is now displayed in WEB-INF / classes.

(7) Contribute to the Tomcat / Apache project and note that log4j now works.

Now that log4j is working, start logging in, solve the problems of the world, take your free time and enjoy a delicious adult drink.

0
Apr 10 '14 at 22:01
source share

You do not want the log4j.properties packaged in your project to be available for deployment - this is a bad idea, as other posters talked about.

Locate the Tomcat root installation that Eclipse points to when it launches your application, and add the log4j.properties file to the desired location. For Tomcat 7, the right place is

$ {TOMCAT_HOME} / Library

0
Jul 17 '14 at 4:03
source share



All Articles