changeset 1332:11b7e11f9ed5

cleaner logging
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 12 Feb 2019 21:50:26 -0700
parents 02fe660e7748
children 25746915a241
files conv.txt src/luan/host/WebHandler.java src/luan/host/init.luan src/luan/modules/http/LuanDomainHandler.java src/luan/modules/http/LuanHandler.java src/luan/modules/logging/Log4j.java src/luan/modules/logging/Log4j.luan src/luan/modules/logging/Logging.luan src/luan/modules/logging/LuanLogger.java
diffstat 9 files changed, 116 insertions(+), 87 deletions(-) [+]
line wrap: on
line diff
diff -r 02fe660e7748 -r 11b7e11f9ed5 conv.txt
--- a/conv.txt	Mon Feb 11 02:17:41 2019 -0700
+++ b/conv.txt	Tue Feb 12 21:50:26 2019 -0700
@@ -1,3 +1,5 @@
+LuanLogger
+
 file - java.*
 Io.unrestricted
 Thread.global_callable
diff -r 02fe660e7748 -r 11b7e11f9ed5 src/luan/host/WebHandler.java
--- a/src/luan/host/WebHandler.java	Mon Feb 11 02:17:41 2019 -0700
+++ b/src/luan/host/WebHandler.java	Tue Feb 12 21:50:26 2019 -0700
@@ -15,7 +15,7 @@
 import luan.LuanClosure;
 import luan.modules.BasicLuan;
 import luan.modules.http.LuanHandler;
