当前位置:  开发笔记 > 编程语言 > 正文

log4j.properties被忽略

如何解决《log4j.properties被忽略》经验,为你挑选了1个好方法。

我在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 jars在我的战争的WEB-INF/lib目录中.是否有某种配置让我无法完成这项工作?

谢谢.

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

这是我的pom.xml中的相关部分:

    

    
        com.google.guava
        guava
        r05
    

    

    
        junit
        junit
        4.7
        test
    
    
        org.glassfish.extras
        glassfish-embedded-all
        3.0
        test
    

    

    
        org.glassfish
        bean-validator
        3.0-JBoss-4.0.0.Beta3
        provided
    
    
        javax
        javaee-api
        6.0
        provided
    

    

    
        org.apache.wicket
        wicket
        1.4.9
    
    
        org.apache.wicket
        wicket-auth-roles
        1.4.9
    
    
        org.jboss.weld
        weld-wicket
        1.0.1-Final
    

    

    
        org.hibernate
        hibernate-core
        3.5.1-Final
    
    
        org.hibernate
        hibernate-annotations
        3.5.1-Final
    
    
        org.hibernate
        hibernate-commons-annotations
        3.2.0.Final
    
    
        org.hibernate
        hibernate-entitymanager
        3.5.1-Final
    
    
        org.hibernate
        hibernate-c3p0
        3.5.1-Final
    


    

    
        postgresql
        postgresql
        8.4-701.jdbc4
    


    

    
        org.slf4j
        slf4j-log4j12
        1.5.6
    
    
        log4j
        log4j
        1.2.16
    


Pascal Thive.. 6

更新:我试图重现这个问题.我创建了一个简单的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看起来像这样(所以任何人都可以重现):


  4.0.0
  com.mycompany
  my-wicketapp
  war
  1.0-SNAPSHOT
  
  quickstart
  
  
  
    
      The Apache Software License, Version 2.0
      http://www.apache.org/licenses/LICENSE-2.0.txt
      repo
    
  
  
    
    
      repository.jboss.org-public
      JBoss repository
      https://repository.jboss.org/nexus/content/groups/public
    
    
    
      java.net2
      Repository hosting the jee6 artifacts
      http://download.java.net/maven/2
    
  
  
    
    
      glassfish
      GlassFish Maven 2 Repository
      http://download.java.net/maven/glassfish
    
  
  
    
      
        org.slf4j
        slf4j-api
        ${slf4j.version}
      
    
  
  
    
      org.hibernate
      hibernate-entitymanager
      3.5.5-Final
    
    
      com.google.guava
      guava
      r05
    
    
      org.glassfish
      javax.servlet
      3.0.1
      provided
    
    
      org.glassfish
      bean-validator
      3.0-JBoss-4.0.0.Beta3
      provided
    
    
      javax
      javaee-api
      6.0
      provided
    
    
    
      org.apache.wicket
      wicket
      ${wicket.version}
    
    
      org.apache.wicket
      wicket-auth-roles
      ${wicket.version}
    
    
      org.jboss.weld
      weld-wicket
      1.0.1-Final
    
    
    
    
      org.slf4j
      slf4j-log4j12
      ${slf4j.version}
    
    
      log4j
      log4j
      1.2.16
    
    
    
      junit
      junit
      4.7
      test
    
    
    
      org.glassfish.extras
      glassfish-embedded-all
      3.0.1
      test
    
    
    
      org.mortbay.jetty
      jetty
      ${jetty.version}
      provided
    
    
      org.mortbay.jetty
      jetty-util
      ${jetty.version}
      provided
    
    
      org.mortbay.jetty
      jetty-management
      ${jetty.version}
      provided
    
  
  
    
      
        false
        src/main/resources
      
      
        false
        src/main/java
        
          **
        
        
          **/*.java
        
      
    
    
      
        false
        src/test/java
        
          **
        
        
          **/*.java
        
      
    
    
      
        true
        org.apache.maven.plugins
        maven-compiler-plugin
        
          1.6
          1.6
          true
          true
        
      
      
        org.mortbay.jetty
        maven-jetty-plugin
      
      
        org.apache.maven.plugins
        maven-eclipse-plugin
        
          true
        
      
      
        org.glassfish
        maven-embedded-glassfish-plugin
        3.0.1
        
          server
          server
          ${project.build.directory}/${project.build.finalName}.war
          8080
          ${project.build.directory}/gfe-${maven.build.timestamp}
          
          true
          
        
      
    
  
  
    1.4.9
    6.1.4
    1.5.6
  

