logback.配置

来源:互联网 发布:淘宝提前收款 编辑:程序博客网 时间:2024/05/20 10:12
1.Logger rootLogger = LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
可以直接使用Root_LOGGER_ANME

2.If a given logger is not assigned a level, then it inherits one from its closest ancestor with an assigned level. More formally:

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

To ensure that all loggers can eventually inherit a level, the root logger always has an assigned level. By default, this level is DEBUG.

如果一个Logger没有指定级别,那么它将继承自最近的父类的级别,而root的存在是为了确保所有的Logger有一个确定的级别。



3.// 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名创建

4.

Logger NameAttached AppendersAdditivity FlagOutput TargetsCommentrootA1not applicableA1Since the root logger stands at the top of the logger hierarchy, the additivity flag does not apply to it.xA-x1, A-x2trueA1, A-x1, A-x2Appenders of "x" and of root.x.ynonetrueA1, A-x1, A-x2Appenders of "x" and of root.x.y.zA-xyz1trueA1, A-x1, A-x2, A-xyz1Appenders of "x.y.z", "x" and of root.securityA-secfalseA-secNo appender accumulation since the additivity flag is set to false. Only appender A-sec will be used.security.accessnonetrueA-secOnly appenders of "security" because the additivity flag in "security" is set to false.Additivity的作用



5.if(logger.isDebugEnabled()) { 
  logger.debug("Entry number: " + i + " is " + String.valueOf(entry[i]));
}
In practice, this overhead is insignificant because evaluating a logger takes less than 1% of the time it takes to actually log a request.

Better alternative
logger.debug("The entry is {}.", entry);  //这种方式更好

. In other words, this form does not incur the cost of parameter construction when the log statement is disabled.


6.

<?xml version="1.0" encoding="UTF-8"?><configuration>    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">        <!-- encoder的默认实现类是ch.qos.logback.classic.encoder.PatternLayoutEncoder -->        <encoder>            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level [%file:%line] %logger{20} - %msg%n</pattern>        </encoder>    </appender>    <appender name="fileAppender" class="ch.qos.logback.core.FileAppender">        <file>granularity.log</file>        <encoder><!-- 必须指定,否则不会往文件输出内容 -->            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level [%file:%line]  %logger{20} - %msg%n</pattern>        </encoder>        <append>true</append>        <prudent>false</prudent>    </appender>    //不同级别打印到不同文件中,使用filter    <appender name="errorAppender" class="ch.qos.logback.core.FileAppender">        <file>log/error.log</file>        <encoder>            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level [%file:%line]  %logger{20} - %msg%n</pattern>        </encoder>        <filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印错误日志 -->            <level>ERROR</level>            <onMatch>ACCEPT</onMatch>            <onMismatch>DENY</onMismatch>        </filter>    </appender>    //默认配置级别    <root level="debug">        <appender-ref ref="errorAppender" />        <appender-ref ref="STDOUT" />        <appender-ref ref="fileAppender" />    </root>    //指定配置级别    <logger name="com2" level="INFO"></logger></configuration>


0 0
原创粉丝点击