diff src/luan/modules/logging/LuanLogger.java @ 1332:11b7e11f9ed5

cleaner logging
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 12 Feb 2019 21:50:26 -0700
parents 307e76ccd0d6
children 25746915a241
line wrap: on
line diff
--- 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);
-	}
-
 }