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>