3

I am facing issues using log4j with Maven. I've one properties file i.e log4j.properties and I've put that file at the same path where project's pom.xml is stored.

pom.xml

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
    <scope>test</scope>
</dependency>

I've used log4j in my code which is under test folder.

Code

package com.example.automationtest;

import org.apache.log4j.*;
import org.junit.Test;

public class AppTest {
    @Test
    public void testLogger(){
   //PropertyConfigurator.configure("C:\\Users\\Desktop\\AutomationTest\\log4j.properties");
    Logger log = Logger.getLogger("exampleLogger");
    log.debug("Hello, World!");
   }
}

I would like to know, how does maven identify where the log4j.properties file is located?

In the above scenario if I run the command mvn test it gives a warning, please check the screenshot below for warning messages.

enter image description here

so as a workaround I am providing the complete path of log4j.properties in my code. I've used below line:

PropertyConfigurator.configure("C:\\Users\\Desktop\\AutomationTest\\log4j.properties")

Is it necessary to use the above line of code, or is there any specific directory where maven looks for log4j.properties file?

Paras
  • 3,197
  • 2
  • 20
  • 30
  • 1
    It is not related to maven rather how log4j works. IMO, log4j expects to finds the 'properties' file available on the classpath. – Arnaud Denoyelle Jul 21 '14 at 12:49
  • so basically I need to set the class path by providing the location of log4j.properties file? – Paras Jul 21 '14 at 12:51
  • 1
    For tests, the directory `src/test/resources` is the appropriate location for the `log4j.properties` file. When Maven starts the tests, this directory will be part of the classpath. – Seelenvirtuose Jul 21 '14 at 12:52
  • I suppose Maven itself is agnostic on log4j. However, you should take care that `log4j.properties` appears in the root of your classpath, so /src/main/resources/ would be the proper place (should then appear in target/classes/ folder) – Gyro Gearless Jul 21 '14 at 12:52
  • @Seelenvirtuose so when I make a folder named resources, do I need to mention the folder path anywhere? – Paras Jul 21 '14 at 12:53
  • No, simply put the file into that directory. – Seelenvirtuose Jul 21 '14 at 12:53
  • @Seelenvirtuose then I've one more query... when I created the folder using `mvn archetype:generate`, it didn't create any resources folder.. so how will it identify that the properties file is located in resources folder? – Paras Jul 21 '14 at 12:54
  • Maybe you should read about the [Maven Standard Directory Layout](http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html). Maven will already consider those directories out of the box. – Seelenvirtuose Jul 21 '14 at 12:56
  • yep it worked fine ... created one resource folder under test directory. Thanks a lot! :) – Paras Jul 21 '14 at 12:59
  • This is a duplicate. Best answer is this: https://stackoverflow.com/a/18918600/10335 – neves Apr 10 '18 at 18:17
  • Possible duplicate of [If using maven, usually you put log4j.properties under java or resources?](https://stackoverflow.com/questions/5132389/if-using-maven-usually-you-put-log4j-properties-under-java-or-resources) – neves Apr 10 '18 at 18:19

2 Answers2

11

The file needs to go into src/main/resources/ or src/test/resources/ (if you need it only for unit tests).

Longer explanation: Maven splits the Java classpath into source code and resources (non-Java things like property files, images, etc). The main reason for this is that Maven can do filtering of resources for you and to make this extra safe, they put the resources into a different folder than the sources.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
1

There's a lot of answers relating to the popular solution src/main/resources or the src/test/resources but generally not having the log4j.properties in your application can be useful. Rather than providing a hardcoded log4j.properties in your code, leave it to the client (app-server, stage environment, etc) to configure the desired logging. So, I would suggest to be cautious on the specifics of your deployment to make the decision of having the log4j.properties included or excluded of your maven assembly.

Reference: If using maven, usually you put log4j.properties under java or resources?