1 package org
.argeo
.init
.logging
;
3 import java
.io
.Serializable
;
4 import java
.lang
.System
.Logger
;
5 import java
.lang
.System
.LoggerFinder
;
6 import java
.util
.HashMap
;
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
;
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.
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
;
24 public ThinLoggerFinder() {
26 throw new IllegalStateException("Only one logging can be initialised.");
31 public Logger
getLogger(String name
, Module module
) {
33 Objects
.requireNonNull(name
);
34 Logger logger
= logging
.getLogger(name
, module
);
35 Objects
.requireNonNull(logger
);
39 private static void init() {
40 logging
= new ThinLogging();
41 reloadConfiguration();
44 /** Reload configuration form system properties */
45 public static void reloadConfiguration() {
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
));
56 logging
.accept(configuration
);
60 * Falls back to java.util.logging if thin logging was not already initialised
61 * by the {@link LoggerFinder} mechanism.
63 public static void lazyInit() {
66 if (javaUtilLogging
!= null)
69 javaUtilLogging
= ThinJavaUtilLogging
.init();
70 javaUtilLogging
.readConfiguration(logging
.getLevels());
73 static Consumer
<Map
<String
, Object
>> getConfigurationConsumer() {
74 Objects
.requireNonNull(logging
);
78 static Flow
.Publisher
<Map
<String
, Serializable
>> getLogEntryPublisher() {
79 Objects
.requireNonNull(logging
);
80 return logging
.getLogEntryPublisher();
84 public Publisher
<Map
<String
, Serializable
>> get() {
85 return getLogEntryPublisher();
88 static void update(Map
<String
, Object
> configuration
) {
90 throw new IllegalStateException("Thin logging must be initialized first");
91 logging
.accept(configuration
);
92 if (javaUtilLogging
!= null)
93 javaUtilLogging
.readConfiguration(logging
.getLevels());
96 static Logger
getLogger(String name
) {
97 return logging
.getLogger(name
, null);
101 public void accept(Map
<String
, Object
> t
) {
102 if (logging
!= null) {
103 // delegate to thin logging
107 // TODO try to congure Java logging ?