Log4j组件:Logger、Appender、Layout
Log4j核心包含三个组件:logger、appender和layout。这三个组件协作提供日志功能:
- 日志的输出目标
- 日志的输出格式
- 日志的输出级别(是否抑制日志的输出)
logger继承特性
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。For example, the logger named "com.foo" is a parent of the logger named "com.foo.Bar". Similarly, "java" is a parent of "java.util" and an ancestor of "java.util.Vector". This naming scheme should be familiar to most developers
Logger日志级别
- TRACE
- DEBUG
- INFO
- WARN
- ERROR
- FATAL
1. 上面的六个日志级别,从上到下级别逐次提高。A logging request(即调用logger的日志输出方法debug,info,war,error等) is said to be enabled if its level is higher than or equal to the level of its logger. Otherwise, the request is said to be disabled. A logger without an assigned level will inherit one from the hierarchy.
2. 假如c是logger对象,并且c的日志级别设置为INFO,那么只有c.info..c.fatal的日志调用才会真正的将日志打印到相应的目标上,c.debug则会抑制日志的输出
2. 一个logger最多指定一个级别
日志级别继承特性
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.
如果root logger的日志级别进行了设置,那么所有的logger,都会有不为空的日志级别。实际项目开发中,应该为root logger指定一个默认的日志级别。那面的两个表格是两个日志级别继承的例子:
。
root | Proot | Proot |
X | Px | Px |
X.Y | none | Px |
X.Y.Z | Pxyz | Pxyz |
root | Proot | Proot |
X | Px | Px |
X.Y | none | Px |
X.Y.Z | Pxyz | Pxyz |
Appender、Layout
在Log4j中,日志输出目标称之为Appender。一个logger可以打印到多个目标,也就是说一个logger可以配置多个appender。Log4j内置了如下几种Appender:
- DailyRollingFileAppender 按日期滚动文件
- RollingFileAppender 按文件大小滚动文件
- ConsoleAppender 打印到控制台
- JDBCAppender 将日志打印到数据库中
- JMSAppender 将日志打印到JMS中
- SocketAppender 将日志打印到log服务器(LogEvent对象序列化到日志服务器)
- ...
每个appender因为目标介质的不同以及方式的不同,每个类都有特定的一些属性(用于配置)
Appender Additivity(logger绑定多个appender的策略)
1. Each enabled logging request for a given logger will be forwarded to all the appenders in that logger as well as the appenders higher(higher here means the hierarchy relationship of logger) in the hierarchy. In other words, appenders are inherited additively from the logger hierarchy. For example, if a console appender is added to the root logger, then all enabled logging requests will at least print on the console. If a file appender is also added to a logger, say C, then enabled logging requests for C and C's children will print on a file and on the console. It is possible to override this default behavior so that appender accumulation is no longer additive by setting the additivity to false.
2. Loggers have their additivity flag set to true by default.
Appender Additivity中断(logger绑定多个appender的策略)
The output of a log statement of logger C will go to all the appenders in C and to all appenders that are defined in 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 its ancestors upto and including P but not the appenders in any of the ancestors of P.