log4j.属性被忽略

2022-09-03 15:09:25

我有一个Java EE 6 Wicket应用程序,使用IntelliJ IDEA 9.0.3在glassfish v3.0.1上使用IntelliJ IDEA 9.0.3部署。我使用slf4j-log4j12-1.5.6和slf4j-api-1.5.8和log4j-1.2.16进行日志记录。

以前,当我通过netbeans或eclipse进行部署时,它工作正常,但是当我使用IntelliJ IDEA部署时,我的log4j.properties文件被忽略,glassfish的日志记录处理我的日志消息。我不认为IDEA与它有任何关系,其他一定发生了什么变化,我只是无法弄清楚是什么。

我已经验证了我的log4j.properties文件在我的WEB-INF/classes目录中,而slf4j/log4j jars在我的战争的WEB-INF/lib目录中。我是否缺少某种配置来使此功能正常工作?

谢谢。

编辑:更新了更多信息,发布了pom依赖项。

以下是我的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>

答案 1

更新:我试图重现问题。我创建了一个简单的 Wicket 项目(与你相同的版本):

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

它有一个简单的log4j.properties记录到标准输出。

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

然后:

  • 我添加了您的所有依赖项(或修改了现有依赖项的版本以匹配您的依赖项)
    • 我刚刚做了一些清理,例如在Hibernate依赖项中,您不需要全部声明它们,利用传递依赖项机制
  • 我添加了相关和repositoriespluginRepositories
  • 我添加了 glassfish 的依赖项以使构建通过javax.servlet
  • 我添加了插件来测试整个事情embedded-glassfish
  • 我做了一些其他不相关的更改
    • 我将编译器设置更改为1.6
    • 我在元素中声明以很好地控制可传递依赖项中的版本。slf4j-apidependencyManagement

完整的pom.xml看起来像这样(所以任何人都可以复制):

<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>

当我使用嵌入式玻璃鱼插件运行项目时:

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

并在浏览器中访问 http://localhost:8080/server,我按预期在标准输出中获取日志:

...
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. ***
********************************************************************

我想知道这是否具有代表性。


我已经检查了战争,log4j.properties确实在WEB-INF/类中。我没有log4j.jar,我有slf4j-log4j12.jar。

slf4j-log4j12.jar不是 log4j.jar, slf4j-log4j12 的替代品.jar是 log4J 版本 1.2 的绑定,您仍然需要 log4j.jar。来自 SLF4J 文档:

在部署时与日志记录框架绑定

如前所述,SLF4J支持各种日志记录框架。SLF4J 发行版附带了几个 jar 文件,称为“SLF4J 绑定”,每个绑定对应于一个受支持的框架。

slf4j-log4j12-1.6.1.jar:log4j 版本 1.2 的绑定,一个广泛使用的日志记录框架。您还需要将 log4j.jar放在类路径上。

我想知道你是如何在 NetBeans 和 Eclipse 下做到这一点的。


答案 2

我有完全相同的问题。

Log4j本身工作得很好:

<dependency>
   <groupId>log4j</groupId>
   <artifactId>log4j</artifactId>
   <version>1.2.16</version>
</dependency>

但是,如果我尝试使用它的Slf4j,那么我的“src/resources/log4j.properties”文件就不再找到了,即使Maven将其添加到类路径上的目录中也是如此。

因此,这不是开箱即用的:

<dependency>
   <groupId>log4j</groupId>
   <artifactId>log4j</artifactId>
   <version>1.2.16</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.6.4</version>
</dependency>
<dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>jcl-over-slf4j</artifactId>
   <version>1.6.4</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jul-to-slf4j</artifactId>
    <version>1.6.4</version>
</dependency>
<dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-api</artifactId>
   <version>1.6.4</version>
</dependency>

要使其工作,您必须显式地将“log4j.properties”添加到类路径中,或者告诉服务器在哪里可以找到它!实现此目的的一种方法是(此示例在 Windows 上):

-Dlog4j.configuration=file:C:\[pathToYourProject]\trunk\target\classes\log4j.properties

在 Eclipse 中(如果这是您使用的),您可以将同一行添加到运行配置/ VM 参数中。


推荐