找不到 log4j2 配置文件。使用默认配置:仅将错误记录到控制台

No log4j2 configuration file found. Using default configuration: logging only errors to the console

$ java -Dlog4j.configuration=file:///path/to/your/log4j2.xml -jar /path/to/your/jar_file.jar

写入控制台,得到

ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.

但是,配置文件似乎已找到但无法解析:

    log4j:WARN Continuable parsing error 2 and column 31
    log4j:WARN Document root element "Configuration", must match DOCTYPE root "null".
    log4j:WARN Continuable parsing error 2 and column 31
    log4j:WARN Document is invalid: no grammar found.
    log4j:ERROR DOM element is - not a <log4j:configuration> element.
    log4j:WARN No appenders could be found for logger (kafka.utils.VerifiableProperties).
    log4j:WARN Please initialize the log4j system properly.
    log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

问题 1

ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.

解决方案 1

使用 log4j 版本 2 又名 "log4j2"

-Dlog4j.configuration=

应该阅读

-Dlog4j.configurationFile=

问题 2

log4j:WARN ....

解决方案 2

在您的项目中,取消包含 log4j-1.2 jar,而是包含 log4j-1.2-api-2.1.jar。我不确定如何排除 log4j 1.2。我知道我的项目的哪些依赖项需要它。所以,通过一些阅读,我排除了一堆东西。

<dependency>
    <groupId>org.apache.kafka</groupId>
    <artifactId>kafka_2.10</artifactId>
    <version>0.8.2.0</version>
    <exclusions>
        <exclusion>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>                
        </exclusion>
        <exclusion>
            <groupId>org.apache.log4j</groupId>
            <artifactId>log4j-core</artifactId>
        </exclusion>
        <exclusion>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
        </exclusion>          
    </exclusions>
</dependency>

我不确定是哪个排除项起作用了。另外,我包含了对 1.2 api 的依赖,它连接到 2.x.

<!--
    http://logging.apache.org/log4j/2.0/manual/migration.html
    http://logging.apache.org/log4j/2.0/maven-artifacts.html
    Log4j 1.x API Bridge
    If existing components use Log4j 1.x and you want to have this logging
    routed to Log4j 2, then remove any log4j 1.x dependencies and add the
    following.
-->
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-1.2-api</artifactId>
    <version>2.2</version>
</dependency>

现在,仅进入控制台的 1.2 日志实际上流向了我们的 2.x 附加程序。

如果你没有 log4j-1.2.jar 作为 的类路径,你将只会看到消息 no log4j2 configuration file found .

如果您的配置文件中有错误,这就是一个问题,因为您不会在启动时被告知问题出在哪里。

例如,如果您有一个 log4j2 无法处理的 log4j2.yaml 文件,例如,, or your config is simply incorrect. You will encounter the no log4j2 configuration file found message, with no further information. This is the case even if you have a valid log4j2.xml file, as log4j2 will only attempt to process the first configuration file it finds.

我发现调试问题的最佳方法是根据提到的命令行参数明确说明您希望使用的配置文件

-Dlog4j.configurationFile=

希望这将帮助您查明问题是否实际上是由您的类加载器未找到 log4j2 配置文件或您的配置中的其他内容引起的。

更新

您还可以使用以下 属性 更改状态记录器的默认级别以获取更多信息:

-Dorg.apache.logging.log4j.simplelog.StatusLogger.level=<level>

我已经处理这个问题一段时间了。我已按照此 post 中的描述更改了所有内容,甚至认为发生了错误。在这种情况下,请确保在更改 .xml 或 .properties 文件中的设置时清理项目。 在日食环境中。选择项目 -> 清理

在我的例子中,我在我的 gradle 项目的类路径中使用 log4j2 Json 文件 log4j2.json,但我遇到了同样的错误。

这里的解决方案是将 JSON 处理的依赖项添加到我的 gradle 依赖项中。

compile group:"com.fasterxml.jackson.core", name:"jackson-core", version:'2.8.4'
compile group:"com.fasterxml.jackson.core", name:"jackson-databind", version:'2.8.4'
compile group:"com.fasterxml.jackson.core", name:"jackson-annotations", version:'2.8.4'

另见 documentation of log4j2:

The JSON support uses the Jackson Data Processor to parse the JSON files. These dependencies must be added to a project that wants to use JSON for configuration:

我检查以验证日志记录的内容,

1) 检查是否没有旧的 log4j 版本。

mvn dependency:tree | grep log ## ./gradlew dependencies | grep log
[INFO] +- com.prayagupd:log-service:jar:1.0:compile
[INFO] +- org.apache.logging.log4j:log4j-api:jar:2.6.2:compile
[INFO] +- org.apache.logging.log4j:log4j-core:jar:2.6.2:compile
[INFO] |  \- commons-logging:commons-logging:jar:1.1.1:compile

2) 确保我正确设置了 log4j2.json,这是通过 -Dlog4j.configurationFile=???

