简体   繁体   English

找不到记录器(log4j)的附加程序?

[英]No appenders could be found for logger(log4j)?

I have put log4j to my buildpath, but I get the following message when I run my application:我已将 log4j 放入我的构建路径,但在运行我的应用程序时收到以下消息:

log4j:WARN No appenders could be found for logger (dao.hsqlmanager).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

What do these warnings mean?这些警告是什么意思? Whats the appender here?这里的appender是什么?

This Short introduction to log4j guide is a little bit old but still valid.这个log4j指南的简短介绍有点旧,但仍然有效。

That guide will give you some information about how to use loggers and appenders.该指南将为您提供有关如何使用记录器和附加器的一些信息。


Just to get you going you have two simple approaches you can take.为了让您继续前进,您可以采取两种简单的方法。

First one is to just add this line to your main method:第一个是将此行添加到您的主要方法中:

BasicConfigurator.configure();

Second approach is to add this standard log4j.properties (taken from the above mentioned guide) file to your classpath:第二种方法是将此标准log4j.properties (取自上述指南)文件添加到您的类路径中:

# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1

# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender

# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

It looks like you need to add the location of your log4j.properties file to the Classpath in Eclipse.看起来您需要将log4j.properties文件的位置添加到 Eclipse 中的类路径。

Make sure your project is open in Eclipse, then click on the "Run" menu at the top of Eclipse and click on the following:确保您的项目在 Eclipse 中打开,然后单击 Eclipse 顶部的“运行”菜单并单击以下内容:

  1. Run
  2. Run Configurations运行配置
  3. Classpath (tab)类路径(选项卡)
  4. User Entries用户条目
  5. Advanced (button on the right)高级(右侧按钮)
  6. Add Folders添加文件夹
  7. then navigate to the folder that contains your log4j.properties file然后导航到包含 log4j.properties 文件的文件夹
  8. Apply申请
  9. Run

The error message should no longer appear.错误消息不应再出现。

Quick solution:快速解决方案:

  1. add code to main function :将代码添加到主函数

     String log4jConfPath = "/path/to/log4j.properties"; PropertyConfigurator.configure(log4jConfPath);
  2. create a file named log4j.properties at /path/to/path/to创建一个名为 log4j.properties 的文件

    log4j.rootLogger=INFO, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{2}: %m%n

This is just a warning.这只是一个警告。

Fixing定影

This occurs when the default configuration files log4j.properties and log4j.xml can not be found and the application performs no explicit configuration.当无法找到默认配置文件log4j.propertieslog4j.xml并且应用程序未执行显式配置时,就会发生这种情况。

To fix that, simply create/copy log4j.properties or log4j.xml into your a location on the classpath (usually the same as the jar files).要解决这个问题,只需将log4j.propertieslog4j.xml创建/复制到类路径上的位置(通常与 jar 文件相同)。

Optionally set java option: -Dlog4j.configuration=file:///path/to/log4j.properties .可选择设置 java 选项: -Dlog4j.configuration=file:///path/to/log4j.properties

log4j uses Thread.getContextClassLoader().getResource() to locate the default configuration files and does not directly check the file system. log4j使用Thread.getContextClassLoader().getResource()定位默认配置文件,不直接检查文件系统。 Knowing the appropriate location to place log4j.properties or log4j.xml requires understanding the search strategy of the class loader in use.知道放置log4j.propertieslog4j.xml的适当位置需要了解正在使用的类加载器的搜索策略。 log4j does not provide a default configuration since output to the console or to the file system may be prohibited in some environments. log4j不提供默认配置,因为在某些环境中可能禁止输出到控制台或文件系统。

Debugging调试

For debugging, you may try to use -Dlog4j.debug=true parameter.对于调试,您可以尝试使用-Dlog4j.debug=true参数。

Configuration of log4j.properties log4j.properties配置

Sample configuration of log4j.properties : log4j.properties示例配置:

# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1

# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender

# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

# Print only messages of level WARN or above in the package com.foo.
log4j.logger.com.foo=WARN

Here is another configuration file that uses multiple appenders:这是另一个使用多个 appender 的配置文件:

log4j.rootLogger=debug, stdout, R

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log

log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

Apache Solr阿帕奇Solr

If using Solr , copy <solr>/example/resources/log4j.properties into a location on the classpath .如果使用Solr ,请将<solr>/example/resources/log4j.properties复制到classpath上的某个位置。

Sample configuration of log4j.properties from Solr goes like: Solr 中log4j.properties示例配置如下:

#  Logging level
solr.log=logs/
log4j.rootLogger=INFO, file, CONSOLE

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender

