X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.init%2Fsrc%2Forg%2Fargeo%2Finit%2Flogging%2FThinLogging.java;h=16e792776566a51502a5aa0924f369cecb8adab3;hb=4e37b03e8835e4f15520e5e0d15252f49d09cf3a;hp=9866a1f232e2a218d9c98d8699b089f619ca7d01;hpb=1010bff4f1ffadbf3f7988fbd3eba70f5d672c88;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.init/src/org/argeo/init/logging/ThinLogging.java b/org.argeo.init/src/org/argeo/init/logging/ThinLogging.java index 9866a1f23..16e792776 100644 --- a/org.argeo.init/src/org/argeo/init/logging/ThinLogging.java +++ b/org.argeo.init/src/org/argeo/init/logging/ThinLogging.java @@ -16,16 +16,17 @@ import java.util.ResourceBundle; import java.util.SortedMap; import java.util.StringTokenizer; import java.util.TreeMap; -import java.util.concurrent.Executor; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; import java.util.concurrent.Flow; import java.util.concurrent.Flow.Subscription; +import java.util.concurrent.ForkJoinPool; import java.util.concurrent.SubmissionPublisher; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; import java.util.function.Consumer; +import org.argeo.init.RuntimeContext; +import org.argeo.init.Service; + /** * A thin logging system based on the {@link Logger} framework. It is a * {@link Consumer} of configuration, and can be registered as such. @@ -48,24 +49,33 @@ class ThinLogging implements Consumer> { private NavigableMap levels = new TreeMap<>(); private volatile boolean updatingConfiguration = false; - private final ExecutorService executor; +// private final ExecutorService executor; private final LogEntryPublisher publisher; + private PrintStreamSubscriber synchronousSubscriber; private final boolean journald; private final Level callLocationLevel; - ThinLogging() { - executor = Executors.newCachedThreadPool((r) -> { - Thread t = new Thread(r); - t.setDaemon(true); - return t; - }); - publisher = new LogEntryPublisher(executor, Flow.defaultBufferSize()); + private boolean synchronous = true; - PrintStreamSubscriber subscriber = new PrintStreamSubscriber(); - publisher.subscribe(subscriber); + ThinLogging() { +// executor = Executors.newCachedThreadPool((r) -> { +// Thread t = new Thread(r); +// t.setDaemon(true); +// return t; +// }); + if (synchronous) { + publisher = new LogEntryPublisher(); + synchronousSubscriber = new PrintStreamSubscriber(); + } else { + publisher = new LogEntryPublisher(); + + PrintStreamSubscriber subscriber = new PrintStreamSubscriber(); + publisher.subscribe(subscriber); + + Runtime.getRuntime().addShutdownHook(new Thread(() -> close(), "Log shutdown")); - Runtime.getRuntime().addShutdownHook(new Thread(() -> close(), "Log shutdown")); + } // initial default level levels.put("", Level.WARNING); @@ -115,11 +125,21 @@ class ThinLogging implements Consumer> { } private void close() { + RuntimeContext runtimeContext = Service.getRuntimeContext(); + if (runtimeContext != null) { + try { + runtimeContext.waitForStop(0); + } catch (InterruptedException e) { + // silent + } + } + publisher.close(); try { // we ait a bit in order to make sure all messages are flushed // TODO synchronize more efficiently - executor.awaitTermination(300, TimeUnit.MILLISECONDS); + // executor.awaitTermination(300, TimeUnit.MILLISECONDS); + ForkJoinPool.commonPool().awaitTermination(300, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { // silent } @@ -216,7 +236,7 @@ class ThinLogging implements Consumer> { /* * INTERNAL CLASSES */ - + private class ThinLogger implements System.Logger { private final String name; @@ -274,6 +294,19 @@ class ThinLogging implements Consumer> { // NOTE: this is the method called when logging a plain message without // exception, so it should be considered as a format only when args are not null +// if (format.contains("{}"))// workaround for weird Jetty formatting +// params = null; + // TODO move this to slf4j wrapper? + if (format.contains("{}")) { + StringBuilder sb = new StringBuilder(); + String[] segments = format.split("\\{\\}"); + for (int i = 0; i < segments.length; i++) { + sb.append(segments[i]); + if (i != (segments.length - 1)) + sb.append("{" + i + "}"); + } + format = sb.toString(); + } String msg = params == null ? format : MessageFormat.format(format, params); publisher.log(this, level, bundle, msg, now, thread, (Throwable) null, findCallLocation(level, thread)); } @@ -285,6 +318,7 @@ class ThinLogging implements Consumer> { private StackTraceElement findCallLocation(Level level, Thread thread) { assert level != null; assert thread != null; + // TODO rather use a StackWalker and make it smarter StackTraceElement callLocation = null; if (level.getSeverity() >= callLocationLevel.getSeverity()) { StackTraceElement[] stack = thread.getStackTrace(); @@ -293,13 +327,17 @@ class ThinLogging implements Consumer> { String className = stack[i].getClassName(); switch (className) { // TODO make it more configurable + // FIXME deal with privileges stacks (in Equinox) case "java.lang.System$Logger": case "java.util.logging.Logger": case "org.apache.commons.logging.Log": case "org.osgi.service.log.Logger": - case "org.argeo.cms.Log": + case "org.eclipse.osgi.internal.log.LoggerImpl": + case "org.argeo.api.cms.CmsLog": case "org.slf4j.impl.ArgeoLogger": + case "org.argeo.cms.internal.osgi.CmsOsgiLogger": case "org.eclipse.jetty.util.log.Slf4jLog": + case "sun.util.logging.internal.LoggingProviderImpl$JULWrapper": lowestLoggerInterface = i; continue stack; default: @@ -323,8 +361,8 @@ class ThinLogging implements Consumer> { private class LogEntryPublisher extends SubmissionPublisher> { - private LogEntryPublisher(Executor executor, int maxBufferCapacity) { - super(executor, maxBufferCapacity); + private LogEntryPublisher() { + super(); } private void log(ThinLogger logger, Level level, ResourceBundle bundle, String msg, Instant instant, @@ -353,7 +391,13 @@ class ThinLogging implements Consumer> { logEntry.put(KEY_THREAD, thread.getName()); // should be unmodifiable for security reasons - submit(Collections.unmodifiableMap(logEntry)); + if (synchronous) { + assert synchronousSubscriber != null; + synchronousSubscriber.onNext(logEntry); + } else { + if (!isClosed()) + submit(Collections.unmodifiableMap(logEntry)); + } } } @@ -519,12 +563,13 @@ class ThinLogging implements Consumer> { sb.append('\n'); for (StackTraceElement ste : throwable.getStackTrace()) { sb.append(prefix); + sb.append('\t'); sb.append(ste.toString()); sb.append('\n'); } if (throwable.getCause() != null) { sb.append(prefix); - sb.append("caused by "); + sb.append("Caused by: "); addThrowable(sb, prefix, throwable.getCause()); } } @@ -538,6 +583,16 @@ class ThinLogging implements Consumer> { logger.log(Logger.Level.INFO, "Log info"); logger.log(Logger.Level.WARNING, "Log warning"); logger.log(Logger.Level.ERROR, "Log exception", new Throwable()); + + try { + // we ait a bit in order to make sure all messages are flushed + // TODO synchronize more efficiently + // executor.awaitTermination(300, TimeUnit.MILLISECONDS); + ForkJoinPool.commonPool().awaitTermination(300, TimeUnit.MILLISECONDS); + } catch (InterruptedException e) { + // silent + } + } }