完成的
val logConfig: PropertiesConfiguration = new PropertiesConfiguration("application.properties")
System.setProperty("log4j.configurationFile", logConfig.getString("log4j.config.file"))

println("log4j.configurationFile :: " + System.getProperty("log4j.configurationFile"))

Configurator.initialize(null, logConfig.getString("log4j.config.file"));

此外,如果正在进行自动检测 确保文件名是 log4j2.* 而不是 log4j.*

3) 另一项检查是针对 log4j2.json 的解析器。感谢 log4j 团队没有在 API.

中提供解析器
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.4.0</version>
</dependency>

如果找不到 json 解析器

,这可能会引发异常
[Fatal Error] log4j2.json:1:1: Content is not allowed in prolog.

我已经通过配置构建路径解决了这个问题。以下是我遵循的步骤: 在日食中。

  1. 创建一个文件夹并在其中保存 logj4 文件。
  2. 在创建的文件夹中单击并转到构建路径。
  3. 点击添加文件夹
  4. 选择创建的文件夹。
  5. 单击确定并应用并关闭。

现在您应该可以 运行

我在 java maven 项目中使用 hive jdbc 并且遇到了同样的问题。

我的方法是在src/main/java/resources

下添加一个log4j2.xml文件
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MyClassName {
    private static final Logger LOG = LoggerFactory.getLogger(MyClassName.class);
}

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="<your_package_name>.<your_class_name>" level="debug">
        <AppenderRef ref="Console"/>
        </Logger>
        <Root level="WARN">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>

我收到此错误是因为 log4j2 文件已从我的 src 文件夹中删除

直接在src文件夹下添加xml文件

<Appenders>
    <Console name="Console" target="SYSTEM_OUT">
        <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n" />
    </Console>

    <RollingFile name="RollingFile" filename="logs/B2CATA-hybrid.log"
        filepattern="${logPath}/%d{yyyyMMddHHmmss}-fargo.log">
        <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n" />
        <Policies>
            <SizeBasedTriggeringPolicy size="100 MB" />
        </Policies>
        <DefaultRolloverStrategy max="20" />
    </RollingFile>

</Appenders>
<Loggers>
   <Logger name="com.pragiti." level="trace" />
    <Root level="info">
        <AppenderRef ref="Console" />
        <AppenderRef ref="RollingFile" />
    </Root>
</Loggers>

我遇到了同样的问题,但是我在网上阅读了有关此问题的信息后发现我的项目中没有 log4j2.xml,因此我将相关代码复制到记事本中并将记事本文件还原为 xml 并添加到文件夹资源下的我的项目中。它对我有用。

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<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>

确保您在 .../src/main/resources 文件夹下放置了 log4j2.* 文件而不是 log4j.* 文件。

这有时会在 web servlet 上找到实际的 log4j2 配置文件之前抛出。至少就我而言,我是这样认为的。因为我已经在 web.xml

  <context-param>
    <param-name>log4jConfiguration</param-name>
    <param-value>classpath:log4j2-app.xml</param-value>
  </context-param>

并检查 log4j-web 源; 在 class

org.apache.logging.log4j.web.Log4jWebInitializerImpl

有线;

String location = this.substitutor
                 .replace(this.servletContext.getInitParameter("log4jConfiguration"));

所有这些让我认为这是找到配置之前的临时日志。

我正在开发 TestNG Maven 项目,通过在 pom.xml 中添加 <resources> 标签对我有用,这恰好是我的自定义配置文件 log4j2.xml 的路径。

<url>http://maven.apache.org</url>
<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<build>
  <resources>
    <resource>
      <directory>src/main/java/resources</directory>
      <filtering>true</filtering>
    </resource>
  </resources>
  <pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.0.0-M3</version>
        <configuration>
          <suiteXmlFiles>
            <suiteXmlFile>testng.xml</suiteXmlFile>
          </suiteXmlFiles>
        </configuration>
      </plugin>
    </plugins>
  </pluginManagement>
</build>
<dependencies>
  <dependency>

将 xml 文件名重命名为 log4j2.xml

后问题已解决
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<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>
  1. 创建一个新的文本文档并复制粘贴上面的代码并保存为log4j2.xml。

  2. 现在复制此 log4j2.xml 文件并将其粘贴到 Java 项目的 src 文件夹下。

  3. 运行 再次执行您的 java 程序,您会看到错误消失了。

只需尝试以下步骤,它就会起作用,

  1. 将 XML 文件直接保存在下面的文件夹结构下。 src/main/java
  2. 确保保存 pom.xml 文件以防发生任何更改..

测试:log4j-ap 2.13.2、log4j-core 2.13.2。

  1. 将 XML 文件直接保存在下面的文件夹结构下。 src/main/java
  2. 在 POM 中:
    <build>   
 <resources>       
             <resource>
                <filtering>false</filtering>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>             
            </resource>
 </resources>  
</build>
  1. 清理并构建。