1

I have my log4j.xml file stored in the project directory but i am getting following error:

log4j:WARN No appenders could be found for logger (p1.Employee).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more 
info.

When i palced it in the bin folder Everything works fine. But i dont check in the bin folder in the code repository so i dont want to place it there.

I dont want to use DOMConfigurator.configure("log4j.xml") as well

Below is my Project Stucture.

enter image description here

user2048204
  • 729
  • 4
  • 13
  • 27
  • 1
    Maven `resources` directory is under classpath, so you should place it inside `src` – 11thdimension Jun 22 '17 at 14:04
  • Possible duplicate of [Where is the correct location to put Log4j.properties in an Eclipse project?](https://stackoverflow.com/questions/5081316/where-is-the-correct-location-to-put-log4j-properties-in-an-eclipse-project) – Mike Adamenko Jun 22 '17 at 14:25

2 Answers2

1

In a normal Java Project, you can place a log4j configuration file, i.e., a log4j.properties or a log4j.xml, in a resources directory. The resources directory should be on the same level as src. Putting it here, will allow log4j to use the configuration file automatically.

Otherwise, you can set the log4j configuration file to use via a VM option by doing something like:

-Dlog4j.configuration=file:///path/to/log4j.xml 

In your case, if you want to place it in the TrailProject project directory, you can do

-Dlog4j.configuration=file:///path/to/TrailProject/log4j.xml
Ishnark
  • 661
  • 6
  • 14
  • Thank you for reply but I have already put it on the same level as src . – user2048204 Jun 22 '17 at 14:24
  • What is on the same level as `src`? The `resources` directory or the configuration file? – Ishnark Jun 22 '17 at 14:36
  • I have the configuration file which is at same level as src. If i add the resources folder (with the configuration file) then i need to add this resources folder to class path in eclipse. But then person who checkc out my code will have to again add the resources folder to classpath . is it? – user2048204 Jun 27 '17 at 12:35
  • How do people use your code? I'm under the impression that your code will be made an executable JAR. – Ishnark Jun 27 '17 at 12:56
  • No. This is test automation code. And hence we are directly running using test Ng.xml file . – user2048204 Jun 27 '17 at 13:06
  • If it is NetBeans project file path must be like that: `Dlog4j.configuration=resources/log4j.properties`. In addition, I added `resources` directory as a source directory in project options. – Halil İbrahim Oymacı Jul 18 '20 at 20:41
0

As per the maven java project folder structure, the ideal path to put the config.xml would be in /src/main/resources directory, so for a non maven project you would put it in an equivalent /resources directory.

Harneet Singh
  • 2,328
  • 3
  • 16
  • 23