diff src/luan/modules/lucene/LuceneIndex.java @ 1393:cc0dbca576dc

better logging
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 06 Sep 2019 05:09:56 -0600
parents 002152af497a
children 9dfff82dfc59
line wrap: on
line diff
--- 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);