当我使用embedded-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. ***
********************************************************************

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


我查过了战争,log4j.properties确实在WEB-INF/classes中.我没有log4j.jar,我有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.



1> Pascal Thive..:

更新:我试图重现这个问题.我创建了一个简单的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看起来像这样(所以任何人都可以重现):


  4.0.0
  com.mycompany
  my-wicketapp
  war
  1.0-SNAPSHOT
  
  quickstart
  
  
  
    
      The Apache Software License, Version 2.0
      http://www.apache.org/licenses/LICENSE-2.0.txt
      repo
    
  
  
    
    
      repository.jboss.org-public
      JBoss repository
      https://repository.jboss.org/nexus/content/groups/public
    
    
    
      java.net2
      Repository hosting the jee6 artifacts
      http://download.java.net/maven/2
    
  
  
    
    
      glassfish
      GlassFish Maven 2 Repository
      http://download.java.net/maven/glassfish
    
  
  
    
      
        org.slf4j
        slf4j-api
        ${slf4j.version}
      
    
  
  
    
      org.hibernate
      hibernate-entitymanager
      3.5.5-Final
    
    
      com.google.guava
      guava
      r05
    
    
      org.glassfish
      javax.servlet
      3.0.1
      provided
    
    
      org.glassfish
      bean-validator
      3.0-JBoss-4.0.0.Beta3
      provided
    
    
      javax
      javaee-api
      6.0
      provided
    
    
    
      org.apache.wicket
      wicket
      ${wicket.version}
    
    
      org.apache.wicket
      wicket-auth-roles
      ${wicket.version}
    
    
      org.jboss.weld
      weld-wicket
      1.0.1-Final
    
    
    
    
      org.slf4j
      slf4j-log4j12
      ${slf4j.version}
    
    
      log4j
      log4j
      1.2.16
    
    
    
      junit
      junit
      4.7
      test
    
    
    
      org.glassfish.extras
      glassfish-embedded-all
      3.0.1
      test
    
    
    
      org.mortbay.jetty
      jetty
      ${jetty.version}
      provided
    
    
      org.mortbay.jetty
      jetty-util
      ${jetty.version}
      provided
    
    
      org.mortbay.jetty
      jetty-management
      ${jetty.version}
      provided
    
  
  
    
      
        false
        src/main/resources
      
      
        false
        src/main/java
        
          **
        
        
          **/*.java
        
      
    
    
      
        false
        src/test/java
        
          **
        
        
          **/*.java
        
      
    
    
      
        true
        org.apache.maven.plugins
        maven-compiler-plugin
        
          1.6
          1.6
          true
          true
        
      
      
        org.mortbay.jetty
        maven-jetty-plugin
      
      
        org.apache.maven.plugins
        maven-eclipse-plugin
        
          true
        
      
      
        org.glassfish
        maven-embedded-glassfish-plugin
        3.0.1
        
          server
          server
          ${project.build.directory}/${project.build.finalName}.war
          8080
          ${project.build.directory}/gfe-${maven.build.timestamp}
          
          true
          
        
      
    
  
  
    1.4.9
    6.1.4
    1.5.6
  

当我使用embedded-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. ***
********************************************************************

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


我查过了战争,log4j.properties确实在WEB-INF/classes中.我没有log4j.jar,我有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.

推荐阅读
谢谢巷议
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有