java日誌框架系列(7):logback框架Layout詳解

1.Layout

layout從字面意思來看就是排版、佈局咯。app

1.Layout簡介

功能負責把事件轉換成字符串。Layout接口的格式化方法doLayout()負責將表明任何類型的事件的轉換成一個String對象並返回給調用者。佈局

經常使用事件類型:ILoggingEvent。this

Layout接口概要以下 :spa

public interface Layout<E> extends ContextAware, LifeCycle {
String doLayout(E event);
String getFileHeader();
String getPresentationHeader();
String getFileFooter();
String getPresentationFooter();
String getContentType();
}

接口很簡單,卻足夠完成不少格式化需求。線程

2.Logback處理的事件類型種類

logback-classic模塊中只會處理ch.qos.logback.classic.spi.ILoggingEvent類型的事件debug

3.自定義Layout

讓咱們實現一個簡單卻可工做的 layout,打印內容包括:自程序啓動以來逝去的時間、記錄事件的級別、包含在方括號裏的調用者線程的名字、logger 名、連字符、事件消息和換行。
輸出相似於:

 

 示例1:code

想要自定義layout,能夠經過繼承LayoutBase類,並重寫doLayout方法,代碼以下:orm

package chapters.layouts;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.CoreConstants;
import ch.qos.logback.core.LayoutBase;
public class MySampleLayout extends LayoutBase<ILoggingEvent> {
  public String doLayout(ILoggingEvent event) {
    StringBuffer sbuf = new StringBuffer(128);
    sbuf.append(event.getTimeStamp()- event.getLoggerContextVO().getBirthTime());
    sbuf.append(" ");
    sbuf.append(event.getLevel());
    sbuf.append(" [");
    sbuf.append(event.getThreadName());
    sbuf.append("] ");
    sbuf.append(event.getLoggerName());
    sbuf.append(" - ");
    sbuf.append(event.getFormattedMessage());
    sbuf.append(CoreConstants.LINE_SEPARATOR);
    return sbuf.toString();
  } 
}

 編寫完代碼以後,須要在xml文件中配置自定義的layout,配置以下:xml

<configuration debug="true">
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> 
    <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
      <layout class="chapters.layouts.MySampleLayout" />     </encoder>   </appender>
  <root level="DEBUG">
    <appender-ref ref="STDOUT" />   </root> </configuration>

因爲在自定義類MySampleLayout中已經實現了繼承的doLayout()方法,所以,在<encoder>標籤中經過子標籤<layout>指定已經實現轉換功能的類MySampleLayout,該類MySampleLayout負責將ILoggingEvent類型事件轉換爲String類型。對象

4.爲自定義的Layout添加參數

如何爲 layout 增長選項?爲 layout 或任何 logback 的其餘組件添加屬性很是簡單:聲明一個屬性及 setter 方法接便可。MySampleLayout2 類包含兩個屬性。第一個是爲輸出添加的前綴。第二個是用於選擇是否顯示記錄請求的線程名。
示例1:
代碼以下:
package chapters.layouts;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.CoreConstants;
import ch.qos.logback.core.LayoutBase;
public class MySampleLayout2 extends LayoutBase<ILoggingEvent> {   String prefix = null;   boolean printThreadName = true;   public void setPrefix(String prefix) {     this.prefix = prefix;   }   public void setPrintThreadName(boolean printThreadName) {     this.printThreadName = printThreadName;   }   public String doLayout(ILoggingEvent event) {     StringBuffer sbuf = new StringBuffer(128);     if (prefix != null) {       sbuf.append(prefix + ": ");     }     sbuf.append(event.getTimeStamp()- event.getLoggerContextVO().getBirthTime());     sbuf.append(" ");     sbuf.append(event.getLevel());     if (printThreadName) {       sbuf.append(" [");       sbuf.append(event.getThreadName());       sbuf.append("] ");     } else {       sbuf.append(" ");     }     sbuf.append(event.getLoggerName());     sbuf.append(" - ");     sbuf.append(event.getFormattedMessage());     sbuf.append(CoreConstants.LINE_SEPARATOR);     return sbuf.toString();   }
}
在配置裏啓用屬性只須要有對應的 setter 方法便可。下面是 MySampleLayout2 所用的配置文件。
<configuration> 
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
      <layout class="chapters.layouts.MySampleLayout2">
        <prefix>MyPrefix</prefix>
        <printThreadName>false</printThreadName>       </layout>     </encoder>   </appender>
  <root level="debug">
    <appender-ref ref="STDOUT" />   </root> </configuration>
相關文章
相關標籤/搜索