java - log4j.properties 文件中的 log4j.rootLogger 属性有何意义?如果我不使用该属性会怎样?

标签 java logging log4j apache-commons-logging

log4j.properties 文件中的 log4j.rootLogger 属性有何意义?如果我不使用此属性会发生什么?

示例:

# 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

如果我将此属性设置为 ERROR 模式,会发生什么情况。

最佳答案

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

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

如果您有以下配置:

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

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

Tree

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 Logger objects 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: ResourceBundles are key-value pattern properties files used for the localization of logging messages. A child logger inherits any ResourceBundle associated with its parent logger.
<小时/>

注释

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

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

关于java - log4j.properties 文件中的 log4j.rootLogger 属性有何意义?如果我不使用该属性会怎样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34333025/

相关文章:

java.util.scanner - 在由关闭流初始化的 Scanner 上调用 next() 不会抛出 IllegalStateException

java - 获取 Logger 的惯用 Kotlin 方式是什么?

java - 使用 HtmlUnit WebClient.getPage 时出现多个错误

java - 正确使用InetSocketAddress

multithreading - CodeSite 类别和线程

python - 自定义回溯输出

java - 使用标记的logstash-logback-encoder错误

java - Java中log4j基于级别的日志记录

java - 将 log4j JMSAppender 与 ActiveMQ 一起使用 - 调试级别中的有线格式超时(客户端)

java - Android 3.1及以上版本自动开启定位服务