changeset 1594:329197048ff0

add DailyRollingFileAppender
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 21 Mar 2021 00:59:58 -0600
parents ea62c2316b2e
children e4deb32bd233
files src/goodjava/logger/DailyRollingFileAppender.java src/goodjava/logger/GoodLogger.java src/goodjava/webserver/handlers/LogHandler.java
diffstat 3 files changed, 85 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
diff -r ea62c2316b2e -r 329197048ff0 src/goodjava/logger/DailyRollingFileAppender.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/goodjava/logger/DailyRollingFileAppender.java	Sun Mar 21 00:59:58 2021 -0600
@@ -0,0 +1,32 @@
+package goodjava.logger;
+
+import java.io.FileWriter;
+import java.io.IOException;
+import java.text.DateFormat;
+import java.util.Date;
+
+
+public final class DailyRollingFileAppender extends WriterAppender {
+	private final DateFormat format;
+	private String fileName;
+
+	public DailyRollingFileAppender(Layout layout,DateFormat format) {
+		super(layout,null);
+		this.format = format;
+	}
+
+	public synchronized void append(LoggingEvent event) {
+		String newFileName = format.format(new Date());
+		if( !newFileName.equals(fileName) ) {
+			fileName = newFileName;
+			if( writer != null )
+				close();
+			try {
+				writer = new FileWriter(fileName,true);
+			} catch(IOException e) {
+				throw new RuntimeException(e);
+			}
+		}
+		super.append(event);
+	}
+}
diff -r ea62c2316b2e -r 329197048ff0 src/goodjava/logger/GoodLogger.java
--- a/src/goodjava/logger/GoodLogger.java	Sat Mar 20 22:29:01 2021 -0600
+++ b/src/goodjava/logger/GoodLogger.java	Sun Mar 21 00:59:58 2021 -0600
@@ -8,7 +8,7 @@
 	public volatile int level;
 	public volatile Appender appender;
 
-	GoodLogger(String name) {
+	public GoodLogger(String name) {
 		this.name = name;
 	}
 
@@ -61,6 +61,6 @@
 	}
 
 	private boolean isEnabled(int level) {
-		return true;
+		return level >= this.level;
 	}
 }
diff -r ea62c2316b2e -r 329197048ff0 src/goodjava/webserver/handlers/LogHandler.java
--- a/src/goodjava/webserver/handlers/LogHandler.java	Sat Mar 20 22:29:01 2021 -0600
+++ b/src/goodjava/webserver/handlers/LogHandler.java	Sun Mar 21 00:59:58 2021 -0600
@@ -1,24 +1,72 @@
 package goodjava.webserver.handlers;
 
+import java.io.File;
+import java.io.IOException;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
 import goodjava.logging.Logger;
-import goodjava.logging.LoggerFactory;
+import goodjava.logger.Layout;
+import goodjava.logger.ListLayout;
+import goodjava.logger.DateLayout;
+import goodjava.logger.Layouts;
+import goodjava.logger.Appender;
+import goodjava.logger.ConsoleAppender;
+import goodjava.logger.DailyRollingFileAppender;
+import goodjava.logger.GoodLogger;
+import goodjava.logger.Level;
+import goodjava.io.IoUtils;
 import goodjava.webserver.Handler;
 import goodjava.webserver.Request;
 import goodjava.webserver.Response;
 
 
 public final class LogHandler implements Handler {
-	private static final Logger logger = LoggerFactory.getLogger("HTTP");
+
+	public static Logger consoleLogger() {
+		Layout layout = new ListLayout(new DateLayout("yyyy-MM-dd:HH:mm:ss")," ",Layouts.MESSAGE,"\n");
+		Appender appender = new ConsoleAppender(layout,System.err);
+		GoodLogger logger = new GoodLogger("HTTP");
+		logger.appender = appender;
+		logger.level = Level.INFO;
+		return logger;
+	}
+
+	public static Logger dirLogger(File dir) {
+		if( dir.exists() && !dir.isDirectory() )
+			throw new RuntimeException("must be a directory");
+		try {
+			IoUtils.mkdirs(dir);
+		} catch(IOException e) {
+			throw new RuntimeException(e);
+		}
+		Layout layout = new ListLayout(new DateLayout("yyyy-MM-dd:HH:mm:ss")," ",Layouts.MESSAGE,"\n");
+		DateFormat fmt = new SimpleDateFormat( "'" + dir + "/'yyyy_MM_dd_HH_mm'.log'" );
+		Appender appender = new DailyRollingFileAppender(layout,fmt);
+		GoodLogger logger = new GoodLogger("HTTP");
+		logger.appender = appender;
+		logger.level = Level.INFO;
+		return logger;
+	}
 
 	private final Handler handler;
+	private final Logger logger;
 
 	public LogHandler(Handler handler) {
+		this(handler,consoleLogger());
+	}
+
+	public LogHandler(Handler handler,Logger logger) {
 		this.handler = handler;
+		this.logger = logger;
 	}
 
 	public Response handle(Request request) {
 		Response response = handler.handle(request);
-		logger.info( request.method + " " + request.path + " " + response.status.code + " " + response.body.length );
+		if( response == null )
+			return null;
+		String ip = (String)request.headers.get("x-real-ip");
+		//String agent = (String)request.headers.get("user-agent");
+		logger.info( ip + " \"" + request.method + " " + request.path + "\" " + response.status.code + " " + response.body.length );
 		return response;
 	}
 }