diff src/goodjava/logger/WriterAppender.java @ 1448:6fc083e1d08c

start logger
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 23 Feb 2020 18:14:32 -0700
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/goodjava/logger/WriterAppender.java	Sun Feb 23 18:14:32 2020 -0700
@@ -0,0 +1,40 @@
+package goodjava.logger;
+
+import java.io.Writer;
+import java.io.IOException;
+
+
+public class WriterAppender implements Appender {
+	protected final Layout layout;
+	protected Writer writer;
+
+	public WriterAppender(Layout layout,Writer writer) {
+		this.layout = layout;
+		this.writer = writer;
+	}
+
+	public synchronized void append(LoggingEvent event) {
+		try {
+			writer.write( layout.format(event) );
+			flush();
+		} catch(IOException e) {
+			printStackTrace(e);
+		}
+	}
+
+	protected void flush() throws IOException {
+		writer.flush();
+	}
+
+	public void close() {
+		try {
+			writer.close();
+		} catch(IOException e) {
+			printStackTrace(e);
+		}
+	}
+
+	protected void printStackTrace(IOException e) {
+		e.printStackTrace();
+	}
+}