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.migrationFromLog4j; 015 016import ch.qos.logback.classic.spi.ILoggingEvent; 017import ch.qos.logback.core.AppenderBase; 018import ch.qos.logback.core.Layout; 019 020public class TrivialLogbackAppender extends AppenderBase<ILoggingEvent> { 021 022 Layout<ILoggingEvent> layout; 023 024 @Override 025 public void start() { 026 if (this.layout == null) { 027 addError("No layout set for the appender named [" + name + "]."); 028 return; 029 } 030 String header = layout.getFileHeader(); 031 System.out.println(header); 032 super.start(); 033 } 034 035 @Override 036 protected void append(ILoggingEvent loggingEvent) { 037 // note that AppenderBase.doAppend will invoke this method only if 038 // this appender was successfully started. 039 String eventAsStr = this.layout.doLayout(loggingEvent); 040 System.out.println(eventAsStr); 041 } 042 043 044 public Layout<ILoggingEvent> getLayout() { 045 return layout; 046 } 047 048 public void setLayout(Layout<ILoggingEvent> layout) { 049 this.layout = layout; 050 } 051 052}