changeset 1321:307e76ccd0d6

generalize separate logging
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 05 Feb 2019 22:36:55 -0700
parents 2c8d1d76a82a
children d5739c101bd6
files conv.txt src/luan/host/Backup.java src/luan/host/Init.luan src/luan/host/WebHandler.java src/luan/modules/Boot.luan src/luan/modules/http/LuanDomainHandler.java src/luan/modules/http/LuanHandler.java src/luan/modules/http/serve.luan src/luan/modules/http/test.luan src/luan/modules/logging/Logging.luan src/luan/modules/logging/LuanLogger.java
diffstat 11 files changed, 75 insertions(+), 57 deletions(-) [+]
line wrap: on
line diff
--- a/conv.txt	Mon Feb 04 20:18:51 2019 -0700
+++ b/conv.txt	Tue Feb 05 22:36:55 2019 -0700
@@ -1,3 +1,5 @@
+Logging.luanhost_logger
+Logging.init
 authorization user
 Hosting.domain
 
--- a/src/luan/host/Backup.java	Mon Feb 04 20:18:51 2019 -0700
+++ b/src/luan/host/Backup.java	Tue Feb 05 22:36:55 2019 -0700
@@ -85,7 +85,7 @@
 						onClose.clear();
 					}
 				};
-				WebHandler.initLuan( luan, from.toString(), from.getName() );
+				WebHandler.initLuan( luan, from.toString(), from.getName(), false );
 				PackageLuan.load(luan,"site:/init.luan");
 				luceneInstances = (LuanTable)luan.eval(getLucenes);
 			}
--- a/src/luan/host/Init.luan	Mon Feb 04 20:18:51 2019 -0700
+++ b/src/luan/host/Init.luan	Tue Feb 05 22:36:55 2019 -0700
@@ -7,24 +7,21 @@
 
 local Init = {}
 
-local dir, domain = ...
+local dir, domain, logging = ...
 
 
 -- logging
 
-java()
-local Logger = require "java:org.apache.log4j.Logger"
-local Level = require "java:org.apache.log4j.Level"
-local EnhancedPatternLayout = require "java:org.apache.log4j.EnhancedPatternLayout"
-local RollingFileAppender = require "java:org.apache.log4j.RollingFileAppender"
-
-local root = gsub(domain,"\.",":")
-
-local logger = Logger.getLogger(root)
-local has_appenders = logger.getAllAppenders().hasMoreElements()
-if not has_appenders then
-	logger.setAdditivity(false)
-	local layout = EnhancedPatternLayout.new("%d %-5p %c{-1} - %m%n")
+if logging then
+	java()
+	local LuanLogger = require "java:luan.modules.logging.LuanLogger"
+	local Level = require "java:org.apache.log4j.Level"
+	local EnhancedPatternLayout = require "java:org.apache.log4j.EnhancedPatternLayout"
+	local RollingFileAppender = require "java:org.apache.log4j.RollingFileAppender"
+	
+	local logger = LuanLogger.getRootLogger()
+	logger.removeAllAppenders()
+	local layout = EnhancedPatternLayout.new("%d %-5p %c - %m%n")
 	local log_dir = dir.."/site/private/local/logs/"
 
 	local function add_appender(file,level)
@@ -41,20 +38,6 @@
 	add_appender("info.log",Level.INFO)
 end
 
-local Logging = require "luan:logging/Logging.luan"
-
-local old_logger = Logging.logger
-Logging.luanhost_logger = old_logger
-
-function Logging.root_logger()
-	return old_logger(root)
-end
-
-function Logging.logger(name)
-	return old_logger( root .. "." .. name )
-end
-
-Init.logger_root = root.."."
 
 
 -- set vars
--- a/src/luan/host/WebHandler.java	Mon Feb 04 20:18:51 2019 -0700
+++ b/src/luan/host/WebHandler.java	Tue Feb 05 22:36:55 2019 -0700
@@ -13,6 +13,7 @@
 import luan.modules.IoLuan;
 import luan.modules.JavaLuan;
 import luan.modules.http.LuanHandler;
+import luan.modules.logging.LuanLogger;
 
 
 public class WebHandler implements Handler {
@@ -29,9 +30,13 @@
 			new File(logDir).mkdirs();
 
 			LuanState luan = new LuanState();
-			LuanTable init = initLuan(luan,dirStr,domain);
-			String loggerRoot = (String)init.rawGet("logger_root");
-			return new LuanHandler(luan,loggerRoot);
+			try {
+				LuanLogger.newLoggerRepository(luan);
+			} catch(LuanException e) {
+				throw new RuntimeException(e);
+			}
+			initLuan(luan,dirStr,domain,true);
+			return new LuanHandler(luan);
 		}
 	};
 
