Logback 打印方法和基本选择规则

根据定义,打印方法决定记录请求的级别。例如,如果L是一个logger实例,那么,语句L.info(“…”)是一条级别为INFO的记录语句。

记录请求的级别在高于或等于其logger的有效级别时被称为被启用,否则,称为被禁用。如前所述,没有被分配级别的logger将从其最近的祖先继承级别。该规则总结如下:

基本选择规则
记录请求级别为p,其logger的有效级别为q,只有则当p>=q时,该请求才会被执行。

该规则是logback的核心。级别排序为:TRACE < DEBUG < INFO < WARN < ERROR。

下表显示了选择规则是如何工作的。行头是记录请求的级别p。列头是logger的有效级别q。

行(请求级别)与列(有效级别)的交叉部分是按照基本选择规则得出的布尔值。

下面是基本选择规则的例子。[code]import ch.qos.logback.classic.Level;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

// get a logger instance named “com.foo”. Let us further assume that the
// logger is of type ch.qos.logback.classic.Logger so that we can
// set its level
ch.qos.logback.classic.Logger logger =
(ch.qos.logback.classic.Logger) LoggerFactory.getLogger(“com.foo”);
//set its Level to INFO. The setLevel() method requires a logback logger
logger.setLevel(Level. INFO);

Logger barlogger = LoggerFactory.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”);[/code]上例的logger.setLevel(Level.INFO)无效。org.slf4j.Logger没有setLevel()方法,ch.qos.logback.classic.Logger有此方法。

REF:http://cwiki.ossez.com/pages/viewpage.action?pageId=4719684