log4j.properties 被忽略

发布于 2024-09-13 12:52:56 字数 4255 浏览 10 评论 0原文

我在 glassfish v3.0.1 上使用 IntelliJ IDEA 9.0.3 使用 Maven 部署了 Java EE 6 Wicket 应用程序。我使用 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 jar 位于我的 war 的 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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

意中人 2024-09-20 12:52:56

更新:我尝试重现该问题。我创建了一个简单的 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
    • 我在 dependencyManagement 元素中声明了 slf4j-api,以很好地控制传递依赖项中的版本。

完整的 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>

当我使用嵌入的 glassfish 插件运行项目时:

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

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


我查了一下war,log4j.properties确实在WEB-INF/classes中。我没有 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 下实现此功能的。

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.

三寸金莲 2024-09-20 12:52:56

我遇到了完全相同的问题。

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 参数中。

I had the exact same problem.

Log4j alone works great:

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

But if I try to use Slf4j over it, then my "src/resources/log4j.properties" file is not found anymore, even if Maven add it to a directory that is on the classpath.

So, this doesn't work out of the box:

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

To make it work, you have to explicitly add the "log4j.properties" to the classpath or tell the server where to find it! A way to achieve this is (this example is on Windows):

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

In Eclipse (if this is what you use), you can add the same line to your Run Configuration / VM arguments.

梦初启 2024-09-20 12:52:56

我建议删除所有 slf4j 依赖项并更改您的日志记录代码以直接使用 Log4j API(如果这不是太多工作,不知道您的项目的大小)。一旦它起作用,请考虑您是否真的需要 slf4j 提供的灵活性。如果这样做,请选择正确版本的 slf4j(slf4j-log4j12 可能不是用于 log4j 1.2.16 的正确版本)并将其重新集成。

我最近遇到了 slf4j,最后完全删除了它,因为我运行了陷入类似的配置问题。

