Java log4j.properties 文件中 log4j.rootLogger 属性的意义是什么?如果我不使用这个属性会怎样?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23869207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 08:43:18  来源:igfitidea点击:

What is the significance of log4j.rootLogger property in log4j.properties file? What happens if i don't use this property?

javalogginglog4japache-commons-logging

提问by Jitendra

What is the significance of log4j.rootLoggerproperty in log4j.propertiesfile? What happens if I don't use this property?

档案中的log4j.rootLogger财产有什么意义log4j.properties?如果我不使用此属性会怎样?

Example:

示例

# 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

What happens if I set this property to ERRORmode.

如果我将此属性设置为ERRORmode会发生什么。

回答by Paul Vargas

Samudra Gupta explains in his book1:

Samudra Gupta 在他的书1 中解释说:

The Loggerobject is the main object that an application developer uses to log any message. The Loggerobjects acting within a particular instance of an application follow a parent-child hierarchy.

Logger对象是应用程序开发人员用来记录任何消息的主要对象。该Logger应用程序的特定实例中的作用对象遵循父子层次结构

If you have the following configuration:

如果您有以下配置:

log4j.rootLogger=WARN, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout
log4j.logger.com.me.proj2=INFO

This is how the logger hierarchy could end up looking:2

这就是记录器层次结构最终的样子:2

Tree

树

Samudra Gupta continues to explain:

Samudra Gupta 继续解释:

At the top of the hierarchy exists a root logger. The root logger exists outside the scope of any custom logger hierarchy that we may come up with. It always exists as the root logger for all possible logger hierarchies, and it has no namespace. All the other application-specific Loggerobjects are child objects to the root logger. The parent-child relationship of loggers signifies the dependency of the loggers acting within the same application. A child logger can inherit properties from its parent logger recursively up the tree. Typically, a child logger will inherit the following properties from its parent logger(s):

  • Level: If the child logger has no explicit tree level specified, it will use the level of its closest parent or the first proper level it finds recursively up the hierarchy.
  • Appender: If there is no appender attached to a logger, the child logger uses the appender of its closest parent logger or the first appender it finds recursively up the tree.
  • ResourceBundle: ResourceBundlesare key-value pattern properties files used for the localization of logging messages. A child logger inherits any ResourceBundleassociated with its parent logger.

在层次结构的顶部存在一个根记录器。根记录器存在于我们可能提出的任何自定义记录器层次结构的范围之外。它始终作为所有可能的记录器层次结构的根记录器存在,并且它没有命名空间。所有其他特定于应用程序的Logger对象都是根记录器的子对象。记录器的父子关系表示在同一应用程序中运行的记录器的依赖关系。子记录器可以在树上递归地从其父记录器继承属性。通常,子记录器将从其父记录器继承以下属性:

  • Level:如果子记录器没有指定明确的树级别,它将使用其最近父级的级别或它在层次结构中递归找到的第一个适当级别。
  • Appender:如果没有附加到记录器的附加器,子记录器使用其最近的父记录器的附加器或它在树上递归找到的第一个附加器。
  • ResourceBundle:ResourceBundles是用于日志消息本地化的键值模式属性文件。子记录器继承ResourceBundle与其父记录器关联的任何内容。


NOTES

笔记

1Samudra Gupta, Pro Apache Log4j, Second Edition(Berkeley, CA: Apress, 2005), 24-25, ISBN13: 978-1-59059-499-5

1Samudra Gupta,Pro Apache Log4j,第二版(加州伯克利:Apress,2005),24-25,ISBN13:978-1-59059-499-5

2Dominic Mitchell, Logging in Java, http://happygiraffe.net/blog/2008/09/03/logging-in-java/, Retrieved 26 May 2014.

2Dominic Mitchell,Java 登录http://happygiraffe.net/blog/2008/09/03/logging-in-java/,2014 年 5 月 26 日检索。

回答by abhishek jotshi

log4j.rootLoggerproperty sets the Level (DEBUG here) and Appender (A1 here) for root Logger. This is not mandatory. Root logger does not have a default appender attached and it can exist without an appender. So, your log4j properties file can be without this property being set.

log4j.rootLogger属性为根 Logger 设置级别(此处为 DEBUG)和 Appender(此处为 A1)。这不是强制性的。根记录器没有附加默认的 appender,它可以在没有 appender 的情况下存在。因此,您的 log4j 属性文件可以不设置此属性。

Root logger is the highest logger in the log4j hierarchy similar to Object class in Java.

根记录器是 log4j 层次结构中最高的记录器,类似于 Java 中的 Object 类。

回答by abhishek jotshi

To answer

回答

What happens if I don't use this property?

如果我不使用此属性会怎样?

If you don't set the rootLogger to a level and an appender, you will get a warning.

如果您没有将 rootLogger 设置为级别和附加程序,您将收到警告。

For example, if you omit or comment out the line log4j.rootLogger=DEBUG, stdout, i.e. say your log4j.properties file contains only the rootlogger and no additional loggers, here the root logger being commented out:

例如,如果您省略或注释掉该行log4j.rootLogger=DEBUG, stdout,即说您的 log4j.properties 文件仅包含 rootlogger 而没有其他记录器,则此处将被注释掉的根记录器:

#log4j.rootLogger=DEBUG, stdout

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

You will get something like the following output:

您将得到类似以下输出的内容:

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