X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.init%2Fsrc%2Forg%2Fargeo%2Finit%2Flogging%2FThinLoggerFinder.java;h=3fa2bc868848baa2ba1528cdd189e4f958a137c6;hb=faeeb8f0ba92b640942d88782ca6f5cfc332597e;hp=9607c9478df78c3f5f259111a4ed7aab46ef4cee;hpb=01b972e4abce933b3ce06bd18e933b08d3b35464;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 9607c9478..3fa2bc868 100644 --- a/org.argeo.init/src/org/argeo/init/logging/ThinLoggerFinder.java +++ b/org.argeo.init/src/org/argeo/init/logging/ThinLoggerFinder.java @@ -1,34 +1,112 @@ 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.concurrent.Flow.Publisher; +import java.util.function.Consumer; +import java.util.function.Supplier; -/** Factory for Java system logging. */ -public class ThinLoggerFinder extends LoggerFinder { +/** + * 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 + implements Consumer>, Supplier>> { private static ThinLogging logging; + private static ThinJavaUtilLogging javaUtilLogging; public ThinLoggerFinder() { if (logging != null) throw new IllegalStateException("Only one logging can be initialised."); - logging = new ThinLogging(); +// init(); } @Override public Logger getLogger(String name, Module module) { - return logging.getLogger(name, module); + lazyInit(); + Objects.requireNonNull(name); + Logger logger = logging.getLogger(name, module); + Objects.requireNonNull(logger); + return logger; + } + + private static void init() { + logging = new ThinLogging(); + reloadConfiguration(); + } + + /** Reload configuration form system properties */ + public static void reloadConfiguration() { + if (logging == null) + return; + 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. + * 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; - logging = new ThinLogging(); - ThinJavaUtilLogging.init(); + if (javaUtilLogging != null) + return; + init(); + javaUtilLogging = ThinJavaUtilLogging.init(); + javaUtilLogging.readConfiguration(logging.getLevels()); } - public static Logger getLogger(String name) { + static Consumer> getConfigurationConsumer() { + Objects.requireNonNull(logging); + return logging; + } + + static Flow.Publisher> getLogEntryPublisher() { + Objects.requireNonNull(logging); + return logging.getLogEntryPublisher(); + } + + @Override + public Publisher> get() { + return 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); } + + @Override + public void accept(Map t) { + if (logging != null) { + // delegate to thin logging + logging.accept(t); + } else { + // ignore + // TODO try to congure Java logging ? + } + + } + }