I suggest removing all the slf4j dependencies and change your logging code to use the Log4j API directly (if this is not too much work, don't know the size of your project). Once this works, consider if you really need the flexibility offered by slf4j. If you do, pick up the correct version of slf4j (slf4j-log4j12 might not be the correct one to use for log4j 1.2.16) and integrate it back in.

I encountered slf4j recently, and in the end removed it altogether because I ran into similar configuration problems.

你如我软肋 2024-09-20 12:52:56

我也有同样的问题。解决方案很简单 - 所有日志依赖项都应该位于类路径上的 glassfish 之前。

请注意,较旧的 Maven2 版本在类路径一致性方面存在一些问题。我使用的 2.2.1 已修复此问题(我认为它已在 2.0.9 中修复)。

I had the same problems. The solution is simple - all logging dependencies should be before the glassfish on the classpath.

Note that older Maven2 versions have some problems with the classpath consistency. I use 2.2.1 which have this issue fixed (it was fixed in 2.0.9, I think).

情绪失控 2024-09-20 12:52:56

查看 log4j 手册。 “默认初始化过程”部分描述了 log4j 如何尝试查找初始化文件。也许您可以尝试其中一些选项来使事情正常进行。

Have a look at the log4j manual. The section "Default Initialization Procedure" describes how log4j will try to find the initialization file. Maybe you can try some of this options to get things work.

烟雨凡馨 2024-09-20 12:52:56

最可能想到的两件事是:

  1. 确保 log4j.properties 文件位于部署的 WEB-INF/classes 中。我假设你的意思是它在你的代码库中的 WEB-INF/classes 中,但是你是否确认这是发送到 glassfish 的战争中的情况,
  2. 确保你的 log4j.jar 位于你部署的 WEB-INF/lib 中,

因为它正在工作在某些部署场景中,我怀疑通过 Maven 运行时的 war 打包是问题所在。上述几点应该可以帮助您确认这一点。

The two most likely things that spring to mind are:

  1. make sure the log4j.properties file is in WEB-INF/classes of the deployment. I assume you mean it is in your WEB-INF/classes in your codebase, but have you confirmed this is the case in the war that sent to glassfish
  2. make sure your log4j.jar is in your deployed WEB-INF/lib

Since it's working in some deployment scenarios, I suspect your war packaging when running via Maven is the problem. The above points should help you confirm this.

不忘初心 2024-09-20 12:52:56

我有以下日志依赖项:

<dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>0.9.17</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.5.8</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>1.5.8</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.14</version>
        <scope>runtime</scope>
    </dependency>

在 /src/main/resources/ 下,我有一个 logback.xml 定义各个方面(附加程序,..)。这被 maven 拾取并复制到 WEB-INF/classes

希望有帮助

I have the following logging dependencies :

<dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>0.9.17</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.5.8</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>1.5.8</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.14</version>
        <scope>runtime</scope>
    </dependency>

under /src/main/resources/ i have a logback.xml defining the various aspects (appenders,..). This gets picked up by maven and copied to WEB-INF/classes

hope that helped

谁对谁错谁最难过 2024-09-20 12:52:56

我支持阿德里安·科斯特的回答。但是,如果您的纯 log4j 不起作用,请尝试以下操作。创建像这样的简单类

import org.apache.log4j.Logger;
public class LogTest {
    private static Logger log;

    public static void main(String[] args) {
        log = Logger.getLogger(LogTest.class);
    }
}

...并在 org.apache.log4j.helpers.Loader#getResource 方法中放置断点。它尝试从类加载器中提取 log4j.xml:

url = classLoader.getResource(resource);      

因此您可以轻松地查看类加载器内部并查看它使用的路径(以及为什么它找不到您的 log4j.xml)。

I would support Adriaan Koster's answer. If your pure log4j doesn't work however, try the following. Create simple class like this

import org.apache.log4j.Logger;
public class LogTest {
    private static Logger log;

    public static void main(String[] args) {
        log = Logger.getLogger(LogTest.class);
    }
}

...and put breakpoint in org.apache.log4j.helpers.Loader#getResource method. It tries to pull log4j.xml from classloader:

url = classLoader.getResource(resource);      

So you can easily look inside the classloader and see what paths it uses (and why it can't find your log4j.xml).

自在安然 2024-09-20 12:52:56

同样非常重要的是,log4j 库是在项目中使用的库的所有模块之后编译的。如果您不将其设置为最后一个对象,则后续模块/库的日志将不会以 log4j.properties 的标准方式显示。

It is also very important that log4j library was compiled after all modules of the libraries used in the project. If you will not set it as last object, the logs from later modules/libraries wouldn't be displayed in a standard way for log4j.properties.

南薇 2024-09-20 12:52:56

对于使用 spring boot 的人:

它似乎忽略 -Dlog4j.configuration=file:..。您可以改用 -Dlogging.config=file:/etc/myconfig/log4j.xml。必须是xml格式。

您还可以在 java 源目录中编辑文件 logback-spring.xml,例如 src/main/resources/。这是默认日志记录配置的标准位置。

更多信息的良好来源:https://www.baeldung.com/spring-boot-logging

For people using spring boot:

It appears to ignore -Dlog4j.configuration=file:... You can instead use -Dlogging.config=file:/etc/myconfig/log4j.xml. It must be xml format.

You can also edit the file logback-spring.xml in your java source dir eg src/main/resources/. This is the standard place for the default logging config.

A good source of further information: https://www.baeldung.com/spring-boot-logging

春风十里 2024-09-20 12:52:56

对我有用的方法:将 log4j2.properties (或 log4j2.xml)添加到我的类路径根目录(tomcat/webapps/ROOT/WEB-INF/classes/)。参数值记录现已启用。

背景:我注意到我的构建中有 log4j 1、2 和 slf4j jar。我们在许多 java 文件中都有 slf4j 导入。正在使用 hibernate.properties 并且 hibernate.show_sql=true 可以工作,但我无法启用参数值日志记录。当 mvn 打包的 war 在 tomcat 中运行时,类路径根目录中的 log4j.properties 被忽略。但是当我从 eclipse 运行它时,log4j.properties 工作了,我可以启用参数值日志记录。

使用的 log4j2.properties:

appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n

rootLogger.level = info
rootLogger.appenderRef.stdout.ref = STDOUT

logger.hibernate.name=org.hibernate.SQL
logger.hibernate.level=debug
logger.hibernate-type.name=org.hibernate.type
logger.hibernate-type.level=trace

基于:https: //www.codejava.net/frameworks/hibernate/how-to-configure-hibernate-logging-with-log4j2

搜索 log4j/slf4j: find /usr/local/tomcat/webapps/ROOT -名称 '*slf4j*' -o -name '*log4j*'

What worked for me: Adding log4j2.properties (or log4j2.xml) to my class path root (tomcat/webapps/ROOT/WEB-INF/classes/). Parameter-value logging is now enabled.

Background: I noticed log4j 1, 2 and slf4j jars in my build. We have slf4j imports in many java files. hibernate.properties was being used and hibernate.show_sql=true worked but I could not enable parameter-value logging. My log4j.properties in the classpath root was being ignored - when the mvn packaged war was run in tomcat. But when I ran it from eclipse, log4j.properties worked and I could enable parameter-value logging.

log4j2.properties used:

appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n

rootLogger.level = info
rootLogger.appenderRef.stdout.ref = STDOUT

logger.hibernate.name=org.hibernate.SQL
logger.hibernate.level=debug
logger.hibernate-type.name=org.hibernate.type
logger.hibernate-type.level=trace

Based on: https://www.codejava.net/frameworks/hibernate/how-to-configure-hibernate-logging-with-log4j2

Search for log4j/slf4j: find /usr/local/tomcat/webapps/ROOT -name '*slf4j*' -o -name '*log4j*'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文