]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.init/src/org/argeo/init/logging/ThinLoggerFinder.java
Improve init launch
[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.concurrent.Flow.Publisher;
11 import java.util.function.Consumer;
12 import java.util.function.Supplier;
13
14 /**
15 * Factory for Java system logging. As it has to be a public class in order to
16 * be exposed as a service provider, it is also the main entry point for the
17 * thin logging system, via static methos.
18 */
19 public class ThinLoggerFinder extends LoggerFinder
20 implements Consumer<Map<String, Object>>, Supplier<Flow.Publisher<Map<String, Serializable>>> {
21 private static ThinLogging logging;
22 private static ThinJavaUtilLogging javaUtilLogging;
23
24 public ThinLoggerFinder() {
25 if (logging != null)
26 throw new IllegalStateException("Only one logging can be initialised.");
27 // init();
28 }
29
30 @Override
31 public Logger getLogger(String name, Module module) {
32 lazyInit();
33 Objects.requireNonNull(name);
34 Logger logger = logging.getLogger(name, module);
35 Objects.requireNonNull(logger);
36 return logger;
37 }
38
39 private static void init() {
40 logging = new ThinLogging();
41 reloadConfiguration();
42 }
43
44 /** Reload configuration form system properties */
45 public static void reloadConfiguration() {
46 if (logging == null)
47 return;
48 Map<String, Object> configuration = new HashMap<>();
49 for (Object key : System.getProperties().keySet()) {
50 Objects.requireNonNull(key);
51 String property = key.toString();
52 if (property.startsWith(ThinLogging.LEVEL_PROPERTY_PREFIX)
53 || property.equals(ThinLogging.DEFAULT_LEVEL_PROPERTY))
54 configuration.put(property, System.getProperty(property));
55 }
56 logging.accept(configuration);
57 }
58
59 /**
60 * Falls back to java.util.logging if thin logging was not already initialised
61 * by the {@link LoggerFinder} mechanism.
62 */
63 public static void lazyInit() {
64 if (logging != null)
65 return;
66 if (javaUtilLogging != null)
67 return;
68 init();
69 javaUtilLogging = ThinJavaUtilLogging.init();
70 javaUtilLogging.readConfiguration(logging.getLevels());
71 }
72
73 static Consumer<Map<String, Object>> getConfigurationConsumer() {
74 Objects.requireNonNull(logging);
75 return logging;
76 }
77
78 static Flow.Publisher<Map<String, Serializable>> getLogEntryPublisher() {
79 Objects.requireNonNull(logging);
80 return logging.getLogEntryPublisher();
81 }
82
83 @Override
84 public Publisher<Map<String, Serializable>> get() {
85 return getLogEntryPublisher();
86 }
87
88 static void update(Map<String, Object> configuration) {
89 if (logging == null)
90 throw new IllegalStateException("Thin logging must be initialized first");
91 logging.accept(configuration);
92 if (javaUtilLogging != null)
93 javaUtilLogging.readConfiguration(logging.getLevels());
94 }
95
96 static Logger getLogger(String name) {
97 return logging.getLogger(name, null);
98 }
99
100 @Override
101 public void accept(Map<String, Object> t) {
102 if (logging != null) {
103 // delegate to thin logging
104 logging.accept(t);
105 } else {
106 // ignore
107 // TODO try to congure Java logging ?
108 }
109
110 }
111
112 }