If using maven, usually you put log4j.properties under java or resources?
Some "data mining" accounts for that src/main/resources
is the typical place.
Results on Google Code Search:
src/main/resources/log4j.properties
: 4877src/main/java/log4j.properties
: 215
src/main/resources
is the "standard placement" for this.
Update: The above answers the question, but its not the best solution. Check out the other answers and the comments on this ... you would probably not shipping your own logging properties with the jar but instead leave it to the client (for example app-server, stage environment, etc) to configure the desired logging. Thus, putting it in src/test/resources
is my preferred solution.
Note: Speaking of leaving the concrete log config to the client/user, you should consider replacing log4j
with slf4j
in your app.
Just putting it in src/main/resources
will bundle it inside the artifact. E.g. if your artifact is a JAR, you will have the log4j.properties
file inside it, losing its initial point of making logging configurable.
I usually put it in src/main/resources
, and set it to be output to target like so:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<targetPath>${project.build.directory}</targetPath>
<includes>
<include>log4j.properties</include>
</includes>
</resource>
</resources>
</build>
Additionally, in order for log4j to actually see it, you have to add the output directory to the class path.
If your artifact is an executable JAR, you probably used the maven-assembly-plugin to create it. Inside that plugin, you can add the current folder of the JAR to the class path by adding a Class-Path
manifest entry like so:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.your-package.Main</mainClass>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
Now the log4j.properties file will be right next to your JAR file, independently configurable.
To run your application directly from Eclipse, add the resources
directory to your classpath in your run configuration: Run->Run Configurations...->Java Application->New
select the Classpath
tab, select Advanced
and browse to your src/resources
directory.