log4j.rootLogger is at the top of all the logger hierarchy, just like we have Object in java classes. So whenever you are logging and there is no match with a logger configuration, root logger configuration will be used.
log4j.rootLogger位于所有记录器层次结构的顶部,就像Java类中有Object一样。 因此,无论何时登录,都与记录器配置不匹配,都将使用根记录器配置。
Let’s understand this with a simple example. Suppose we have a log4j.properties file like below.
让我们用一个简单的例子来理解这一点。 假设我们有一个log4j.properties文件,如下所示。
#Define root logger options
log4j.rootLogger=DEBUG, console
#Define console appender
log4j.appender.console=org.apache.log4j.ConsoleAppender
logrj.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-5p %c{1} - (%F:%L) %m%n
#Define loggers
log4j.logger.com.journaldev.log4j.main=ERROR, console
#setting additivity
log4j.additivity.com.journaldev.log4j.main=false
And here is a simple java program in package com.journaldev.log4j.main
corresponding to logger defined above.
这是包com.journaldev.log4j.main
与上面定义的记录器相对应的简单Java程序。
package com.journaldev.log4j.main;
import org.apache.log4j.Logger;
public class Log4jExample {
private final static Logger logger = Logger.getLogger(Log4jExample.class);
public static void main(String[] args) {
logger.trace("My Trace Log");
logger.debug("My Debug Log");
logger.info("My Info Log");
logger.warn("My Warn Log");
logger.error("My error log");
logger.fatal("My fatal log");
}
}
Below image shows the output when we run it.
下图显示了运行时的输出。
Now below is the output when I change the package declaration to com.journaldev.log4j.main1
, so that it doesn’t match with any of the defined logger.
现在,下面是将包声明更改为com.journaldev.log4j.main1
时的输出,以使其与任何已定义的记录器都不匹配。
Here the root logger configuration is being used, i.e logging level as DEBUG and a single appender “console”.
这里使用的是根记录器配置,即记录级别为DEBUG和单个附加程序“控制台”。
So is it required to define log4j.rootLogger always?
那么是否需要始终定义log4j.rootLogger?
No, log4j doesn’t give you any warning or any exception if you won’t define it. Unless a Logger instance is trying to use it.
不 ,如果您不定义log4j,它不会给您任何警告或任何异常。 除非Logger实例正在尝试使用它。
So if I comment the root logger configuration above and my java program is in com.journaldev.log4j.main
package, then a logger is defined for it and everything works fine.
因此,如果我注释了上面的根记录器配置,并且我的java程序位于com.journaldev.log4j.main
包中,则com.journaldev.log4j.main
定义了一个记录器,并且一切正常。
But if I am running my java program from com.journaldev.log4j.main1
package for which no logger is defined, it tries to look for root logger configuration and don’t find it. So it prints below warning message to console.
但是,如果我从com.journaldev.log4j.main1
程序包运行我的Java程序( com.journaldev.log4j.main1
记录器),它将尝试查找根记录器配置,但找不到它。 因此它将在警告消息下方打印到控制台。
log4j:WARN No appenders could be found for logger (com.journaldev.log4j.main1.Log4jExample).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See https://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Finally, how do we define the same thing in log4j.xml file?
最后,我们如何在log4j.xml文件中定义相同的内容?
Below is the XML based configuration with same thing as above log4j.properties file.
以下是基于XML的配置,与上述log4j.properties文件相同。
<root>
<priority value="DEBUG" />
<appender-ref ref="console" />
</root>
That’s all for log4j root logger configuration significance and usage.
这就是log4j根记录器配置的意义和用法。
翻译自: https://www.journaldev.com/10739/log4j-rootlogger-significance-log4j-properties-file