layout從字面意思來看就是排版、佈局咯。app
功能:負責把事件轉換成字符串。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(); }
接口很簡單,卻足夠完成不少格式化需求。線程
logback-classic模塊中只會處理ch.qos.logback.classic.spi.ILoggingEvent類型的事件。debug
示例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類型。對象
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(); }
}
<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>