@@ -85,13 +90,13 @@
 		return true;
 	}
 */
-	static LuanTable initLuan(LuanState luan,String dir,String domain) {
+	static LuanTable initLuan(LuanState luan,String dir,String domain,boolean logging) {
 		LuanTable init;
 		try {
 			init = (LuanTable)luan.eval(
 				"local Luan = require 'luan:Luan.luan'\n"
 				+"local f = Luan.load_file 'classpath:luan/host/Init.luan'\n"
-				+"return f('"+dir+"','"+domain+"')\n"
+				+"return f('"+dir+"','"+domain+"',"+logging+")\n"
 			);
 		} catch(LuanException e) {
 			throw new RuntimeException(e);
--- a/src/luan/modules/Boot.luan	Mon Feb 04 20:18:51 2019 -0700
+++ b/src/luan/modules/Boot.luan	Tue Feb 05 22:36:55 2019 -0700
@@ -9,6 +9,7 @@
 local match = StringLuan.match  -- String.match
 local IoLuan = require "java:luan.modules.IoLuan"
 local LuanUrl = require "java:luan.modules.url.LuanUrl"
+IoLuan.unrestricted()  -- not right
 
 
 local Boot = {}
--- a/src/luan/modules/http/LuanDomainHandler.java	Mon Feb 04 20:18:51 2019 -0700
+++ b/src/luan/modules/http/LuanDomainHandler.java	Tue Feb 05 22:36:55 2019 -0700
@@ -8,6 +8,7 @@
 import luan.LuanTable;
 import luan.LuanCloner;
 import luan.LuanException;
+import luan.modules.logging.LuanLogger;
 
 
 public final class LuanDomainHandler implements Handler {
@@ -19,12 +20,13 @@
 			LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE);
 			LuanState luan = (LuanState)cloner.clone(luanInit);
 			try {
+				LuanLogger.newLoggerRepository(luan);
 				LuanTable Http = (LuanTable)luan.require("luan:http/Http.luan");
 				Http.put( "domain", domain );
 			} catch(LuanException e) {
 				throw new RuntimeException(e);
 			}
-			return new LuanHandler(luan,null);
+			return new LuanHandler(luan);
 		}
 	};
 
--- a/src/luan/modules/http/LuanHandler.java	Mon Feb 04 20:18:51 2019 -0700
+++ b/src/luan/modules/http/LuanHandler.java	Tue Feb 05 22:36:55 2019 -0700
@@ -12,8 +12,7 @@
 import java.util.ArrayList;
 import java.util.concurrent.locks.ReadWriteLock;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.apache.log4j.Logger;
 import luan.webserver.Request;
 import luan.webserver.Response;
 import luan.webserver.Status;
@@ -29,6 +28,7 @@
 import luan.LuanException;
 import luan.modules.PackageLuan;
 import luan.modules.BasicLuan;
+import luan.modules.logging.LuanLogger;
 
 
 public final class LuanHandler implements Handler, LuanState.OnClose {
@@ -52,11 +52,9 @@
 		}
 	}
 
