Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4j.properties being ignored

I have a Java EE 6 Wicket application deployed with maven using IntelliJ IDEA 9.0.3 on glassfish v3.0.1. I use slf4j-log4j12-1.5.6 with slf4j-api-1.5.8 and log4j-1.2.16 for logging.

It was previously working fine when I deployed through netbeans or eclipse, however when I deploy with IntelliJ IDEA my log4j.properties file is ignored and glassfish's logging handles my log messages. I do not think IDEA has anything to do with it, something else must have changed I just can't figure out what.

I have verified that my log4j.properties file is in my WEB-INF/classes directory and the slf4j/log4j jars are in the WEB-INF/lib directory of my war. Is there some sort of configuration I am missing to make this work?

thanks.

edit: Updated with more info, posted pom dependencies.

Here is the relevant section from my pom.xml:

    <!-- Guava -->

    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>r05</version>
    </dependency>

    <!-- Test -->

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.7</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.glassfish.extras</groupId>
        <artifactId>glassfish-embedded-all</artifactId>
        <version>3.0</version>
        <scope>test</scope>
    </dependency>

    <!-- Java EE 6 -->

    <dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>bean-validator</artifactId>
        <version>3.0-JBoss-4.0.0.Beta3</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>6.0</version>
        <scope>provided</scope>
    </dependency>

    <!-- Wicket -->

    <dependency>
        <groupId>org.apache.wicket</groupId>
        <artifactId>wicket</artifactId>
        <version>1.4.9</version>
    </dependency>
    <dependency>
        <groupId>org.apache.wicket</groupId>
        <artifactId>wicket-auth-roles</artifactId>
        <version>1.4.9</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.weld</groupId>
        <artifactId>weld-wicket</artifactId>
        <version>1.0.1-Final</version>
    </dependency>

    <!-- Hibernate -->

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.5.1-Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-annotations</artifactId>
        <version>3.5.1-Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-commons-annotations</artifactId>
        <version>3.2.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>3.5.1-Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-c3p0</artifactId>
        <version>3.5.1-Final</version>
    </dependency>


    <!-- Database -->

    <dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>8.4-701.jdbc4</version>
    </dependency>


    <!-- Logging -->

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.5.6</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.16</version>
    </dependency>

</dependencies>
like image 608
kgrad Avatar asked Aug 09 '10 21:08

kgrad


People also ask

What are the system properties checked by log4j?

log4j uses 6 levels, namely TRACE, DEBUG, INFO, WARN, ERROR and FATAL.

Is log4j properties required?

This is not mandatory. Root logger does not have a default appender attached and it can exist without an appender. So, your log4j properties file can be without this property being set.

Where does log4j properties go?

log4j. properties should be in your classpath.


1 Answers

Update: I tried to reproduce the issue. I created a simple Wicket project (same version as you):

mvn archetype:create \
-DarchetypeGroupId=org.apache.wicket \
-DarchetypeArtifactId=wicket-archetype-quickstart \
-DarchetypeVersion=1.4.9 \
-DgroupId=com.mycompany \
-DartifactId=my-wicketapp 

Which has a simple log4j.properties logging to the standard output.

log4j.appender.Stdout=org.apache.log4j.ConsoleAppender
log4j.appender.Stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.Stdout.layout.conversionPattern=%-5p - %-26.26c{1} - %m\n

log4j.rootLogger=INFO,Stdout

log4j.logger.org.apache.wicket=INFO
log4j.logger.org.apache.wicket.protocol.http.HttpSessionStore=INFO
log4j.logger.org.apache.wicket.version=INFO
log4j.logger.org.apache.wicket.RequestCycle=INFO

Then:

  • I added all your dependencies (or modified the versions of existing one to match yours)
    • I just did some cleanup e.g. in the Hibernate dependencies, you don't need to declare them all, leverage the transitive dependencies mechanism
  • I added relevant repositories and pluginRepositories
  • I added glassfish's javax.servlet dependency to make the build pass
  • I added the embedded-glassfish plugin to test the whole thing
  • I made a few other unrelated changes
    • I changed the compiler settings to 1.6
    • I declared slf4j-api in the dependencyManagement element to control nicely the version in transitive dependencies.

The full pom.xml looks like this (so anybody can reproduce):