log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%-4r [%t] %-5p %c %x \u2013 %m%n

#- size rotation with log cleanup.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.MaxFileSize=4MB
log4j.appender.file.MaxBackupIndex=9

#- File to log to and log format
log4j.appender.file.File=${solr.log}/solr.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%-5p - %d{yyyy-MM-dd HH:mm:ss.SSS}; %C; %m\n

log4j.logger.org.apache.zookeeper=WARN
log4j.logger.org.apache.hadoop=WARN

# set to INFO to enable infostream log messages
log4j.logger.org.apache.solr.update.LoggingInfoStream=OFF

See also:也可以看看:

Most of the answers here suggested that log4j.properties file be placed in the right location(for maven project, it should be located in src/main/resources )这里的大多数答案都建议将log4j.properties文件放在正确的位置(对于 maven 项目,它应该位于src/main/resources

But for me, the problem is that my log4j.properties is not correctly configured.但对我来说,问题是我的log4j.properties没有正确配置。 Here's a sample that works for me, you can try it out first.这是一个对我有用的示例,您可以先尝试一下。

# Root logger option
log4j.rootLogger=INFO, stdout

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

As explained earlier there are 2 approaches如前所述,有两种方法

First one is to just add this line to your main method:第一个是将此行添加到您的主要方法中:

BasicConfigurator.configure();

Second approach is to add this standard log4j.properties file to your classpath:第二种方法是将此标准log4j.properties文件添加到您的类路径:

While taking second approach you need to make sure you initialize the file properly, Eg.在采用第二种方法时,您需要确保正确初始化文件,例如。

Properties props = new Properties();
props.load(new FileInputStream("log4j property file path"));
props.setProperty("log4j.appender.File.File", "Folder where you want to store log files/" + "File Name");

Make sure you create required folder to store log files.确保创建所需的文件夹来存储日志文件。

You use the Logger in your code to log a message.您可以在代码中使用Logger来记录消息。 The Appender is a Object appended to a Logger to write the message to a specific target. Appender是附加到Logger的对象,用于将消息写入特定目标。 There are FileAppender to write to text-files or the ConsoleAppender to write to the Console.FileAppender写入文本文件或ConsoleAppender写入控制台。 You need to show your code of the Logger and Appender setup for more help.您需要显示 Logger 和 Appender 设置的代码以获得更多帮助。

please read the tutorial for a better understanding of the interaction of Logger and Appender.请阅读教程以更好地理解 Logger 和 Appender 的交互。

Make sure the properties file has properly set.确保属性文件已正确设置。 And again, it seems like that the compiler cannot find the properties file, you can set as follow at the pom (only when you use maven project).再次,编译器似乎找不到属性文件,您可以在pom中设置如下(仅当您使用maven项目时)。

<build>
       <sourceDirectory> src/main/java</sourceDirectory>
       <testSourceDirectory> src/test/java</testSourceDirectory>
        <resources>
             <resource>
                  <directory>resources</directory>
             </resource>
        </resources>           
</build >

I get the same error.我犯了同样的错误。 Here the problem which leads to this error message:这是导致此错误消息的问题:

I create some objects which use the Logger before I configure the log4j:在配置 log4j 之前,我创建了一些使用 Logger 的对象:

Logger.getLogger(Lang.class.getName()).debug("Loading language: " + filename);

Solution: Configure the log4j at the beginning in the main method:解决方法:在main方法的开头配置log4j:

PropertyConfigurator.configure(xmlLog4JConfigFile); 
// or BasicConfigurator.configure(); if you dont have a config file

I faced the same issue when I tried to run the JUnit test class.当我尝试运行 JUnit 测试类时,我遇到了同样的问题。

The issue is resolved after I manually added the log4j.properties file in the src/test/resources folder.在 src/test/resources 文件夹中手动添加 log4j.properties 文件后,问题得到解决。

Adding the below code to the log4j.properties file solved the issue:将以下代码添加到 log4j.properties 文件解决了该问题:

# Root logger option
log4j.rootLogger=INFO, file, stdout

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\logging.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

Add the following as the first code:添加以下作为第一个代码:

Properties prop = new Properties();
prop.setProperty("log4j.rootLogger", "WARN");
PropertyConfigurator.configure(prop);

I think you should understand where the log4j jar file or the Java code looks for the log4j configuration files.我认为您应该了解 log4j jar 文件或 Java 代码在哪里查找 log4j 配置文件。

src/main/resources/log4j.properties is Eclipse path. src/main/resources/log4j.properties是 Eclipse 路径。 Place them in a proper location so that you don't have to hard code the absolute path in code.将它们放在适当的位置,这样您就不必在代码中硬编码绝对路径。

Read my article and sample solution for that https://askyourquestions.info/how-to-see-where-the-log-is-logger-in-slf4j/阅读我的文章和示例解决方案https://askyourquestions.info/how-to-see-where-the-log-is-logger-in-slf4j/

Another reason this may happen (in RCP4) is that you are using multiple logging frameworks in your target file.这可能发生的另一个原因(在 RCP4 中)是您在目标文件中使用了多个日志框架。 As an example, this will occur if you use a combination of slf4j, log4j and ch.qos.logback.slf4j in your target files content tab.例如,如果您在目标文件内容选项卡中使用 slf4j、log4j 和 ch.qos.logback.slf4j 的组合,就会发生这种情况。

In my case, the error was the flag " additivity ".就我而言,错误是标志“可加性”。 If it's "false" for you root project package, then the child packages will not have an appender and you will see the " appender not found " error.如果根项目包为“false”,则子包将没有附加程序,您将看到“找不到附加程序”错误。

如果您正在使用 Eclipse 并且在一切正常后突然出现此问题,请尝试转到Project - Clean - Clean

I ran into this issue when trying to build an executable jar with maven in intellij 12. It turned out that because the java manifest file didn't include a class path the log4j properties file couldn't be found at the root level (where the jar file was executed from.)我在 Intellij 12 中尝试使用 maven 构建可执行 jar 时遇到了这个问题。结果是因为 java 清单文件不包含类路径,所以在根级别找不到 log4j 属性文件(其中jar 文件是从中执行的。)

FYI I was getting the logger like this:仅供参考,我得到了这样的记录器:

Logger log = LogManager.getLogger(MyClassIWantedToLogFrom.class);

And I was able to get it to work with a pom file that included this:我能够让它与包含以下内容的 pom 文件一起使用:

         <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.2-beta-5</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath> 
                        <mainClass>com.mycompany.mainPackage.mainClass</mainClass>
                    </manifest>
                    <manifestEntries>
                        <Class-Path>.</Class-Path> <!-- need to add current directory to classpath properties files can be found -->
                    </manifestEntries>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

Make sure your project is open in Eclipse, then click on the "Run" menu at the top of Eclipse and click on the following:确保您的项目在 Eclipse 中打开,然后单击 Eclipse 顶部的“运行”菜单并单击以下内容:

  1. Run

  2. Run Configurations运行配置

  3. Classpath (tab)类路径(选项卡)

  4. User Entries用户条目

  5. add jar on the right在右边添加罐子

  6. add log4j jar file添加 log4j jar 文件

  7. Apply申请

  8. Run

The error message should no longer appear.错误消息不应再出现。

The reason can be lack of the word static in some:原因可能是在某些情况下缺少static一词:

final static Logger logging = Logger.getLogger(ProcessorTest.class);

If I make logger the instance field, I am getting exactly this very warning:如果我将 logger 作为实例字段,我会收到非常警告:

No appenders could be found for logger (org.apache.kafka.producer.Sender)

What is worse, the warning points not to ProcessorTest , where the mistake lives, but to an absolutely different class (Sender) as a source of problems.更糟糕的是,警告不是指向错误所在的ProcessorTest ,而是指向一个完全不同的类(Sender)作为问题的根源。 That class has correct set logger and need not any changes!这个类有正确的一组记录仪,不用任何变化! We could look for the problem for ages!我们可以寻找多年的问题!

I faced the same problem when I use log4j2.我在使用 log4j2 时遇到了同样的问题。 My problem is caused by using wrong dependent library:我的问题是由于使用了错误的依赖库引起的:

<dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <scope>runtime</scope>
    </dependency>

Instead, I should use:相反,我应该使用:

<dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <scope>runtime</scope>
    </dependency>

In my case, I have a log4j2.xml defined in my "resources" directory, and specified to use it by:就我而言,我在“资源”目录中定义了一个 log4j2.xml,并指定通过以下方式使用它:

System.setProperty("log4j.configurationFile", "log4j2.xml");

Log4J display this warning message when Log4j Java code is searching to create a first log line in your program.当 Log4j Java 代码正在搜索以在您的程序中创建第一个日志行时,Log4J 会显示此警告消息。

At this moment, Log4j make 2 things此时,Log4j 做了两件事

  1. it search to find log4j.properties file它搜索以找到log4j.properties文件
  2. it search to instantiate the appender define in log4j.properties它搜索以实例化log4j.properties定义的 appender

If log4J doesn't find log4j.properties file or if appender declared in log4j.rootlogger are not defined elsewhere in log4j.properties file the warning message is displayed.如果log4J找不到log4j.properties文件,或者如果log4j.rootlogger中声明的 appender 未在log4j.properties文件中的其他地方定义,则会显示警告消息。

CAUTION: the content of Properties file must be correct.注意:Properties 文件的内容必须正确。

The following content is NOT correct以下内容不正确

log4j.rootLogger=file

log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=c:/Trace/MsgStackLogging.log
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%m%n
log4j.appender.FILE.ImmediateFlush=true
log4j.appender.FILE.Threshold=debug
log4j.appender.FILE.Append=false

because file appender is declared in LOWER-CASE in log4j.rootlogger statement and defined in log4j.appender statement using UPPER-CASE !因为file appender 在log4j.rootlogger语句中以 LOWER-CASE 声明,并在 log4j.appender 语句中使用 UPPER-CASE 定义!

A correct file would be正确的文件是

log4j.rootLogger=FILE

log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=c:/Trace/MsgStackLogging.log
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%m%n
log4j.appender.FILE.ImmediateFlush=true
log4j.appender.FILE.Threshold=debug
log4j.appender.FILE.Append=false

If MAVEN is used, you must put log4j.properties files in src/main/resources AND start a MAVEN build.如果使用 MAVEN,则必须将 log4j.properties 文件放在src/main/resources并启动 MAVEN 构建。

Log4j.properties file is then copied in target/classes folder.然后将 Log4j.properties 文件复制到target/classes文件夹中。

Log4J use the log4j.properties file that it found in target/classes ! Log4J 使用它在target/classes找到的log4j.properties文件!

I had this problem too.我也有这个问题。 I just forgot to mark the resources directory in IntelliJ IDEA我只是忘记在 IntelliJ IDEA 中标记资源目录

  1. Rightclick on your directory右键单击您的目录
  2. Mark directory as将目录标记为
  3. Resources root资源根

First of all: Create a log4j.properties file首先:创建一个 log4j.properties 文件

# Root logger option
log4j.rootLogger=INFO, stdout

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

Place it in src/main/resources/把它放在 src/main/resources/

After that, use this 2 dependencies:之后,使用这两个依赖项:

<dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.5</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.5</version>
        </dependency>

It is necessary to add this final dependency to POM file:有必要将这个最终依赖添加到 POM 文件中:

<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.5.RELEASE</version>
        </dependency>

My Eclipse installation could not find log4j.properties when running JUnit tests from Eclipse, even though the file was located at src/test/resources .从 Eclipse 运行JUnit测试时,我的 Eclipse 安装找不到log4j.properties ,即使该文件位于src/test/resources

The reason was that Eclipse (or the m2e connector) did not copy content from src/test/resources to the expected output folder target/test-classes - the root cause was that in the project's properties under Java Build Path -> Source tab -> Source folders on build path -> src/test/resources , somehow there was an Excluded: ** entry.原因是 Eclipse(或m2e连接器)没有将内容从src/test/resources复制到预期的输出文件夹target/test-classes - 根本原因是在Java Build Path -> Source选项卡下的项目属性中 - >构建路径上的源文件夹-> src/test/resources ,不知何故有一个Excluded: **条目。 I removed that excluded entry.我删除了那个排除的条目。

Alternatively, I could have manually copied src/test/resources/log4j.properties to target/test-classes/log4j.properties .或者,我可以手动将src/test/resources/log4j.properties复制到target/test-classes/log4j.properties

If log4j.properties is indeed on the classpath, you are using Spring Boot to make a WAR file for deployment to an app server, you are omitting a web.xml file in favour of Spring Boot's autoconfigure, and you are not getting any log messages whatsoever, you need to explicitly configure Log4j.如果log4j.properties确实在类路径上,则您正在使用 Spring Boot 制作 WAR 文件以部署到应用服务器,您省略了web.xml文件以支持 Spring Boot 的自动配置,并且您没有收到任何日志消息无论如何,您需要显式配置 Log4j。 Assuming you are using Log4j 1.2.x:假设您使用的是 Log4j 1.2.x:

public class AppConfig extends SpringBootServletInitializer {

    public static void main( String[] args ) {
        // Launch the application
        ConfigurableApplicationContext context = SpringApplication.run( AppConfig.class, args );
    }

    @Override
    protected SpringApplicationBuilder configure( SpringApplicationBuilder application ) {
        InputStream log4j = this.getClass().getClassLoader().getResourceAsStream("log4j.properties");
        PropertyConfigurator.configure(log4j);
        return application;
    }

// Other beans as required...
}

也许在java构建路径中添加相关项目包含log4j,当我在使用eclipse的mahout项目中遇到这个问题时,我将mahout_h2o添加到其中,它有效!

if you work together with a lot of projects you may face a style problem.如果你和很多项目一起工作,你可能会面临风格问题。

*you have to have one lof4j.properties file and this file is included log properties of other project. *您必须有一个 lof4j.properties 文件,并且该文件包含其他项目的日志属性。

*Beside you can try to put log4j properties files into src path when the project is worked Linux OS, libs of other project and log4.properties files can be under one folder into a location on the classpath. *另外你可以尝试在项目运行Linux操作系统时将log4j属性文件放入src路径,其他项目的libs和log4.properties文件可以在一个文件夹下放入类路径上的某个位置。

First import:第一次导入:

 import org.apache.log4j.PropertyConfigurator;

Then add below code to main method:然后将以下代码添加到main方法中:

String log4jConfPath ="path to/log4j.properties";
PropertyConfigurator.configure(log4jConfPath);

Create a file at path to and add the below code to that file.路径上创建一个文件并将以下代码添加到该文件中。

log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{2}: %m%n

The solution on this site worked for me https://crunchify.com/java-how-to-configure-log4j-logger-property-correctly/ .这个网站上的解决方案对我有用 https://crunchify.com/java-how-to-configure-log4j-logger-property-correctly/ I now see no warnings at all from log4j我现在根本看不到 log4j 的警告

I put this in a log4j.properties file that I put in src/main/resources我把它放在我放在 src/main/resources 中的 log4j.properties 文件中

# This sets the global logging level and specifies the appenders
log4j.rootLogger=INFO, theConsoleAppender

# settings for the console appender
log4j.appender.theConsoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.theConsoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.theConsoleAppender.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

Consider the log4j JVM argument Dlog4j.configuration考虑 log4j JVM 参数Dlog4j.configuration

In general:一般来说:

Add the JVM argument that points out the log4j config file.添加指出 log4j 配置文件的 JVM 参数。 The syntax is just like follows:语法如下:

java [ options ] -jar file.jar [ arguments ]

A sample of a real command line is just like the following:一个真实的命令行示例如下所示:

java -Dlog4j.configuration=conf/log4j.xml -jar myJarFile.jar myArg1 myArg2

For IntelliJ IDE users:对于 IntelliJ IDE 用户:

1.Run/Debug Configurations
2.Edit configurations...
3.VM options
4.Enter the same value also starting with "-D"

Tips:提示:

1.Eclipse IDE users will find an equivalent approach 1.Eclipse IDE 用户会找到一个等效的方法

2.For the run/debug configuration editor, it's very likely that, at the beginning of the times, your particular executable file is not there. 2.对于运行/调试配置编辑器,很可能在开始时,您的特定可执行文件不存在。 Depending on the size of the project you're currently working on, it can be unpleasant to navigate directories to find it.根据您当前正在处理的项目的大小,导航目录以找到它可能会令人不快。 It's less of a hassle if you just run/execute the file (click play) once before proceeding to run/debug config no matter what the execution result is.如果您在继续运行/调试配置之前运行/执行一次文件(单击播放),那么无论执行结果如何,都不会那么麻烦。

3.Pay attention to your working directory, relative paths, and classpath. 3.注意你的工作目录、相对路径和类路径。

For me, the reason was apparently different, and the error message misleading.对我来说,原因显然不同,错误信息具有误导性。

With just this in my build.gradle, it would complain that slf4j was missing at the beginning of the log, but still log things, albeit in a poor format:在我的 build.gradle 中只有这个,它会抱怨日志开头缺少 slf4j,但仍然记录内容,尽管格式很差:

    compile 'log4j:log4j:1.2.17'

Adding that dependency would cause the discussed "no appenders could be found" error message for me, even though I had defined them in src/main/java/log4j.properties :添加该依赖项会导致我讨论的“找不到附加程序”错误消息,即使我已经在src/main/java/log4j.properties定义了它们:

    compile 'log4j:log4j:1.2.17'
    compile 'org.slf4j:slf4j-log4j12:1.7.25'

Finally, adding the following dependency (which I only guessed at by copycatting it from another project) resolved the issue:最后,添加以下依赖项(我只是通过从另一个项目复制它来猜测的)解决了该问题:

    compile 'log4j:log4j:1.2.17'
    compile 'org.slf4j:slf4j-log4j12:1.7.25'
    compile 'commons-logging:commons-logging:1.2'

I don't know why, but with this it works.我不知道为什么,但是有了这个就行了。 Any insights on that?对此有何见解?

在java eclipse中,将conf_ref复制到conf文件夹。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM