简体   繁体   English

找不到 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

Written to the console, you get写到控制台,你得到

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

But, it also looks like the configuration file has been found and was not parsable:但是,看起来配置文件已经找到并且无法解析:

    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.

Problem 1问题一

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

Solution 1解决方案1

To work with version 2 of log4j aka "log4j2"使用 log4j 又名“log4j2”的第 2 版

-Dlog4j.configuration=

should read应该读

-Dlog4j.configurationFile=

Problem 2问题二

log4j:WARN ....

Solution 2解决方案2

In your project, uninclude the log4j-1.2 jar and instead, include the log4j-1.2-api-2.1.jar.在您的项目中,取消包含 log4j-1.2 jar,而是包含 log4j-1.2-api-2.1.jar。 I wasn't sure how exactly to exclude the log4j 1.2.我不确定如何准确排除 log4j 1.2。 I knew that what dependency of my project was requiring it.我知道我的项目的什么依赖需要它。 So, with some reading, I excluded a bunch of stuff.所以,通过一些阅读,我排除了一堆东西。

<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>

I am not sure which of the exclusions did the trick.我不确定哪些排除项起作用了。 Separately, I included a dependency to the 1.2 api which bridges to 2.x.另外,我包含了一个对 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>

Now, the 1.2 logs which were only going to the console actually flow to our 2.x appenders.现在,仅进入控制台的 1.2 日志实际上流向了我们的 2.x 附加程序。

If you don't have the fortune of the log4j-1.2.jar on your classpath as Renko points out in his comment , you will only see the message no log4j2 configuration file found .如果 Renko在他的评论中指出的那样,您的类路径上没有 log4j-1.2.jar 的财富,您将只会看到消息no log4j2 configuration file found

This is a problem if there is an error in your configuration file, as you will not be told where the problem lies upon start-up.如果您的配置文件中有错误,这是一个问题,因为您不会在启动时被告知问题出在哪里。

For instance if you have a log4j2.yaml file which log4j2 fails to process, because for example, you have not configured a YAML parser for your project , or your config is simply incorrect.例如,如果您有一个 log4j2 无法处理的 log4j2.yaml 文件,因为例如您没有为您的项目配置 YAML 解析器,或者您的配置根本不正确。 You will encounter the no log4j2 configuration file found message, with no further information.您将遇到no log4j2 configuration file found消息,没有更多信息。 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 .即使您有一个有效的 log4j2.xml 文件也是如此,因为 log4j2 只会尝试处理它找到的第一个配置文件

I've found the best way to debug the problem is to explicitly state the configuration file you wish to use as per the command line argument mentioned above .我发现调试问题的最佳方法是根据上面提到的命令行参数明确说明您希望使用的配置文件。

-Dlog4j.configurationFile=

Hopefully this will help you pinpoint if the issue is actually caused by your classloader not finding the log4j2 configuration file or something else in your configuration.希望这将帮助您查明问题是否实际上是由您的类加载器未找到 log4j2 配置文件或您的配置中的其他内容引起的。

Update更新

You can also use the below property to change the default level of the status logger to get further information:您还可以使用以下属性更改状态记录器的默认级别以获取更多信息:

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

i had same problem, but i noticed that i have no log4j2.xml in my project after reading on the net about this problem, so i copied the related code in a notepad and reverted the notepad file to xml and add to my project under the folder resources.我有同样的问题,但我在网上阅读了这个问题后发现我的项目中没有 log4j2.xml,所以我将相关代码复制到记事本中并将记事本文件恢复为 xml 并添加到我的项目下文件夹资源。 it works for me.这个对我有用。

<?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>

I have been dealing with this problem for a while.我一直在处理这个问题。 I have changed everything as described in this post and even thought error occured.我已经按照这篇文章中的描述更改了所有内容,甚至认为发生了错误。 In that case make sure that you clean the project when changing settings in .xml or .properties file.在这种情况下,请确保在更改 .xml 或 .properties 文件中的设置时清理项目。 In eclipse environment.在eclipse环境中。 Choose Project -> Clean选择项目 -> 清理

I use hive jdbc in a java maven project and have the same issues.我在 java maven 项目中使用 hive jdbc 并且有同样的问题。

My method is to add a log4j2.xml file under src/main/java/resources我的方法是在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 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>

Stuffs I check to verify logging,我检查以验证日志记录的东西,

1) Check that there're no older log4j versions. 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) make sure I'm setting log4j2.json properly, which is done with -Dlog4j.configurationFile=??? 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"))

or或者

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

Also if auto detection is happening make sure the file name is log4j2.* not log4j.*此外,如果发生自动检测,请确保文件名是 log4j2.* 而不是 log4j.*

3) Another check is for parser for log4j2.json. 3) 另一项检查是针对 log4j2.json 的解析器。 Thanks log4j team for not providing parser within the API.感谢 log4j 团队没有在 API 中提供解析器。

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.4.0</version>
</dependency>

This might throw an exception if can not find the json parser如果找不到 json 解析器,这可能会引发异常

[Fatal Error] log4j2.json:1:1: Content is not allowed in prolog.

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

