]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.init/src/org/argeo/init/logging/ThinJavaUtilLogging.java
Use latest release of Argeo Maven parent
[lgpl/argeo-commons.git] / org.argeo.init / src / org / argeo / init / logging / ThinJavaUtilLogging.java
1 package org.argeo.init.logging;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.ByteArrayOutputStream;
5 import java.io.IOException;
6 import java.lang.System.Logger.Level;
7 import java.util.Map;
8 import java.util.Properties;
9 import java.util.logging.Handler;
10 import java.util.logging.LogManager;
11 import java.util.logging.LogRecord;
12
13 /**
14 * Fallback wrapper around the java.util.logging framework, when thin logging
15 * could not be instantiated directly.
16 */
17 class ThinJavaUtilLogging {
18 private LogManager logManager;
19
20 private ThinJavaUtilLogging(LogManager logManager) {
21 this.logManager = logManager;
22 this.logManager.reset();
23 }
24
25 static ThinJavaUtilLogging init() {
26 LogManager logManager = LogManager.getLogManager();
27 ThinJavaUtilLogging thinJul = new ThinJavaUtilLogging(logManager);
28 return thinJul;
29 }
30
31 private static Level fromJulLevel(java.util.logging.Level julLevel) {
32 if (java.util.logging.Level.ALL.equals(julLevel))
33 return Level.ALL;
34 else if (java.util.logging.Level.FINER.equals(julLevel))
35 return Level.TRACE;
36 else if (java.util.logging.Level.FINE.equals(julLevel))
37 return Level.DEBUG;
38 else if (java.util.logging.Level.INFO.equals(julLevel))
39 return Level.INFO;
40 else if (java.util.logging.Level.WARNING.equals(julLevel))
41 return Level.WARNING;
42 else if (java.util.logging.Level.SEVERE.equals(julLevel))
43 return Level.ERROR;
44 else if (java.util.logging.Level.OFF.equals(julLevel))
45 return Level.OFF;
46 else
47 throw new IllegalArgumentException("Unsupported JUL level " + julLevel);
48 }
49
50 private static java.util.logging.Level toJulLevel(Level level) {
51 if (Level.ALL.equals(level))
52 return java.util.logging.Level.ALL;
53 else if (Level.TRACE.equals(level))
54 return java.util.logging.Level.FINER;
55 else if (Level.DEBUG.equals(level))
56 return java.util.logging.Level.FINE;
57 else if (Level.INFO.equals(level))
58 return java.util.logging.Level.INFO;
59 else if (Level.WARNING.equals(level))
60 return java.util.logging.Level.WARNING;
61 else if (Level.ERROR.equals(level))
62 return java.util.logging.Level.SEVERE;
63 else if (Level.OFF.equals(level))
64 return java.util.logging.Level.OFF;
65 else
66 throw new IllegalArgumentException("Unsupported logging level " + level);
67 }
68
69 void readConfiguration(Map<String, Level> configuration) {
70 this.logManager.reset();
71 Properties properties = new Properties();
72 for (String name : configuration.keySet()) {
73 properties.put(name + ".level", toJulLevel(configuration.get(name)).toString());
74 }
75 try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
76 properties.store(out, null);
77 try (ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray())) {
78 logManager.readConfiguration(in);
79 }
80 } catch (IOException e) {
81 throw new IllegalStateException("Cannot apply JUL configuration", e);
82 }
83 logManager.getLogger("").addHandler(new ThinHandler());
84 }
85
86 /**
87 * A fallback {@link Handler} forwarding only messages and logger name (all
88 * other {@link LogRecord} information is lost.
89 */
90 private static class ThinHandler extends Handler {
91 @Override
92 public void publish(LogRecord record) {
93 java.lang.System.Logger systemLogger = ThinLoggerFinder.getLogger(record.getLoggerName());
94 systemLogger.log(ThinJavaUtilLogging.fromJulLevel(record.getLevel()), record.getMessage(),
95 record.getThrown());
96 }
97
98 @Override
99 public void flush() {
100 }
101
102 @Override
103 public void close() throws SecurityException {
104 }
105
106 }
107 }