001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package chapters.layouts;
015
016import ch.qos.logback.classic.spi.ILoggingEvent;
017import ch.qos.logback.core.CoreConstants;
018import ch.qos.logback.core.LayoutBase;
019
020public class MySampleLayout2 extends LayoutBase<ILoggingEvent> {
021
022    String prefix = null;
023    boolean printThreadName = true;
024
025    public void setPrefix(String prefix) {
026        this.prefix = prefix;
027    }
028
029    public void setPrintThreadName(boolean printThreadName) {
030        this.printThreadName = printThreadName;
031    }
032
033    public String doLayout(ILoggingEvent event) {
034        StringBuilder sbuf = new StringBuilder(128);
035        if (prefix != null) {
036            sbuf.append(prefix + ": ");
037        }
038        sbuf.append(event.getTimeStamp() - event.getLoggerContextVO().getBirthTime());
039        sbuf.append(" ");
040        sbuf.append(event.getLevel());
041        if (printThreadName) {
042            sbuf.append(" [");
043            sbuf.append(event.getThreadName());
044            sbuf.append("] ");
045        } else {
046            sbuf.append(" ");
047        }
048        sbuf.append(event.getLoggerName());
049        sbuf.append(" - ");
050        sbuf.append(event.getFormattedMessage());
051        sbuf.append(CoreConstants.LINE_SEPARATOR);
052        return sbuf.toString();
053    }
054}