From ec10ed45bde7a361e7340e01bbf302d7b4ce67a7 Mon Sep 17 00:00:00 2001 From: Michael Peter Christen Date: Thu, 16 Jan 2014 13:08:39 +0100 Subject: [PATCH] better logging in logger --- source/net/yacy/cora/util/ConcurrentLog.java | 81 ++++++++++--------- .../yacy/kelondro/index/RowCollection.java | 9 ++- 2 files changed, 48 insertions(+), 42 deletions(-) diff --git a/source/net/yacy/cora/util/ConcurrentLog.java b/source/net/yacy/cora/util/ConcurrentLog.java index 69f947e55..dae312cfa 100644 --- a/source/net/yacy/cora/util/ConcurrentLog.java +++ b/source/net/yacy/cora/util/ConcurrentLog.java @@ -46,15 +46,23 @@ import java.util.logging.Logger; */ public final class ConcurrentLog { - + private final static Logger ConcurrentLogLogger = Logger.getLogger("ConcurrentLog"); private final static Message POISON_MESSAGE = new Message(); - private final static BlockingQueue logQueue = new ArrayBlockingQueue(300); - private final static Worker logRunnerThread = new Worker(); + private final static BlockingQueue logQueue = new ArrayBlockingQueue(500); + private static Worker logRunnerThread = null; static { - logRunnerThread.start(); + ensureWorkerIsRunning(); } + public static void ensureWorkerIsRunning() { + if (logRunnerThread == null || !logRunnerThread.isAlive()) { + logRunnerThread = new Worker(); + logRunnerThread.start(); + ConcurrentLogLogger.log(Level.INFO, "started ConcurrentLog.Worker."); + } + } + private final Logger theLogger; public ConcurrentLog(final String appName) { @@ -165,12 +173,18 @@ public final class ConcurrentLog { public final boolean isLoggable(final Level level) { return this.theLogger.isLoggable(level); } - - + + /* + public final void logException(final Throwable thrown) { + if (thrown == null) return; + enQueueLog(this.theLogger, Level.WARNING, thrown.getMessage(), thrown); + } + */ + // static log messages public final static void logException(final Throwable thrown) { if (thrown == null) return; - enQueueLog("StackTrace", Level.WARNING, thrown.getMessage(), thrown); + enQueueLog("ConcurrentLog", Level.WARNING, thrown.getMessage(), thrown); } public final static void severe(final String appName, final String message) { enQueueLog(appName, Level.SEVERE, message); @@ -234,10 +248,11 @@ public final class ConcurrentLog { return Logger.getLogger(appName).isLoggable(Level.FINEST); } + // private private final static void enQueueLog(final Logger logger, final Level level, final String message, final Throwable thrown) { if (!logger.isLoggable(level)) return; if (logRunnerThread == null || !logRunnerThread.isAlive()) { - if (thrown == null) logger.log(level, message); else logger.log(level, message, thrown); + if (thrown == null) logger.log(level, "* " + message); else logger.log(level, "* " + message, thrown); // the * is inefficient, but should show up only in emergency cases } else { try { if (thrown == null) logQueue.put(new Message(logger, level, message)); else logQueue.put(new Message(logger, level, message, thrown)); @@ -250,7 +265,7 @@ public final class ConcurrentLog { private final static void enQueueLog(final Logger logger, final Level level, final String message) { if (!logger.isLoggable(level)) return; if (logRunnerThread == null || !logRunnerThread.isAlive()) { - logger.log(level, message); + logger.log(level, "* " + message); // the * is inefficient, but should show up only in emergency cases } else { try { logQueue.put(new Message(logger, level, message)); @@ -262,7 +277,7 @@ public final class ConcurrentLog { private final static void enQueueLog(final String loggername, final Level level, final String message, final Throwable thrown) { if (logRunnerThread == null || !logRunnerThread.isAlive()) { - if (thrown == null) Logger.getLogger(loggername).log(level, message); else Logger.getLogger(loggername).log(level, message, thrown); + if (thrown == null) Logger.getLogger(loggername).log(level, "* " + message); else Logger.getLogger(loggername).log(level, "* " + message, thrown); // the * is inefficient, but should show up only in emergency cases } else { try { if (thrown == null) logQueue.put(new Message(loggername, level, message)); else logQueue.put(new Message(loggername, level, message, thrown)); @@ -274,7 +289,7 @@ public final class ConcurrentLog { private final static void enQueueLog(final String loggername, final Level level, final String message) { if (logRunnerThread == null || !logRunnerThread.isAlive()) { - Logger.getLogger(loggername).log(level, message); + Logger.getLogger(loggername).log(level, "* " + message); // the * is inefficient, but should show up only in emergency cases } else { try { logQueue.put(new Message(loggername, level, message)); @@ -336,14 +351,10 @@ public final class ConcurrentLog { public void run() { Message entry; Map loggerCache = new HashMap(); - //Map loggerCounter = new HashMap(); try { while ((entry = logQueue.take()) != POISON_MESSAGE) { if (entry.logger == null) { assert entry.loggername != null; - //AtomicInteger i = loggerCounter.get(entry.loggername); - //if (i == null) {i = new AtomicInteger(0); loggerCounter.put(entry.loggername, i);} - //i.incrementAndGet(); Logger l = loggerCache.get(entry.loggername); if (l == null) {l = Logger.getLogger(entry.loggername); loggerCache.put(entry.loggername, l);} if (entry.thrown == null) { @@ -360,10 +371,10 @@ public final class ConcurrentLog { } } } - } catch (final InterruptedException e) { - ConcurrentLog.logException(e); + } catch (final Throwable e) { + ConcurrentLogLogger.log(Level.SEVERE, "ConcurrentLog.Worker has terminated", e); } - //Logger.getLogger("Log").log(Level.INFO, "closing logRunner with cached loggers: " + loggerCounter.entrySet().toString()); + ConcurrentLogLogger.log(Level.INFO, "terminating ConcurrentLog.Worker with " + logQueue.size() + " cached loglines."); } } @@ -408,9 +419,11 @@ public final class ConcurrentLog { e.printStackTrace(ps); ps.close(); exceptionLog.severe(msg + "\n" + baos.toString(), e); - ConcurrentLog.logException(e); - ConcurrentLog.logException(e.getCause()); - if (e instanceof InvocationTargetException) ConcurrentLog.logException(((InvocationTargetException) e).getTargetException()); + ConcurrentLogLogger.log(Level.SEVERE, e.getMessage(), e); + if (e instanceof InvocationTargetException) { + Throwable target = ((InvocationTargetException) e).getTargetException(); + ConcurrentLogLogger.log(Level.SEVERE, target.getMessage(), target); + } } }); } finally { @@ -419,31 +432,19 @@ public final class ConcurrentLog { } public final static void shutdown() { - if (logRunnerThread == null || !logRunnerThread.isAlive()) return; + if (logRunnerThread == null || !logRunnerThread.isAlive()) { + ConcurrentLogLogger.log(Level.INFO, "shutdown of ConcurrentLog.Worker void because it was not running."); + return; + } try { + ConcurrentLogLogger.log(Level.INFO, "shutdown of ConcurrentLog.Worker: injection of poison message"); logQueue.put(POISON_MESSAGE); - logRunnerThread.join(1000); + logRunnerThread.join(2000); + ConcurrentLogLogger.log(Level.INFO, "shutdown of ConcurrentLog.Worker: terminated"); } catch (final InterruptedException e) { } } - public final static String format(final String s, int n, final int fillChar) { - final int l = s.length(); - if (l >= n) return s; - final StringBuilder sb = new StringBuilder(l + n); - for (final int i = l + n; i > n; n--) sb.insert(0, fillChar); - return sb.toString(); - } - - public final static boolean allZero(final byte[] a) { - return allZero(a, 0, a.length); - } - - public final static boolean allZero(final byte[] a, final int astart, final int alength) { - for (int i = 0; i < alength; i++) if (a[astart + i] != 0) return false; - return true; - } - public static String stackTrace() { Throwable t = new Throwable(); StackTraceElement[] e = t.getStackTrace(); diff --git a/source/net/yacy/kelondro/index/RowCollection.java b/source/net/yacy/kelondro/index/RowCollection.java index 442d47c2e..66f8ca737 100644 --- a/source/net/yacy/kelondro/index/RowCollection.java +++ b/source/net/yacy/kelondro/index/RowCollection.java @@ -400,7 +400,7 @@ public class RowCollection implements Sortable, Iterable, private final void addUnique(final byte[] a, final int astart, final int alength) throws SpaceExceededException { assert (a != null); assert (astart >= 0) && (astart < a.length) : " astart = " + astart; - assert (!(ConcurrentLog.allZero(a, astart, alength))) : "a = " + NaturalOrder.arrayList(a, astart, alength); + assert (!(allZero(a, astart, alength))) : "a = " + NaturalOrder.arrayList(a, astart, alength); assert (alength > 0); assert (astart + alength <= a.length); assert alength == this.rowdef.objectsize : "alength =" + alength + ", rowdef.objectsize = " + this.rowdef.objectsize; @@ -424,7 +424,7 @@ public class RowCollection implements Sortable, Iterable, protected final void addSorted(final byte[] a, final int astart, final int alength) throws SpaceExceededException { assert (a != null); assert (astart >= 0) && (astart < a.length) : " astart = " + astart; - assert (!(ConcurrentLog.allZero(a, astart, alength))) : "a = " + NaturalOrder.arrayList(a, astart, alength); + assert (!(allZero(a, astart, alength))) : "a = " + NaturalOrder.arrayList(a, astart, alength); assert (alength > 0); assert (astart + alength <= a.length); assert alength == this.rowdef.objectsize : "alength =" + alength + ", rowdef.objectsize = " + this.rowdef.objectsize; @@ -436,6 +436,11 @@ public class RowCollection implements Sortable, Iterable, this.lastTimeWrote = System.currentTimeMillis(); } + private final static boolean allZero(final byte[] a, final int astart, final int alength) { + for (int i = 0; i < alength; i++) if (a[astart + i] != 0) return false; + return true; + } + public synchronized final void addAllUnique(final RowCollection c) throws SpaceExceededException { if (c == null) return; assert(this.rowdef.objectsize == c.rowdef.objectsize);