首页  

logback简介     所属分类 logback 浏览量 286
logback 分成三个模块
logback-core logback-classic logback-access
logback-core是其它两个模块的基础模块
logback-classic是Log4j的一个改良版本
logback-classic完整实现SLF4J API 
可以方便地更换成其它日记系统如Log4j或j.u.l

logback-access 与Servlet容器集成,提供 Http访问日记的功能
The logback-access module, part of the standard logback distribution, 
integrates with Servlet containers such as Jetty or Tomcat to provide rich and powerful HTTP-access log functionality.


核心对象
Logger Appender Layout

org.slf4j.Logger;
org.slf4j.LoggerFactory;
final static Logger logger = LoggerFactory.getLogger("xxx.class");
 
logger.trace("trace");
logger.debug("debug");
logger.info("info");
logger.warn("warn");
logger.error("error");

日志级别
ch.qos.logback.classic.Level

过滤器基于三值逻辑(ternary logic)
参考 Linux iptables
ACCEPT DENY NEUTRAL(中立)
DENY    事件立即被抛弃,不再经过剩余过滤器
NEUTRAL 下一个过滤器会接着处理事件
ACCEPT  事件被立即处理,不再经过剩余过滤器


Logback整体流程
Logger 产生日志信息
Layout 修饰这条msg的显示格式
Filter 过滤显示内容
Appender  输出

两种类型的过滤器
常规过滤器 和 TuroboFilter过滤器

EvaluatorFilter 过滤器 过滤日志内容 ,增加依赖 janino-2.3.2.jar


<?xml version="1.0" encoding="UTF-8"?>
  <configuration>
      <!-- 控制台输出 -->
       <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
           <encoder>
               <pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern>
           </encoder>
       </appender> 
      <!-- 时间滚动输出 level为 DEBUG 日志 -->
      <appender name="file—debug"
          class="ch.qos.logback.core.rolling.RollingFileAppender">
          <filter class="ch.qos.logback.classic.filter.LevelFilter">
              <level>DEBUG</level>
              <onMatch>ACCEPT</onMatch>
              <onMismatch>DENY </onMismatch>
          </filter>
          <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
              <FileNamePattern>/logs/debug.%d{yyyy-MM-dd}.log</FileNamePattern>
              <MaxHistory>30</MaxHistory>
          </rollingPolicy>
          <encoder>
              <pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern>
          </encoder>
      </appender>
  
      <!-- 时间滚动输出 level为 ERROR 日志 -->
      <appender name="file—error"
          class="ch.qos.logback.core.rolling.RollingFileAppender">
          <filter class="ch.qos.logback.classic.filter.LevelFilter">
              <level>ERROR</level>
              <onMatch>ACCEPT</onMatch>
              <onMismatch>DENY</onMismatch>
          </filter>
          <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
              <FileNamePattern>/logs/error.%d{yyyy-MM-dd}.log</FileNamePattern>
              <MaxHistory>30</MaxHistory>
          </rollingPolicy>
          <encoder>
              <pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern>
          </encoder>
      </appender>
  
      <!-- 特定过滤含有某字符串的日志 -->
      <appender name="file-str"
          class="ch.qos.logback.core.rolling.RollingFileAppender">
          <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
              <evaluator>
                  <expression>message.contains("str")</expression>
              </evaluator>
              <onMatch>ACCEPT</onMatch>
              <onMismatch>DENY</onMismatch>
          </filter>
          <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
              <FileNamePattern>/logs/contains.%d{yyyy-MM-dd}.log
              </FileNamePattern>
              <MaxHistory>30</MaxHistory>
          </rollingPolicy>
          <encoder>
              <pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern>
          </encoder>
      </appender>
  
      <!-- 数据库输出 -->
      <appender name="db" class="ch.qos.logback.classic.db.DBAppender">
          <connectionSource
              class="ch.qos.logback.core.db.DriverManagerConnectionSource">
              <driverClass>com.mysql.jdbc.Driver</driverClass>
              <url>jdbc:mysql://host_name:3306/datebase_name</url>
              <user>username</user>
              <password>password</password>
          </connectionSource>
      </appender>
  
      <logger name="java.sql.Connection">
          <level value="DEBUG" />
      </logger>
 
 
     <root level="DEBUG">
         <appender-ref ref="stdout" />
         <appender-ref ref="file—debug" />
         <appender-ref ref="file—error" />
         <appender-ref ref="file-str" />
         <appender-ref ref="db" />
     </root>
 </configuration>


上一篇     下一篇
JFR实战

log4j2使用要点

JNDI demo

MySQL获取所有表和表结构信息

BI常用词汇

akka简介