In my case I am using the log4j2 Json file log4j2.json in the classpath of my gradle project and I got the same error.就我而言,我在 gradle 项目的类路径中使用 log4j2 Json 文件log4j2.json ,但遇到了同样的错误。

The solution here was to add dependency for JSON handling to my gradle dependencies.这里的解决方案是将 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'

See also documentation of log4j2 :另请参阅log4j2 的文档

The JSON support uses the Jackson Data Processor to parse the JSON files. JSON 支持使用 Jackson 数据处理器来解析 JSON 文件。 These dependencies must be added to a project that wants to use JSON for configuration:必须将这些依赖项添加到要使用 JSON 进行配置的项目中:

I am getting this error because log4j2 file got deleted from my src folder我收到此错误是因为 log4j2 文件已从我的 src 文件夹中删除

Simply add xml file under src folder只需在 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>

I am working on TestNG Maven project, This worked for me by adding <resources> tag in pom.xml , this happens to be the path of my custom configuration file log4j2.xml .我正在研究 TestNG Maven 项目,这对我log4j2.xml ,通过在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后问题解决

Tested with: log4j-ap 2.13.2, log4j-core 2.13.2.测试:log4j-ap 2.13.2,log4j-core 2.13.2。

  1. Keep XML file directly under below folder structure.将 XML 文件直接保存在下面的文件夹结构下。 src/main/java源代码/主/Java
  2. In the POM:在 POM 中:
 <build> <resources> <resource> <filtering>false</filtering> <directory>src/main/resources</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build>
  1. Clean and build.清洁和建造。

I have solve this problem by configuring the build path.我通过配置构建路径解决了这个问题。 Here are the steps that I followed: In eclipse.以下是我遵循的步骤: 在 eclipse 中。

  1. create a folder and save the logj4 file in it.创建一个文件夹并将 logj4 文件保存在其中。
  2. Write click in the folder created and go to Build path.在创建的文件夹中写入单击并转到构建路径。
  3. Click on Add folder点击添加文件夹
  4. Choose the folder created.选择创建的文件夹。
  5. Click ok and Apply and close.单击确定并应用并关闭。

Now you should be able to run现在你应该可以运行了

This sometimes can be thrown before the actual log4j2 configuration file found on the web servlet.这有时会在 web servlet 上找到的实际 log4j2 配置文件之前抛出。 at least for my case I think so.至少就我而言,我是这么认为的。 Cuz I already have in my web.xml因为我的 web.xml 中已经有了

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

and checking the log4j-web source;并检查 log4j-web 源; in class在班上

org.apache.logging.log4j.web.Log4jWebInitializerImpl

there is the line;有这条线;

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

all those makes me think that this is temporary log before configuration found.所有这些让我认为这是找到配置之前的临时日志。

<?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. Create a new Text document and copy-paste the above code and save it as log4j2.xml.创建一个新的 Text 文档并复制粘贴上面的代码并将其保存为 log4j2.xml。

  2. Now copy this log4j2.xml file and paste it under your src folder of your Java project.现在复制这个 log4j2.xml 文件并将其粘贴到 Java 项目的 src 文件夹下。

  3. Run your java program again, you will see error is gone.再次运行你的java程序,你会看到错误消失了。

Just try below steps, it will work,只需尝试以下步骤,它就会起作用,

  1. Keep XML file directly under below folder structure.将 XML 文件直接保存在下面的文件夹结构下。 src/main/java
  2. Make sure to save the pom.xml file in case any changes made..确保保存pom.xml文件以防发生任何更改。

Answer for Intellij users 2022 Intellij 用户的答案 2022

If you are using Intellij:如果您使用 Intellij:

Please follow these steps:请按照以下步骤操作:

  1. Put "resources" folder under "YourProject" root and mark it as "Resources Root".将“资源”文件夹放在“YourProject”根目录下,并将其标记为“资源根目录”。

  2. Put "log4j2.xml" in resources folder将“log4j2.xml”放入资源文件夹

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误StatusLogger找不到log4j2配置文件。 使用默认配置:仅将错误记录到控制台 - ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console log4j2 - StatusLogger未找到Log4j 2配置文件。 使用默认配置 - log4j2 - StatusLogger No Log4j 2 configuration file found. Using default configuration log4j2配置记录到文件但不记录到控制台 - log4j2 configuration logging to file but not to console 错误StatusLogger找不到log4j2配置文件。 使用ANT目标进行编译时使用默认配置 - ERROR StatusLogger No log4j2 configuration file found. Using default configuration while compiling using ANT target Log4j2 配置 - 找不到 log4j2 配置文件 - Log4j2 configuration - No log4j2 configuration file found 使用java 9找不到log4j2配置文件 - log4j2 configuration file not found with java 9 找不到log4j2配置文件 - log4j2 configuration file not found 从jar内使用默认的log4j2配置文件 - Using a default log4j2 configuration file from inside the jar 错误 StatusLogger 未找到 log4j2 配置文件。 在更新 Struts 2.5.12 版本时 - ERROR StatusLogger No log4j2 configuration file found. While updating the version 2.5.12 of Struts log4J2到slf4j到log4j1产生“未找到log4j2配置文件” - log4J2 to slf4j to log4j1 produces “No log4j2 configuration file found”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM