log4j 介绍(2)--Logger 篇

本文详细介绍了Log4j中的Logger级别的继承机制,包括如何设置Logger的级别、未指定级别时如何继承父Logger的级别,以及如何判断日志请求是否启用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

紧紧接着log4j 介绍(1)--Logger篇
If a given logger is not assigned a level, then it inherits one from its closest ancestor with an assigned level. More formally:

[color=darkred][b]Level Inheritance [/b][/color]
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.

来句中文解释: Level是指: DEBUG < INFO < WARN < ERROR < FATAL.
如果logger C的level没有被制定,那么它会去寻找它最接近的上面的parent或者ancestor的Level,一直到root的Level为止。 例如:

// get a logger instance named "com.foo"
Logger logger = Logger.getLogger("com.foo");

// Now set its level. Normally you do not need to set the
// level of a logger programmatically. This is usually done
// in configuration files.
logger.setLevel(Level.INFO);

Logger barlogger = Logger.getLogger("com.foo.Bar");

//barlogger no setting level, so will inherite its level
//from logger named "com.foo". Hence barlogger has the INFO leval


A logging request 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. This rule is summarized below.

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.

Here is an example of this rule.

// get a logger instance named "com.foo"
Logger logger = Logger.getLogger("com.foo");

// Now set its level. Normally you do not need to set the
// level of a logger programmatically. This is usually done
// in configuration files.
logger.setLevel(Level.INFO);

Logger barlogger = Logger.getLogger("com.foo.Bar");

// This request is enabled, because WARN >= INFO.
logger.warn("Low fuel level.");

// This request is disabled, because DEBUG < INFO.
logger.debug("Starting search for nearest gas station.");

// The logger instance barlogger, named "com.foo.Bar",
// will inherit its level from the logger named
// "com.foo" Thus, the following request is enabled
// because INFO >= INFO.
barlogger.info("Located nearest gas station.");

// This request is disabled, because DEBUG < INFO.
barlogger.debug("Exiting gas station search");



Calling the getLogger method with the same name will always return a reference to the exact same logger object.
比如:
Logger x = Logger.getLogger("wombat");
Logger y = Logger.getLogger("wombat");

x 和 y都是指向同一个logger 对象的
这样的好处可以在任意的地方定义一些引用,来指向同一个logger对象,而不需要传递引用。灵活方便一点

Configuration of the log4j environment is typically done at application initialization. The preferred way is by reading a configuration file. This approach will be discussed shortly.
读取配置文件的顺序是: 先会去class目录下寻找log4j.xml,然后才是log4j.properties.

Log4j makes it easy to name loggers by software component. This can be accomplished by statically instantiating a logger in each class, with the logger name equal to the fully qualified name of the class. This is a useful and straightforward method of defining loggers. As the log output bears the name of the generating logger, this naming strategy makes it easy to identify the origin of a log message. However, this is only one possible, albeit common, strategy for naming loggers. Log4j does not restrict the possible set of loggers. The developer is free to name the loggers as desired.

Nevertheless, naming loggers after the class where they are located seems to be the best strategy known so far.
一般开发人员会在每个不同的classes中定义对应的logger,这样的好处很多(我们都是这么做的)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值