X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.init%2Fsrc%2Forg%2Fargeo%2Finit%2Flogging%2FThinLoggerFinder.java;h=f443b2e669d28aa9599716e126adb24397db6f66;hb=1010bff4f1ffadbf3f7988fbd3eba70f5d672c88;hp=7849309460933695f5b60a96d44f5e8cf2592c4c;hpb=b7d8618ce593bbeca7e311d32a4d98988e27f877;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.init/src/org/argeo/init/logging/ThinLoggerFinder.java b/org.argeo.init/src/org/argeo/init/logging/ThinLoggerFinder.java index 784930946..f443b2e66 100644 --- a/org.argeo.init/src/org/argeo/init/logging/ThinLoggerFinder.java +++ b/org.argeo.init/src/org/argeo/init/logging/ThinLoggerFinder.java @@ -1,14 +1,27 @@ package org.argeo.init.logging; +import java.io.Serializable; import java.lang.System.Logger; import java.lang.System.LoggerFinder; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.concurrent.Flow; +import java.util.function.Consumer; -/** Factory for Java system logging. */ +/** + * Factory for Java system logging. As it has to be a public class in order to + * be exposed as a service provider, it is also the main entry point for the + * thin logging system, via static methos. + */ public class ThinLoggerFinder extends LoggerFinder { - private ThinLogging logging; + private static ThinLogging logging; + private static ThinJavaUtilLogging javaUtilLogging; public ThinLoggerFinder() { - logging = new ThinLogging(); + if (logging != null) + throw new IllegalStateException("Only one logging can be initialised."); + init(); } @Override @@ -16,4 +29,53 @@ public class ThinLoggerFinder extends LoggerFinder { return logging.getLogger(name, module); } + private static void init() { + logging = new ThinLogging(); + + Map configuration = new HashMap<>(); + for (Object key : System.getProperties().keySet()) { + Objects.requireNonNull(key); + String property = key.toString(); + if (property.startsWith(ThinLogging.LEVEL_PROPERTY_PREFIX) + || property.equals(ThinLogging.DEFAULT_LEVEL_PROPERTY)) + configuration.put(property, System.getProperty(property)); + } + logging.accept(configuration); + } + + /** + * Falls back to java.util.logging if thin logging was not already initialised + * by the {@link LoggerFinder} mechanism. + */ + public static void lazyInit() { + if (logging != null) + return; + if (javaUtilLogging != null) + return; + init(); + javaUtilLogging = ThinJavaUtilLogging.init(); + javaUtilLogging.readConfiguration(logging.getLevels()); + } + + public static Consumer> getConfigurationConsumer() { + Objects.requireNonNull(logging); + return logging; + } + + public static Flow.Publisher> getLogEntryPublisher() { + Objects.requireNonNull(logging); + return logging.getLogEntryPublisher(); + } + + static void update(Map configuration) { + if (logging == null) + throw new IllegalStateException("Thin logging must be initialized first"); + logging.accept(configuration); + if (javaUtilLogging != null) + javaUtilLogging.readConfiguration(logging.getLevels()); + } + + static Logger getLogger(String name) { + return logging.getLogger(name, null); + } }