<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.mycompany</groupId>
  <artifactId>my-wicketapp</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <!-- TODO project name  -->
  <name>quickstart</name>
  <description/>
  <!--
        TODO <organization> <name>company name</name> <url>company url</url>
        </organization>
    -->
  <licenses>
    <license>
      <name>The Apache Software License, Version 2.0</name>
      <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
      <distribution>repo</distribution>
    </license>
  </licenses>
  <repositories>
    <!-- For Hibernate Artifacts -->
    <repository>
      <id>repository.jboss.org-public</id>
      <name>JBoss repository</name>
      <url>https://repository.jboss.org/nexus/content/groups/public</url>
    </repository>
    <!-- repository for Java EE 6 Binaries -->
    <repository>
      <id>java.net2</id>
      <name>Repository hosting the jee6 artifacts</name>
      <url>http://download.java.net/maven/2</url>
    </repository>
  </repositories>
  <pluginRepositories>
    <!-- GlassFish repository for the embedded-glassfish plugin -->
    <pluginRepository>
      <id>glassfish</id>
      <name>GlassFish Maven 2 Repository</name>
      <url>http://download.java.net/maven/glassfish</url>
    </pluginRepository>
  </pluginRepositories>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${slf4j.version}</version>
      </dependency>
    </dependencies>
  </dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>3.5.5-Final</version>
    </dependency>
    <dependency>
      <groupId>com.google.guava</groupId>
      <artifactId>guava</artifactId>
      <version>r05</version>
    </dependency>
    <dependency>
      <groupId>org.glassfish</groupId>
      <artifactId>javax.servlet</artifactId>
      <version>3.0.1</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.glassfish</groupId>
      <artifactId>bean-validator</artifactId>
      <version>3.0-JBoss-4.0.0.Beta3</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax</groupId>
      <artifactId>javaee-api</artifactId>
      <version>6.0</version>
      <scope>provided</scope>
    </dependency>
    <!--  WICKET DEPENDENCIES -->
    <dependency>
      <groupId>org.apache.wicket</groupId>
      <artifactId>wicket</artifactId>
      <version>${wicket.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.wicket</groupId>
      <artifactId>wicket-auth-roles</artifactId>
      <version>${wicket.version}</version>
    </dependency>
    <dependency>
      <groupId>org.jboss.weld</groupId>
      <artifactId>weld-wicket</artifactId>
      <version>1.0.1-Final</version>
    </dependency>
    <!--
            OPTIONAL <dependency> <groupId>org.apache.wicket</groupId>
            <artifactId>wicket-extensions</artifactId>
            <version>${wicket.version}</version> </dependency>
        -->
    <!-- LOGGING DEPENDENCIES - LOG4J -->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>${slf4j.version}</version>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.16</version>
    </dependency>
    <!--  JUNIT DEPENDENCY FOR TESTING -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.7</version>
      <scope>test</scope>
    </dependency>
    <!-- GLASSFISH EMBEDDED FOR TESTING -->
    <dependency>
      <groupId>org.glassfish.extras</groupId>
      <artifactId>glassfish-embedded-all</artifactId>
      <version>3.0.1</version>
      <scope>test</scope>
    </dependency>
    <!--  JETTY DEPENDENCIES FOR TESTING  -->
    <dependency>
      <groupId>org.mortbay.jetty</groupId>
      <artifactId>jetty</artifactId>
      <version>${jetty.version}</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.mortbay.jetty</groupId>
      <artifactId>jetty-util</artifactId>
      <version>${jetty.version}</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.mortbay.jetty</groupId>
      <artifactId>jetty-management</artifactId>
      <version>${jetty.version}</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>
  <build>
    <resources>
      <resource>
        <filtering>false</filtering>
        <directory>src/main/resources</directory>
      </resource>
      <resource>
        <filtering>false</filtering>
        <directory>src/main/java</directory>
        <includes>
          <include>**</include>
        </includes>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <filtering>false</filtering>
        <directory>src/test/java</directory>
        <includes>
          <include>**</include>
        </includes>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </testResource>
    </testResources>
    <plugins>
      <plugin>
        <inherited>true</inherited>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
          <optimize>true</optimize>
          <debug>true</debug>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>maven-jetty-plugin</artifactId>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-eclipse-plugin</artifactId>
        <configuration>
          <downloadSources>true</downloadSources>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.glassfish</groupId>
        <artifactId>maven-embedded-glassfish-plugin</artifactId>
        <version>3.0.1</version>
        <configuration>
          <serverID>server</serverID>
          <name>server</name>
          <app>${project.build.directory}/${project.build.finalName}.war</app>
          <port>8080</port>
          <instanceRoot>${project.build.directory}/gfe-${maven.build.timestamp}</instanceRoot>
          <!--contextRoot>${build.finalName}</contextRoot-->
          <autoDelete>true</autoDelete>
          <!--configFile>${basedir}/domain.xml</configFile-->
        </configuration>
      </plugin>
    </plugins>
  </build>
  <properties>
    <wicket.version>1.4.9</wicket.version>
    <jetty.version>6.1.4</jetty.version>
    <slf4j.version>1.5.6</slf4j.version>
  </properties>
</project>

And when I run the project with the embedded-glassfish plugin:

$ mvn package
...
$ mvn embedded-glassfish:run
...

and access http://localhost:8080/server in a browser, I get my logs in the standard output as expected:

...
INFO: [WicketApplication] Started Wicket version 1.4.9 in development mode
********************************************************************
*** WARNING: Wicket is running in DEVELOPMENT mode.              ***
***                               ^^^^^^^^^^^                    ***
*** Do NOT deploy to your live server(s) without changing this.  ***
*** See Application#getConfigurationType() for more information. ***
********************************************************************

I wonder if this is representative or not.


I have checked the war, log4j.properties is indeed in WEB-INF/classes. I don't have a log4j.jar, i have slf4j-log4j12.jar.

slf4j-log4j12.jar is not a replacement for log4j.jar, slf4j-log4j12.jar is a binding for log4J version 1.2, you still need log4j.jar. From the SLF4J documentation:

Binding with a logging framework at deployment time

As mentioned previously, SLF4J supports various logging frameworks. The SLF4J distribution ships with several jar files referred to as "SLF4J bindings", with each binding corresponding to a supported framework.

slf4j-log4j12-1.6.1.jar: Binding for log4j version 1.2, a widely used logging framework. You also need to place log4j.jar on your class path.

I wonder how you got this working under NetBeans and Eclipse.

like image 130
Pascal Thivent Avatar answered Oct 21 '22 12:10

Pascal Thivent