]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.init/src/org/argeo/init/logging/ThinLoggerFinder.java
Close release cycle
[lgpl/argeo-commons.git] / org.argeo.init / src / org / argeo / init / logging / ThinLoggerFinder.java
1 package org.argeo.init.logging;
2
3 import java.io.Serializable;
4 import java.lang.System.Logger;
5 import java.lang.System.LoggerFinder;
6 import java.util.HashMap;
7 import java.util.Map;
8 import java.util.Objects;
9 import java.util.concurrent.Flow;
10 import java.util.function.Consumer;
11
12 /**
13 * Factory for Java system logging. As it has to be a public class in order to
14 * be exposed as a service provider, it is also the main entry point for the
15 * thin logging system, via static methos.
16 */
17 public class ThinLoggerFinder extends LoggerFinder {
18 private static ThinLogging logging;
19 private static ThinJavaUtilLogging javaUtilLogging;
20
21 public ThinLoggerFinder() {
22 if (logging != null)
23 throw new IllegalStateException("Only one logging can be initialised.");
24 // init();
25 }
26
27 @Override
28 public Logger getLogger(String name, Module module) {
29 lazyInit();
30 return logging.getLogger(name, module);
31 }
32
33 private static void init() {
34 logging = new ThinLogging();
35 reloadConfiguration();
36 }
37
38 /** Reload configuration form system properties */
39 public static void reloadConfiguration() {
40 if (logging == null)
41 return;
42 Map<String, Object> configuration = new HashMap<>();
43 for (Object key : System.getProperties().keySet()) {
44 Objects.requireNonNull(key);
45 String property = key.toString();
46 if (property.startsWith(ThinLogging.LEVEL_PROPERTY_PREFIX)
47 || property.equals(ThinLogging.DEFAULT_LEVEL_PROPERTY))
48 configuration.put(property, System.getProperty(property));
49 }
50 logging.accept(configuration);
51 }
52
53 /**
54 * Falls back to java.util.logging if thin logging was not already initialised
55 * by the {@link LoggerFinder} mechanism.
56 */
57 public static void lazyInit() {
58 if (logging != null)
59 return;
60 if (javaUtilLogging != null)
61 return;
62 init();
63 javaUtilLogging = ThinJavaUtilLogging.init();
64 javaUtilLogging.readConfiguration(logging.getLevels());
65 }
66
67 public static Consumer<Map<String, Object>> getConfigurationConsumer() {
68 Objects.requireNonNull(logging);
69 return logging;
70 }
71
72 public static Flow.Publisher<Map<String, Serializable>> getLogEntryPublisher() {
73 Objects.requireNonNull(logging);
74 return logging.getLogEntryPublisher();
75 }
76
77 static void update(Map<String, Object> configuration) {
78 if (logging == null)
79 throw new IllegalStateException("Thin logging must be initialized first");
80 logging.accept(configuration);
81 if (javaUtilLogging != null)
82 javaUtilLogging.readConfiguration(logging.getLevels());
83 }
84
85 static Logger getLogger(String name) {
86 return logging.getLogger(name, null);
87 }
88 }