【问题标题】:log4j2.xml not being picked up by STSSTS 未获取 log4j2.xml
【发布时间】:2014-04-15 23:33:13
【问题描述】:

我一直在尝试通过 log4j2.xml 在 Spring 项目中使用基本配置进行日志记录。试了两天没有成功。该项目似乎忽略了我的 log4j2.xml 文件并选择了默认配置。

这就是我一直在做的:

  1. 已创建 log4j2.xml 并将其添加到类路径中。下面给出的配置。
  2. 确保此 xml 文件是部署程序集的一部分,希望它被拾取
  3. 解压缩生成的 war 文件以查看该文件是否作为部署的一部分添加...是的。

没有任何帮助。我在 StackOverflow 上查找了几篇关于此类问题的帖子,但其中大部分都提到我们所要做的就是将文件添加到类路径中。但就我而言,当我运行应用程序时,只有默认配置才是我认为处于活动状态的配置。准确地说,虽然下面的配置显示“信息”,但声明 log.info("Inside foo()"); 不会打印消息。当我介入时,我看到 Info 级别的日志未启用。

任何帮助将不胜感激。

===================

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="INFO">
    <appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="[%d{ISO8601}] [%t] %-5p %c{6} - %msg%n"/>
        </Console>
        <File name="File" fileName="example.log">
            <PatternLayout pattern="[%d{ISO8601}] [%t] %-5p %c{6} - %msg%n"/>
        </File>
    </appenders>
    <loggers>
        <logger name="com.mypackage" level="info" additivity="false">
            <appender-ref ref="Console"/>
            <appender-ref ref="File"/>
        </logger>
        <root level="info">
            <appender-ref ref="Console"/>
        </root>
    </loggers>
</configuration>

我的 gradle 构建脚本中的依赖项:

def tomcatVersion = '7.0.42'
tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
"org.apache.tomcat.embed:tomcat-embed-logging-juli:${tomcatVersion}"
tomcat("org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}") {
    exclude group: 'org.eclipse.jdt.core.compiler', module: 'ecj'
}

compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
compile group: 'com.datastax.cassandra', name: 'cassandra-driver-core', version:'1.0.3'
//compile group: 'com.yammer.metrics', name: 'metrics-core', version:'2.2.0'
compile 'org.springframework:spring-core:3.2.3.RELEASE'
compile 'org.springframework:spring-webmvc:3.2.3.RELEASE'
compile 'org.springframework.security:spring-security-web:3.2.0.RELEASE'
compile 'org.springframework.security:spring-security-core:3.2.0.RELEASE'
compile 'org.springframework.security:spring-security-config:3.2.0.RELEASE'
compile 'org.springframework.hateoas:spring-hateoas:0.7.0.RELEASE'

providedCompile 'javax.servlet:javax.servlet-api:3.0.1'

runtime 'com.fasterxml.jackson.core:jackson-core:2.2.2'
runtime 'com.fasterxml.jackson.core:jackson-databind:2.2.2'
runtime 'javax.xml.bind:jaxb-api:2.2.9'

compile 'org.apache.logging.log4j:log4j-jcl:2.0-rc1'
runtime 'org.apache.logging.log4j:log4j-jcl:2.0-rc1'

// 编译'org.slf4j:slf4j-api:1.7.5' // 运行时 'org.slf4j:slf4j-jdk14:1.7.5'

testCompile group: 'junit', name: 'junit', version: '4.+'
testCompile 'org.springframework:spring-test:3.2.3.RELEASE'
testCompile "org.mockito:mockito-all:1.9.5"

【问题讨论】:

  • 哪个应用服务器?你只使用 log4j 还是 commons 日志框架?我最近用 log4j 和 wildly 提交了类似的错误。
  • 狮子座,感谢您的回复。这是我正在尝试使用 Spring 4.0 框架设置的 Web 服务器(Tomcat 7.0)。我是服务器端的新手......不确定当你说哪个应用程序服务器时这是否是你的意思。我使用 log4j。没有公共日志记录框架。我将使用我的项目所具有的依赖项更新帖子。
  • 我现在注意到的一件事是:“org.apache.tomcat.embed:tomcat-embed-logging-juli:${tomcatVersion}”,在我的 build.gradle 依赖项中。会不会有冲突?

标签: java xml logging configuration log4j2


【解决方案1】:

您需要在类路径中有 log4j2 api jar 和 log4j2 核心 jar,以及 log4j2.xml 文件。如果在类路径中找不到核心 jar 或 log4j2.xml,log4j2 将自动配置为仅将错误级别消息打印到控制台的默认配置。

尝试从您的应用程序中打印出来,看看类路径中是否有必要的 jar:

//api
System.out.println(org.apache.logging.log4j.Logger.class.getResource("/org/ap‌​ache/logging/log4j/Logger.class"));
//core
System.out.println(org.apache.logging.log4j.Logger.class.getResource("/org/ap‌​ache/logging/log4j/core/Appender.class"));
//config
System.out.println(org.apache.logging.log4j.Logger.class.getResource("/log4j2.xml"));

【讨论】:

  • 太棒了!就是这样。我删除了 log4j-jcl 并添加了核心、api。它就像一个魅力。您建议添加的打印语句现在打印正确的输出(与之前将最后两个打印为 null 不同)。它还启用了信息级别的日志......这是我根据我的 log4j2.xml 所期望的。非常感谢,Remko。
  • 我的 log4j 正在搜索“log4j2.xml”而不是“/log4j2.xml”。我有后者的匹配,但不是相对版本。有什么想法吗?
  • 在此之前我必须排除 spring-boot-starter-logging。 configurations { compile.exclude module: 'spring-boot-starter-logging' }
猜你喜欢
  • 2016-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-03
  • 1970-01-01
相关资源
最近更新 更多