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.
可以指定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的日志级别高,日志请求才会被处理。引用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.
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的说明:
-
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.