changeset 1449:dd14d2dce7ee

add appenders
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 28 Feb 2020 21:14:23 -0700
parents 6fc083e1d08c
children 28b1ddacfad3
files src/goodjava/logger/Level.java src/goodjava/logger/LevelAppender.java src/goodjava/logger/ListAppender.java src/goodjava/logger/RollingFileAppender.java
diffstat 4 files changed, 100 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/goodjava/logger/Level.java	Sun Feb 23 18:14:32 2020 -0700
+++ b/src/goodjava/logger/Level.java	Fri Feb 28 21:14:23 2020 -0700
@@ -1,7 +1,7 @@
 package goodjava.logger;
 
 
-final class Level {
+public final class Level {
 	public final static int DEBUG = 0;
 	public final static int INFO = 1;
 	public final static int WARN = 2;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/goodjava/logger/LevelAppender.java	Fri Feb 28 21:14:23 2020 -0700
@@ -0,0 +1,21 @@
+package goodjava.logger;
+
+
+public class LevelAppender implements Appender {
+	private final Appender appender;
+	private final int level;
+
+	public LevelAppender(Appender appender,int level) {
+		this.appender = appender;
+		this.level = level;
+	}
+
+	public void append(LoggingEvent event) {
+		if( event.level >= this.level )
+			appender.append(event);
+	}
+
+	public void close() {
+		appender.close();
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/goodjava/logger/ListAppender.java	Fri Feb 28 21:14:23 2020 -0700
@@ -0,0 +1,22 @@
+package goodjava.logger;
+
+
+public class ListAppender implements Appender {
+	private final Appender[] appenders;
+
+	public ListAppender(Appender... appenders) {
+		this.appenders = appenders;
+	}
+
+	public synchronized void append(LoggingEvent event) {
+		for( Appender appender : appenders ) {
+			appender.append(event);
+		}
+	}
+
+	public void close() {
+		for( Appender appender : appenders ) {
+			appender.close();
+		}
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/goodjava/logger/RollingFileAppender.java	Fri Feb 28 21:14:23 2020 -0700
@@ -0,0 +1,56 @@
+package goodjava.logger;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+
+
+public class RollingFileAppender extends WriterAppender {
+	protected final String fileName;
+	protected final File file;
+	public long maxFileSize = 10*1024*1024;
+	public int backups = 1;
+
+	public RollingFileAppender(Layout layout,String fileName) throws IOException {
+		super(layout,null);
+		this.fileName = fileName;
+		this.file = new File(fileName);
+		open();
+	}
+
+	protected void open() throws IOException {
+		this.writer = new FileWriter(file,true);
+	}
+
+	public synchronized void append(LoggingEvent event) {
+		super.append(event);
+		if( file.length() > maxFileSize ) {
+			rollOver();
+		}
+	}
+
+	protected void rollOver() {
+		close();
+		File backup = new File(fileName+'.'+backups);
+		if( backup.exists() ) {
+			if( !backup.delete() )
+				debug("couldn't delete "+backup);
+		}
+		for( int i=backups-1; i>=1; i-- ) {
+			File f = backup;
+			backup = new File(fileName+'.'+i);
+			backup.renameTo(f);
+		}
+		if( !file.renameTo(backup) )
+			debug("couldn't rename "+file+" to "+backup);
+		try {
+			open();
+		} catch(IOException e) {
+			throw new RuntimeException(e);
+		}
+	}
+
+	protected void debug(String msg) {
+		System.err.println("RollingFileAppender: "+msg);
+	}
+}