changeset 1393:cc0dbca576dc

better logging
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 06 Sep 2019 05:09:56 -0600
parents 002152af497a
children 8fe777ba5045
files examples/blog/src/lib/Db.luan src/luan/Luan.java src/luan/host/init.luan src/luan/modules/logging/LuanLogger.java src/luan/modules/lucene/LuceneIndex.java src/luan/modules/lucene/PostgresBackup.java
diffstat 6 files changed, 42 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/examples/blog/src/lib/Db.luan	Fri Sep 06 00:19:47 2019 -0600
+++ b/examples/blog/src/lib/Db.luan	Fri Sep 06 05:09:56 2019 -0600
@@ -8,7 +8,6 @@
 local Db = {}
 
 local postgres_spec = Hosting.postgres_spec and Hosting.postgres_spec()
-logger.info("postgres_spec="..postgres_spec)
 if postgres_spec ~= nil then
 	function postgres_spec.completer(doc)
 		return doc
--- a/src/luan/Luan.java	Fri Sep 06 00:19:47 2019 -0600
+++ b/src/luan/Luan.java	Fri Sep 06 05:09:56 2019 -0600
@@ -15,6 +15,7 @@
 import luan.modules.BasicLuan;
 import luan.modules.JavaLuan;
 import luan.modules.PackageLuan;
+import luan.modules.logging.LuanLogger;
 import luan.impl.LuanCompiler;
 
 
@@ -172,6 +173,10 @@
 		return null;
 	}
 
+	public Logger getLogger(Class cls) {
+		return LuanLogger.getLogger(this,cls);
+	}
+
 
 
 	// static
--- a/src/luan/host/init.luan	Fri Sep 06 00:19:47 2019 -0600
+++ b/src/luan/host/init.luan	Fri Sep 06 05:09:56 2019 -0600
@@ -78,7 +78,6 @@
 local pg = fn()
 
 function Hosting.postgres_spec()
-	logger.info("pg="..pg.." domain="..domain)
 	if pg == nil then
 		return nil
 	end
--- a/src/luan/modules/logging/LuanLogger.java	Fri Sep 06 00:19:47 2019 -0600
+++ b/src/luan/modules/logging/LuanLogger.java	Fri Sep 06 05:09:56 2019 -0600
@@ -32,12 +32,16 @@
 
 	private static ThreadLocal<Luan> tl = new ThreadLocal<Luan>();
 
