Maven – How to configure log4j using Netbeans and Maven for standalone app

log4jmavennetbeans6.5

Setting up a new standalone app and want to use log4j. Am using Maven on Netbeans (6.5), and was able to add the log4j jar/dependency, but it can't seem to find my log4j.xml file and I'm not sure how to tell Netbeans to find it.

Here's my error:

log4j:WARN No appenders could be found for logger (com.domain.project).
log4j:WARN Please initialize the log4j system properly.    

log4j.xml path is src/main/resources/com/domain/product/gui/resource/log4j.xml

Note: the location of the log4j.xml was chosen to be consistent with legacy code (i.e. so future maintainers can find it), but this could be changed if necessary.

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.domain.project</groupId>
  <artifactId>project-log</artifactId>
  <packaging>jar</packaging>
  <version>0.1-SNAPSHOT</version>
  <name>project-log</name>
  <url>http://maven.apache.org</url>
  <build> 
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.0.2</version>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.9</version>
        </dependency>
    <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.14</version>
            <scope>compile</scope>
        </dependency>
  </dependencies>
</project>

Here's my very simple log4j.xml:

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

  <appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender">
      <layout class="org.apache.log4j.PatternLayout">
         <!-- The default pattern: Date Priority [Category] Message\n -->
         <param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
      </layout>
  </appender>

  <category name="com.domain.project">
    <priority value="debug"/>
  </category>

  <root>
      <priority value="info"/>
      <appender-ref ref="ConsoleAppender"/>
  </root>

</log4j:configuration>

And my call to set up logging:

    static Logger log = Logger.getLogger("com.domain.project");

How do I tell Netbeans to set up a Maven classpath to this configuration file?

Ilane

Best Answer

I recently faced the same problem when I wanted to get the logging working when the application was launched from within the IDE.

My solution is to use the additionalArguments of the nbm-maven-plugin as illustrated below.

Notice the use of -J-Dlog4j.configuration=... the extra -J ensures that the option is passed to the application, otherwise it only affects the JVM of the maven launcher.

<build>
  <pluginManagement
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>nbm-maven-plugin</artifactId>
        <extensions>true</extensions>
        <configuration>
          <brandingToken>${brandingToken}</brandingToken>
          <cluster>${brandingToken}</cluster>
          <additionalArguments>-J-Dlog4j.configuration=file:${basedir}/log4j.properties</additionalArguments>
        </configuration>
      </plugin>
    </plugins>
  </pluginManagement>
</build>
Related Topic