Log4j 学习二

Apache Logging Services Project 日志组件
log4j - Java 语言
log4xx - C++ 语言
log4net - .NET 版本
log4php - PHP 语言
Chainsaw - 日志查看与分析工具

Logger hierarchy (Logger 分层结构)

logger 是有名称的,并且 logger 的名称大小写敏感,遵循分级名称规则。引用Log4j的说明:
Named Hierarchy
A logger is said to be an ancestor of another logger if its name followed by a dot is a prefix of the descendant logger name. A logger is said to be a parent of a child logger if there are no ancestors between itself and the descendant logger.
例如,名称为"com.foo"的logger为名称为"com.foo.Bar"的logger的父亲,同样,"java"为"java.util"的父亲、"java.util.Vector"的祖先。

可以指定logger的日志级别,可取得值为(从低到高):TRACE、DEBUG、INFO、WARN、ERROR、FATAL。
这些常量被定义在org.apache.log4j.Level类中。可以通过继承Level类来定义自己的日志级别,但不建议这样做。

日志级别可以被继承
如果没有为logger指定日志级别,则它将从已指定日志级别的最近的祖先那里继承日志级别。引用Log4j的说明:
Level Inheritance

The inherited level for a given logger C, is equal to the first non-null level in the logger hierarchy, starting at C and proceeding upwards in the hierarchy towards the root logger.

为确保每一个logger最终都可以有一个日志级别,经常为rootLogger指定一个日志级别。

只有日志请求的级别比logger的日志级别高,日志请求才会被处理。引用Log4j的说明:
Basic Selection Rule

A log request of level p in a logger with (either assigned or inherited, whichever is appropriate) level q, is enabled if p >= q.

This rule is at the heart of log4j. It assumes that levels are ordered. For the standard levels, we have DEBUG < INFO < WARN < ERROR < FATAL.

用相同的名称调用Logger.getLogger方法,永远都返回对同一个logger对象的引用。

logger的分层结构与它们创建和配置的先后次序无关,也就是说,"父亲"总能找到它的"子孙"。

Appender 日志输出目的地

Log4j中日志输出的目的地为appender,一个logger可以有多个日志输出目的地,即可以有多个appender。

可以通过Logger类的addAppender(org.apache.log4j.Appender appender)方法来为logger添加appender。

appender具有相加性,引用Log4j的说明:

Each enabled logging request for a given logger will be forwarded to all the appenders in that logger as well as the appenders higher in the hierarchy.
Appender Additivity

The output of a log statement of logger C will go to all the appenders in C and its ancestors. This is the meaning of the term "appender additivity".

However, if an ancestor of logger C, say P, has the additivity flag set to false, then C's output will be directed to all the appenders in C and it's ancestors upto and including P but not the appenders in any of the ancestors of P.

Loggers have their additivity flag set to true by default.


你可能感兴趣的:(apache,C++,c,log4j,C#)