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