-	public LuanHandler(LuanState luanInit,String loggerRoot) {
+	public LuanHandler(LuanState luanInit) {
 		this.luanInit = luanInit;
-		if( loggerRoot==null )
-			loggerRoot = "";
-		logger = LoggerFactory.getLogger(loggerRoot+LuanHandler.class.getName());
+		this.logger = LuanLogger.getLogger(luanInit,LuanHandler.class.getName());
 		try {
 			LuanTable Http = (LuanTable)luanInit.require("luan:http/Http.luan");
 			Http.put( "reset_luan", new LuanJavaFunction(resetLuanMethod,this) );
@@ -75,6 +73,7 @@
 		try {
 			PackageLuan.load(luan,"site:/init.luan");
 		} catch(LuanException e) {
+			//e.printStackTrace();
 			String err = e.getLuanStackTraceString();
 			logger.error(err);
 		}
--- a/src/luan/modules/http/serve.luan	Mon Feb 04 20:18:51 2019 -0700
+++ b/src/luan/modules/http/serve.luan	Tue Feb 05 22:36:55 2019 -0700
@@ -1,6 +1,5 @@
 local Io = require "luan:Io.luan"
 local Server = require "luan:http/Server.luan"
-local Logging = require "luan:logging/Logging.luan"
 
 
 if #{...} ~= 1 then
@@ -8,5 +7,4 @@
 	return
 end
 
-Logging.init()
 Server.serve(...)
--- a/src/luan/modules/http/test.luan	Mon Feb 04 20:18:51 2019 -0700
+++ b/src/luan/modules/http/test.luan	Tue Feb 05 22:36:55 2019 -0700
@@ -1,7 +1,6 @@
 local Luan = require "luan:Luan.luan"
 local Io = require "luan:Io.luan"
 local Server = require "luan:http/Server.luan"
-local Logging = require "luan:logging/Logging.luan"
 
 
 if #{...} ~= 2 then
@@ -11,6 +10,5 @@
 
 local dir, test = ...
 
-Logging.init()
 Server.init_dir(dir)
 Luan.do_file(test)
--- a/src/luan/modules/logging/Logging.luan	Mon Feb 04 20:18:51 2019 -0700
+++ b/src/luan/modules/logging/Logging.luan	Tue Feb 05 22:36:55 2019 -0700
@@ -1,7 +1,6 @@
 local Luan = require "luan:Luan.luan"
 local error = Luan.error
 java()
-local Logger = require "java:org.apache.log4j.Logger"
 local EnhancedPatternLayout = require "java:org.apache.log4j.EnhancedPatternLayout"
 local ConsoleAppender = require "java:org.apache.log4j.ConsoleAppender"
 local Level = require "java:org.apache.log4j.Level"
@@ -10,18 +9,17 @@
 
 local Logging = {}
 
-function Logging.init()
-	local root = Logger.getRootLogger()
+function Logging.init_root()
+	local root = LuanLogger.getRootLogger()
 	local has_appenders = root.getAllAppenders().hasMoreElements()
-	if has_appenders then
-		return false
+	if not has_appenders then
+		root.setLevel(Level.INFO)
+		local layout = EnhancedPatternLayout.new("%d %-5p %c - %m%n")
+		local appender = ConsoleAppender.new(layout,"System.err");
+		root.addAppender(appender);
 	end
-	root.setLevel(Level.INFO)
-	local layout = EnhancedPatternLayout.new("%d %-5p %c - %m%n")
-	local appender = ConsoleAppender.new(layout,"System.err");
-	root.addAppender(appender);
-	return true
 end
+Logging.init_root()
 
 local function to_luan_logger(log4j_logger)
 	local tbl = {}
@@ -50,11 +48,11 @@
 end
 
 function Logging.logger(name)
-	return to_luan_logger( Logger.getLogger(name) )
+	return to_luan_logger( LuanLogger.getLogger(name) )
 end
 
 function Logging.root_logger()
-	return to_luan_logger( Logger.getRootLogger() )
+	return to_luan_logger( LuanLogger.getRootLogger() )
 end
 
 return Logging
--- a/src/luan/modules/logging/LuanLogger.java	Mon Feb 04 20:18:51 2019 -0700
+++ b/src/luan/modules/logging/LuanLogger.java	Tue Feb 05 22:36:55 2019 -0700
@@ -1,6 +1,11 @@
 package luan.modules.logging;
 
 import org.apache.log4j.Logger;
+import org.apache.log4j.LogManager;
+import org.apache.log4j.Hierarchy;
+import org.apache.log4j.Level;
+import org.apache.log4j.spi.LoggerRepository;
+import org.apache.log4j.spi.RootLogger;
 import luan.Luan;
 import luan.LuanState;
 import luan.LuanException;
@@ -30,4 +35,31 @@
 		logger.debug( luan.toString(obj) );
 	}
 
+
+	private static String KEY = "Logger.Repository";
+
+	public static LoggerRepository getLoggerRepository(LuanState luan) {
+		LoggerRepository lr = (LoggerRepository)luan.registry().get(KEY);
+		return lr != null ? lr : LogManager.getLoggerRepository();
+	}
+
+	public static void setLoggerRepository(LuanState luan,LoggerRepository lr) throws LuanException {
+		luan.registry().put(KEY,lr);
+		LuanTable module = (LuanTable)luan.require("luan:logging/Logging.luan");
+		module.call( "init_root" );
+	}
+
+	public static void newLoggerRepository(LuanState luan) throws LuanException {
+		LoggerRepository lr =  new Hierarchy(new RootLogger(Level.DEBUG));
+		setLoggerRepository(luan,lr);
+	}
+
+	public static Logger getRootLogger(LuanState luan) {
+		return getLoggerRepository(luan).getRootLogger();
+	}
+
+	public static Logger getLogger(LuanState luan,String name) {
+		return getLoggerRepository(luan).getLogger(name);
+	}
+
 }