我正在尝试设置 SLF4J 以与 Log4J2 一起使用。但我不断收到此错误:
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
这是我的 POM 依赖项:
<properties>
<log4j.version>2.1</log4j.version>
</properties>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j.version}</version>
</dependency>
我使用一个简单的配置进行测试:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
我的 log4j2.xml 文件位于 src/main/resources 中。
我不知道还要检查什么。我也尝试过将 xml 文件移动到/src 目录,但没有帮助。我错过了什么?
最佳答案
您没有指定用于部署的服务器,当我尝试在 Wildfly 8.1 上部署 Web 应用程序并将 log4j2.xml 放在 src/main/resources 文件夹中时,我遇到了同样的问题。
this thread中指出这不是 log4j2 bug,而是 JBoss/Wildfly 代码中的 bug。
在线程中引用 Hassan Kalaldeh:
I found what is the problem, if you extract log4j-web-2.1.jar and open servlet container Initializer service in file : log4j-web-2.1\META-INF\services\javax.servlet.ServletContainerInitializer you will find this: javax.servlet.ServletContainerInitializer
# # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You under the Apache license, Version 2.0 # (the "License"); you may not use this file except in compliance with # the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the license for the specific language governing permissions and # limitations under the license. # org.apache.logging.log4j.web.Log4jServletContainerInitializer
I've been reading JBoss deployer source code and I found it always read first row of service file // this line from ServletContainerInitializerDeploymentProcessor.loadSci() String servletContainerInitializerClassName = reader.readLine();
so when I manually edit the service file and just keep the last line (org.apache.logging.log4j.web.Log4jServletContainerInitializer) it works fine now
无论如何,我确实将 log4j2 版本恢复到 2.0.2 并且它也能正常工作。
关于maven - Log4j2 找不到我的配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26552518/