-	public static Logger getLogger(Luan luan,String name) {
+	private static void init(Luan luan) {
 		try {
 			luan.require("luan:logging/Logging.luan");  // ensure initialization
 		} catch(LuanException e) {
 			throw new RuntimeException(e);
 		}
+	}
+
+	public static Logger getLogger(Luan luan,String name) {
+		init(luan);
 		tl.set(luan);
 		try {
 			return LoggerFactory.getLogger(name);
@@ -47,6 +51,7 @@
 	}
 
 	public static Logger getLogger(Luan luan,Class cls) {
+		init(luan);
 		tl.set(luan);
 		try {
 			return LoggerFactory.getLogger(cls);
--- a/src/luan/modules/lucene/LuceneIndex.java	Fri Sep 06 00:19:47 2019 -0600
+++ b/src/luan/modules/lucene/LuceneIndex.java	Fri Sep 06 05:09:56 2019 -0600
@@ -79,13 +79,12 @@
 import luan.LuanException;
 import luan.LuanRuntimeException;
 import luan.modules.parsers.LuanToString;
-import luan.modules.logging.LuanLogger;
 import luan.lib.logging.Logger;
 import luan.lib.logging.LoggerFactory;
 
 
 public final class LuceneIndex {
-	private static final Logger logger = LoggerFactory.getLogger(LuceneIndex.class);
+	private static final Logger sysLogger = LoggerFactory.getLogger(LuceneIndex.class);
 
 	private static final class Closer implements Closeable {
 		final LuceneIndex li;
@@ -110,7 +109,7 @@
 
 		protected void finalize() throws Throwable {
 			if( !isClosed ) {
-				logger.error("not closed",created);
+				sysLogger.error("not closed",created);
 				close();
 			}
 			super.finalize();
@@ -145,6 +144,8 @@
 	public static final StringFieldParser STRING_FIELD_PARSER = new StringFieldParser(new KeywordAnalyzer());
 	public static final StringFieldParser ENGLISH_FIELD_PARSER = new StringFieldParser(new EnglishAnalyzer(version));
 
+	private final Logger luanLogger;
+
 	private final ReentrantLock writeLock = new ReentrantLock();
 	private final File indexDir;
 	private SnapshotDeletionPolicy snapshotDeletionPolicy;
@@ -171,7 +172,7 @@
 	private LuceneIndex(Luan luan,File indexDir,FieldParser defaultFieldParser,String[] defaultFields,String key,LuanTable postgresSpec)
 		throws LuanException, IOException, ClassNotFoundException, SQLException
 	{
-		final Logger logger = LuanLogger.getLogger(luan,LuceneIndex.class);
+		this.luanLogger = luan.getLogger(LuceneIndex.class);
 		this.key = key;
 		this.defaultFieldParser = defaultFieldParser;
 		this.defaultFields = defaultFields;
@@ -191,14 +192,14 @@
 		} else {
 			Map spec = postgresSpec.asMap();
 			LuanFunction completer = Utils.removeRequiredFunction(spec,"completer");
-			postgresBackup = new PostgresBackup(spec);
+			postgresBackup = new PostgresBackup(luan,spec);
 			if( postgresBackup != null ) {
 				if( !wasCreated && postgresBackup.wasCreated ) {
-					logger.error("rebuilding postgres backup");
+					luanLogger.error("rebuilding postgres backup");
 					rebuild_postgres_backup(completer);
 				} else if( wasCreated && !postgresBackup.wasCreated ) {
-					logger.error("restoring from postgres");
-					restore_from_postgres(luan);
+					luanLogger.error("restoring from postgres");
+					restore_from_postgres();
 				}
 			}
 		}
@@ -822,8 +823,7 @@
 	public void rebuild_postgres_backup(LuanFunction completer)
 		throws IOException, LuanException, SQLException
 	{
-		final Logger logger = LuanLogger.getLogger(completer.luan(),LuceneIndex.class);
-		logger.info("start rebuild_postgres_backup");
+		luanLogger.info("start rebuild_postgres_backup");
 		writeLock.lock();
 		IndexSearcher searcher = openSearcher();
 		boolean ok = false;
@@ -858,14 +858,13 @@
 				postgresBackup.rollback();
 			writeLock.unlock();
 		}
-		logger.info("end rebuild_postgres_backup");
+		luanLogger.info("end rebuild_postgres_backup");
 	}
 
-	public void restore_from_postgres(Luan luan)
+	public void restore_from_postgres()
 		throws IOException, LuanException, SQLException
 	{
-		final Logger logger = LuanLogger.getLogger(luan,LuceneIndex.class);
-		logger.warn("start restore_from_postgres");
+		luanLogger.warn("start restore_from_postgres");
 		if( postgresBackup==null )
 			throw new NullPointerException();
 		if( writeLock.isHeldByCurrentThread() )
@@ -887,7 +886,7 @@
 			wrote();
 			writeLock.unlock();
 		}
-		logger.warn("end restore_from_postgres");
+		luanLogger.warn("end restore_from_postgres");
 	}
 
 	void restore(LuanTable doc)
@@ -897,18 +896,16 @@
 	}
 
 	public void check(LuanFunction completer) throws IOException, SQLException, LuanException {
-		final Logger logger = LuanLogger.getLogger(completer.luan(),LuceneIndex.class);
-		logger.info("start check");
+		luanLogger.info("start check");
 		CheckIndex.Status status = new CheckIndex(fsDir).checkIndex();
 		if( !status.clean )
-			logger.error("index not clean");
+			luanLogger.error("index not clean");
 		if( postgresBackup != null )
 			checkPostgres(completer);
-		logger.info("end check");
+		luanLogger.info("end check");
 	}
 
 	private void checkPostgres(LuanFunction completer) throws IOException, SQLException, LuanException {
-		final Logger logger = LuanLogger.getLogger(completer.luan(),LuceneIndex.class);
 		final PostgresBackup.Checker postgresChecker;
 		final IndexSearcher searcher;
 		writeLock.lock();
@@ -943,10 +940,10 @@
 				long idPostgres = idsPostgres.get(iPostgres);
 				if( idLucene < idPostgres ) {
 					iLucene++;
-					logger.error("id "+idLucene+" found in lucene but not postgres");
+					luanLogger.error("id "+idLucene+" found in lucene but not postgres");
 				} else if( idLucene > idPostgres ) {
 					iPostgres++;
-					logger.error("id "+idPostgres+" found in postgres but not lucene");
+					luanLogger.error("id "+idPostgres+" found in postgres but not lucene");
 				} else {  // ==
 					LuanTable docPostgres = postgresChecker.getDoc(idPostgres);
 					TopDocs td = searcher.search(new TermQuery(term("id",idLucene)),1);
@@ -955,9 +952,9 @@
 					LuanTable docLucene = toTable(completer.luan(),doc);
 					docLucene = (LuanTable)completer.call(docLucene);
 					if( !equal(docPostgres,docLucene) ) {
-						logger.error("id "+idLucene+" not equal");
-						logger.error("lucene = "+lts.toString(docLucene));
-						logger.error("postgres = "+lts.toString(docPostgres));
+						luanLogger.error("id "+idLucene+" not equal");
+						luanLogger.error("lucene = "+lts.toString(docLucene));
+						luanLogger.error("postgres = "+lts.toString(docPostgres));
 					}
 					iLucene++;
 					iPostgres++;
@@ -965,11 +962,11 @@
 			}
 			while( iLucene < nLucene ) {
 				long idLucene = idsLucene.get(iLucene++);
-				logger.error("id "+idLucene+" found in lucene but not postgres");
+				luanLogger.error("id "+idLucene+" found in lucene but not postgres");
 			}
 			while( iPostgres < nPostgres ) {
 				long idPostgres = idsPostgres.get(iPostgres++);
-				logger.error("id "+idPostgres+" found in postgres but not lucene");
+				luanLogger.error("id "+idPostgres+" found in postgres but not lucene");
 			}
 		} finally {
 			close(searcher);
--- a/src/luan/modules/lucene/PostgresBackup.java	Fri Sep 06 00:19:47 2019 -0600
+++ b/src/luan/modules/lucene/PostgresBackup.java	Fri Sep 06 05:09:56 2019 -0600
@@ -17,13 +17,14 @@
 import luan.LuanException;
 import luan.modules.Utils;
 import luan.modules.parsers.LuanToString;
-import luan.modules.logging.LuanLogger;
 import luan.lib.logging.Logger;
 import luan.lib.logging.LoggerFactory;
 
 
 final class PostgresBackup {
-	private static final Logger logger = LoggerFactory.getLogger(PostgresBackup.class);
+	private static final Logger sysLogger = LoggerFactory.getLogger(PostgresBackup.class);
+
+	private final Logger luanLogger;
 
 	final boolean wasCreated;
 	private final String url;
@@ -35,9 +36,10 @@
 	private int trans = 0;
 	private final LuanToString luanToString = new LuanToString();
 
-	PostgresBackup(Map spec)
+	PostgresBackup(Luan luan,Map spec)
 		throws ClassNotFoundException, SQLException, LuanException
 	{
+		this.luanLogger = luan.getLogger(PostgresBackup.class);
 /*
 		Class.forName("org.postgresql.Driver");
 		url = "jdbc:postgresql://localhost:5432/luan";
@@ -98,7 +100,7 @@
 	protected void finalize() throws Throwable {
 		super.finalize();
 		if( !con.isClosed() ) {
-			logger.error("con not closed");
+			sysLogger.error("con not closed");
 			con.close();
 		}
 	}
@@ -118,8 +120,7 @@
 		updateStmt.setLong(2,id);
 		int n = updateStmt.executeUpdate();
 		if( n==0 ) {
-			Logger logger = LuanLogger.getLogger(doc.luan(),PostgresBackup.class);
-			logger.error("update not found for id="+id+", trying add");
+			luanLogger.error("update not found for id="+id+", trying add");
 			add(doc);
 		} else if( n!=1 )
 			throw new RuntimeException();