changeset 1682:97cc73664ca8

improve RollingFileAppender
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 05 Jun 2022 10:51:43 -0600
parents 6061be2cd84b
children ea56e3b04c03
files src/goodjava/logger/RollingFileAppender.java src/goodjava/logger/examples/RollingFiles.java src/luan/host/init.luan src/luan/host/run.luan
diffstat 4 files changed, 27 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
diff -r 6061be2cd84b -r 97cc73664ca8 src/goodjava/logger/RollingFileAppender.java
--- a/src/goodjava/logger/RollingFileAppender.java	Fri Jun 03 09:17:21 2022 -0600
+++ b/src/goodjava/logger/RollingFileAppender.java	Sun Jun 05 10:51:43 2022 -0600
@@ -7,15 +7,14 @@
 
 
 public class RollingFileAppender extends WriterAppender {
-	protected final String fileName;
+	protected final String[] fileNames;
 	protected final File file;
 	public long maxFileSize = 10*1024*1024;
-	public int backups = 1;
 
-	public RollingFileAppender(Layout layout,String fileName) throws IOException {
+	public RollingFileAppender(Layout layout,String[] fileNames) throws IOException {
 		super(layout,null);
-		this.fileName = fileName;
-		this.file = new File(fileName);
+		this.fileNames = fileNames;
+		this.file = new File(fileNames[0]);
 		open();
 	}
 
@@ -32,12 +31,12 @@
 
 	protected void rollOver() {
 		close();
-		File backup = new File(fileName+'.'+backups);
+		File backup = new File(fileNames[fileNames.length-1]);
 		try {
 			IoUtils.delete(backup);
-			for( int i=backups-1; i>=1; i-- ) {
+			for( int i=fileNames.length-2; i>=1; i-- ) {
 				File f = backup;
-				backup = new File(fileName+'.'+i);
+				backup = new File(fileNames[i]);
 				IoUtils.move(backup,f);
 			}
 			IoUtils.move(file,backup);
diff -r 6061be2cd84b -r 97cc73664ca8 src/goodjava/logger/examples/RollingFiles.java
--- a/src/goodjava/logger/examples/RollingFiles.java	Fri Jun 03 09:17:21 2022 -0600
+++ b/src/goodjava/logger/examples/RollingFiles.java	Sun Jun 05 10:51:43 2022 -0600
@@ -29,15 +29,16 @@
 
 	static void config() throws IOException {
 		Layout layout = new ListLayout(new DateLayout("yyyy-MM-dd HH:mm:ss,SSS")," ",Layouts.LEVEL_PADDED," ",Layouts.LOGGER," - ",Layouts.MESSAGE,"\n",Layouts.THROWABLE);
-		Appender error = appender(layout,"error.log",Level.ERROR);
-		Appender warn = appender(layout,"warn.log",Level.WARN);
-		Appender info = appender(layout,"info.log",Level.INFO);
+		Appender error = appender(layout,"error",Level.ERROR);
+		Appender warn = appender(layout,"warn",Level.WARN);
+		Appender info = appender(layout,"info",Level.INFO);
 		Appender appender = new ListAppender(error,warn,info);
 		GoodLoggerFactory.setConfigurer( new SimpleConfigurer(Level.INFO,appender) );
 	}
 
-	static Appender appender(Layout layout,String fileName,int level) throws IOException {
-		RollingFileAppender appender = new RollingFileAppender(layout,fileName);
+	static Appender appender(Layout layout,String name,int level) throws IOException {
+		String[] fileNames = new String[]{ name+".log", name+"_1.log" };
+		RollingFileAppender appender = new RollingFileAppender(layout,fileNames);
 		return new LevelAppender(appender,level);
 	}
 }
diff -r 6061be2cd84b -r 97cc73664ca8 src/luan/host/init.luan
--- a/src/luan/host/init.luan	Fri Jun 03 09:17:21 2022 -0600
+++ b/src/luan/host/init.luan	Sun Jun 05 10:51:43 2022 -0600
@@ -30,15 +30,20 @@
 	local log_dir = dir.."/site/private/local/logs/"
 
 	local function new_appender(file,level)
-		local appender = RollingFileAppender.new(layout, log_dir..file)
+		-- tmp
+		local Io = require "luan:Io.luan"
+		Io.uri("file:"..log_dir..file..".log.1").delete()
+
+		local files = { log_dir..file..".log", log_dir..file.."_1.log" }
+		local appender = RollingFileAppender.new(layout, files)
 		appender.maxFileSize = one_mb
 		appender = LevelAppender.new(appender,level)
 		return appender
 	end
 
-	local err = new_appender("error.log",Level.ERROR)
-	local warn = new_appender("warn.log",Level.WARN)
-	local info = new_appender("info.log",Level.INFO)
+	local err = new_appender("error",Level.ERROR)
+	local warn = new_appender("warn",Level.WARN)
+	local info = new_appender("info",Level.INFO)
 	local appender = ListAppender.new(err,warn,info)
 	LuanLogger.configure(appender)
 end
diff -r 6061be2cd84b -r 97cc73664ca8 src/luan/host/run.luan
--- a/src/luan/host/run.luan	Fri Jun 03 09:17:21 2022 -0600
+++ b/src/luan/host/run.luan	Sun Jun 05 10:51:43 2022 -0600
@@ -30,15 +30,16 @@
 	local layout = ListLayout.new(DateLayout.new("yyyy-MM-dd HH:mm:ss,SSS")," ",Layouts.LEVEL_PADDED," ",Layouts.LOGGER," - ",Layouts.MESSAGE,"\n",Layouts.THROWABLE)
 
 	local function new_appender(file,level)
-		local appender = RollingFileAppender.new(layout, file)
+		local files = { file..".log", file.."_1.log" }
+		local appender = RollingFileAppender.new(layout, files)
 		appender.maxFileSize = one_mb
 		appender = LevelAppender.new(appender,level)
 		return appender
 	end
 
-	local err = new_appender("logs/luan_error.log",Level.ERROR)
-	local warn = new_appender("logs/luan_warn.log",Level.WARN)
-	local info = new_appender("logs/luan_info.log",Level.INFO)
+	local err = new_appender("logs/luan_error",Level.ERROR)
+	local warn = new_appender("logs/luan_warn",Level.WARN)
+	local info = new_appender("logs/luan_info",Level.INFO)
 	local appender = ListAppender.new(err,warn,info)
 	LuanLogger.configure(appender)
 end