Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure maven to use different log4j.properties files in different environments

Tags:

I want to be able to use different log4j configuration for different environments.

In my development environment, I want to use log4j.properties (A). But when I build in Maven for the production environment, I want to use log4j.properties (B).

Please tell me how to configure this in my pom.xml?

like image 519
Kedron Avatar asked Mar 03 '12 03:03

Kedron


People also ask

How do you set a log4j properties file location?

The file is named log4j. properties and is located in the $DGRAPH_HOME/dgraph-hdfs-agent/lib directory. The file defines the ROLLINGFILE appenders for the root logger and also sets the log level for the file. The level of the root logger is defined as INFO and attaches the ROLLINGFILE appender to it.

Does Maven use log4j?

As you can see in this answer, maven supports SLF4J logging. If you just add the Log4j to SLF4j adapter to the plugin. By adding this dependency, log4j will redirect to SLF4j and SLF4j redirects to the maven logging.


1 Answers

You can use profiles to achieve the desired behavior:

<build>     <plugins>         <plugin>             <groupId>org.apache.maven.plugins</groupId>             <artifactId>maven-resources-plugin</artifactId>             <version>2.5</version>             <executions>                 <execution>                     <id>log4j</id>                     <phase>process-resources</phase>                     <goals>                         <goal>copy-resources</goal>                     </goals>                     <configuration>                         <outputDirectory>output_directory</outputDirectory>                         <resources>                             <resource>${log4j.file}</resource>                         </resources>                     </configuration>                 </execution>             </executions>         </plugin>     </plugins> </build>  <profiles>     <profile>         <id>dev</id>         <activation>             <activeByDefault>true</activeByDefault>         </activation>         <properties>             <log4j.file>path_to_file_A</log4j.file>         </properties>     </profile>     <profile>         <id>prod</id>         <properties>             <log4j.file>path_to_file_B</log4j.file>         </properties>     </profile> </profiles> 
like image 161
Andrew Logvinov Avatar answered Oct 24 '22 18:10

Andrew Logvinov