maven - Log4j2 找不到我的配置文件

标签 maven slf4j log4j2

我正在尝试设置 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 中。

enter image description here

enter image description here

我不知道还要检查什么。我也尝试过将 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/

相关文章:

java - 我正在创建一个 Maven 项目并且我是初学者..我收到了这个错误。可以帮助解决这是我的 pom.xml & web.xml

java - 如何使用 Maven 在 Java 单元测试中处理冲突的外部库版本

grails - 嵌套闭包无法访问私有(private)静态字段

java - 使用 logback-classic/slf4j 配置默认警告/错误日志

java - log4j2查找websphere环境变量

java - Maven - 与 Spark 和 HtmlUnit 的依赖冲突

java - Maven 使用错误的依赖版本

java - 执行 "sbt run"时,没有通过 scalalogging 显示任何信息消息

java - 仅当存在警告或更严重级别的日志事件时才记录所有级别

java - 如何在 log4j2 中生成标题行