-import luan.modules.logging.LuanLogger;
+import luan.modules.logging.Log4j;
 
 
 public class WebHandler implements Handler {
@@ -41,7 +41,7 @@
 
 			LuanState luan = new LuanState();
 			try {
-				LuanLogger.newLoggerRepository(luan);
+				Log4j.newLoggerRepository(luan);
 			} catch(LuanException e) {
 				throw new RuntimeException(e);
 			}
diff -r 02fe660e7748 -r 11b7e11f9ed5 src/luan/host/init.luan
--- a/src/luan/host/init.luan	Mon Feb 11 02:17:41 2019 -0700
+++ b/src/luan/host/init.luan	Tue Feb 12 21:50:26 2019 -0700
@@ -12,12 +12,12 @@
 
 if logging then
 	java()
-	local LuanLogger = require "java:luan.modules.logging.LuanLogger"
+	local Log4j = require "java:luan.modules.logging.Log4j"
 	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()
+	local logger = Log4j.getRootLogger()
 	logger.removeAllAppenders()
 	local layout = EnhancedPatternLayout.new("%d %-5p %c - %m%n")
 	local log_dir = dir.."/site/private/local/logs/"
diff -r 02fe660e7748 -r 11b7e11f9ed5 src/luan/modules/http/LuanDomainHandler.java
--- a/src/luan/modules/http/LuanDomainHandler.java	Mon Feb 11 02:17:41 2019 -0700
+++ b/src/luan/modules/http/LuanDomainHandler.java	Tue Feb 12 21:50:26 2019 -0700
@@ -8,7 +8,7 @@
 import luan.LuanTable;
 import luan.LuanCloner;
 import luan.LuanException;
-import luan.modules.logging.LuanLogger;
+import luan.modules.logging.Log4j;
 
 
 public class LuanDomainHandler implements Handler, DomainHandler.Factory {
@@ -26,11 +26,15 @@
 		return new LuanHandler(luan);
 	}
 
+	protected void newLoggerRepository(LuanState luan) throws LuanException {
+		Log4j.newLoggerRepository(luan);
+	}
+
 	protected LuanState newLuan(String domain) {
 		LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE);
 		LuanState luan = (LuanState)cloner.clone(luanInit);
 		try {
-			LuanLogger.newLoggerRepository(luan);
+			newLoggerRepository(luan);
 			LuanTable Http = (LuanTable)luan.require("luan:http/Http.luan");
 			Http.put( "domain", domain );
 		} catch(LuanException e) {
diff -r 02fe660e7748 -r 11b7e11f9ed5 src/luan/modules/http/LuanHandler.java
--- a/src/luan/modules/http/LuanHandler.java	Mon Feb 11 02:17:41 2019 -0700
+++ b/src/luan/modules/http/LuanHandler.java	Tue Feb 12 21:50:26 2019 -0700
@@ -12,7 +12,7 @@
 import java.util.ArrayList;
 import java.util.concurrent.locks.ReadWriteLock;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
-import org.apache.log4j.Logger;
+import org.slf4j.Logger;
 import luan.webserver.Request;
 import luan.webserver.Response;
 import luan.webserver.Status;
diff -r 02fe660e7748 -r 11b7e11f9ed5 src/luan/modules/logging/Log4j.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/luan/modules/logging/Log4j.java	Tue Feb 12 21:50:26 2019 -0700
@@ -0,0 +1,48 @@
+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 org.apache.log4j.spi.RepositorySelector;
+import luan.LuanState;
+import luan.LuanException;
+import luan.LuanTable;
+
+
+public final class Log4j {
+	private static String KEY = "Logger.Repository";
+
+	public static void newLoggerRepository(LuanState luan) throws LuanException {
+		LoggerRepository lr =  new Hierarchy(new RootLogger(Level.DEBUG));
+		luan.registry().put(KEY,lr);
+		LuanTable module = (LuanTable)luan.require("luan:logging/Log4j.luan");
+		module.call( "init_root" );
+	}
+
+	private static final LoggerRepository defaultLoggerRepository = LogManager.getLoggerRepository();
+
+	private static LoggerRepository getLoggerRepository(LuanState luan) {
+		LoggerRepository lr = (LoggerRepository)luan.registry().get(KEY);
+		return lr != null ? lr : defaultLoggerRepository;
+	}
+
+	static {
+		LogManager.setRepositorySelector( new RepositorySelector() {
+			public LoggerRepository getLoggerRepository() {
+				LuanState luan = LuanLogger.luan();
+				return luan==null ? defaultLoggerRepository : Log4j.getLoggerRepository(luan);
+			}
+		}, new Object() );
+	}
+
+	public static Logger getRootLogger(LuanState luan) {
+		return getLoggerRepository(luan).getRootLogger();
+	}
+
+	public static Logger getLogger(LuanState luan,String name) {
+		return getLoggerRepository(luan).getLogger(name);
+	}
+}
diff -r 02fe660e7748 -r 11b7e11f9ed5 src/luan/modules/logging/Log4j.luan
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/luan/modules/logging/Log4j.luan	Tue Feb 12 21:50:26 2019 -0700
@@ -0,0 +1,24 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+java()
+local JavaLog4j = require "java:luan.modules.logging.Log4j"
+local EnhancedPatternLayout = require "java:org.apache.log4j.EnhancedPatternLayout"
+local ConsoleAppender = require "java:org.apache.log4j.ConsoleAppender"
+local Level = require "java:org.apache.log4j.Level"
+
+
+local Log4j = {}
+
+function Log4j.init_root()
+	local root = JavaLog4j.getRootLogger()
+	local has_appenders = root.getAllAppenders().hasMoreElements()
+	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
+end
+Log4j.init_root()
+
+return Log4j
diff -r 02fe660e7748 -r 11b7e11f9ed5 src/luan/modules/logging/Logging.luan
--- a/src/luan/modules/logging/Logging.luan	Mon Feb 11 02:17:41 2019 -0700
+++ b/src/luan/modules/logging/Logging.luan	Tue Feb 12 21:50:26 2019 -0700
@@ -1,58 +1,22 @@
-local Luan = require "luan:Luan.luan"
-local error = Luan.error
 java()
-local EnhancedPatternLayout = require "java:org.apache.log4j.EnhancedPatternLayout"
-local ConsoleAppender = require "java:org.apache.log4j.ConsoleAppender"
-local Level = require "java:org.apache.log4j.Level"
 local LuanLogger = require "java:luan.modules.logging.LuanLogger"
 
+local Package = require "luan:Package.luan"
+if Package.load("java:org.apache.log4j.Logger") ~= false then
+	require "luan:logging/Log4j.luan"
+end
+
 
 local Logging = {}
 
-function Logging.init_root()
-	local root = LuanLogger.getRootLogger()
-	local has_appenders = root.getAllAppenders().hasMoreElements()
-	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
-end
-Logging.init_root()
-
-local function to_luan_logger(log4j_logger)
-	local tbl = {}
-
-	local luanLogger = LuanLogger.new(log4j_logger)
-	tbl.error = luanLogger.error
-	tbl.warn = luanLogger.warn
-	tbl.info = luanLogger.info
-	tbl.debug = luanLogger.debug
-
-	function tbl.get_level()
-		local level = log4j_logger.getLevel()
-		return level and level.toString()
-	end
-
-	function tbl.get_effective_level()
-		local level = log4j_logger.getEffectiveLevel()
-		return level and level.toString()
-	end
-
-	function tbl.set_level(level)
-		log4j_logger.setLevel( Level.toLevel(level) )
-	end
-
-	return tbl
-end
-
 function Logging.logger(name)
-	return to_luan_logger( LuanLogger.getLogger(name) )
-end
-
-function Logging.root_logger()
-	return to_luan_logger( LuanLogger.getRootLogger() )
+	local luan_logger = LuanLogger.new(name)
+	return {
+		error = luan_logger.error
+		warn = luan_logger.warn
+		info = luan_logger.info
+		debug = luan_logger.debug
+	}
 end
 
 return Logging
diff -r 02fe660e7748 -r 11b7e11f9ed5 src/luan/modules/logging/LuanLogger.java
--- a/src/luan/modules/logging/LuanLogger.java	Mon Feb 11 02:17:41 2019 -0700
+++ b/src/luan/modules/logging/LuanLogger.java	Tue Feb 12 21:50:26 2019 -0700
@@ -1,22 +1,16 @@
 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 org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import luan.LuanState;
 import luan.LuanException;
-import luan.LuanTable;
 
 
 public final class LuanLogger {
 	private final Logger logger;
 
-	public LuanLogger(Logger logger) {
-		this.logger = logger;
+	public LuanLogger(LuanState luan,String name) {
+		this.logger = getLogger(luan,name);
 	}
 
 	public void error(LuanState luan,Object obj) throws LuanException {
@@ -36,30 +30,23 @@
 	}
 
 
-	private static String KEY = "Logger.Repository";
+	private static ThreadLocal<LuanState> tl = new ThreadLocal<LuanState>();
 
-	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 Logger getLogger(LuanState luan,String name) {
+		try {
+			luan.require("luan:logging/Logging.luan");  // ensure initialization
+		} catch(LuanException e) {
+			throw new RuntimeException(e);
+		}
+		tl.set(luan);
+		try {
+			return LoggerFactory.getLogger(name);
+		} finally {
+			tl.remove();
+		}
 	}
 
-	public static void newLoggerRepository(LuanState luan) throws LuanException {
-		LoggerRepository lr =  new Hierarchy(new RootLogger(Level.DEBUG));
-		setLoggerRepository(luan,lr);
+	public static LuanState luan() {
+		return tl.get();
 	}
-
-	public static Logger getRootLogger(LuanState luan) {
-		return getLoggerRepository(luan).getRootLogger();
-	}
-
-	public static Logger getLogger(LuanState luan,String name) {
-		return getLoggerRepository(luan).getLogger(name);
-	}
-
 }