我一直在尝试通过log4j2.xml在Spring项目中使用基本配置进行日志记录。试了两天没有运气。该项目似乎忽略了我的log4j2.xml文件并选择了默认配置。
这就是我一直在做的事情:
没有帮助。我在StackOverflow上找到了几个关于这些问题的帖子,但大多数提到我们所要做的就是将文件添加到classpath中。但在我的情况下,当我运行应用程序时,只有默认配置是我认为是活动的。确切地说,虽然下面的配置说" info",但是log.info("Inside foo()");
语句不会打印消息。当我介入时,我看到未启用信息级别日志。
非常感谢任何帮助。
==================
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>
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'
// compile&#39; org.slf4j:slf4j-api:1.7.5&#39; //运行时&#39; org.slf4j:slf4j-jdk14:1.7.5&#39;
testCompile group: 'junit', name: 'junit', version: '4.+'
testCompile 'org.springframework:spring-test:3.2.3.RELEASE'
testCompile "org.mockito:mockito-all:1.9.5"
答案 0 :(得分:3)
您需要在类路径中包含log4j2 api jar和log4j2核心jar,以及log4j2.xml文件。如果在类路径上找不到核心jar或log4j2.xml,则log4j2将自动配置为仅将ERROR级别消息打印到控制台的默认配置。
尝试从您的应用程序打印此内容,以查看必需的jar是否在类路径中:
//api
System.out.println(org.apache.logging.log4j.Logger.class.getResource("/org/apache/logging/log4j/Logger.class"));
//core
System.out.println(org.apache.logging.log4j.Logger.class.getResource("/org/apache/logging/log4j/core/Appender.class"));
//config
System.out.println(org.apache.logging.log4j.Logger.class.getResource("/log4j2.xml"));