1 package org
.argeo
.init
.logging
;
3 import java
.io
.ByteArrayInputStream
;
4 import java
.io
.ByteArrayOutputStream
;
5 import java
.io
.IOException
;
6 import java
.lang
.System
.Logger
.Level
;
8 import java
.util
.Properties
;
9 import java
.util
.logging
.Handler
;
10 import java
.util
.logging
.LogManager
;
11 import java
.util
.logging
.LogRecord
;
14 * Fallback wrapper around the java.util.logging framework, when thin logging
15 * could not be instantiated directly.
17 class ThinJavaUtilLogging
{
18 private LogManager logManager
;
20 private ThinJavaUtilLogging(LogManager logManager
) {
21 this.logManager
= logManager
;
22 this.logManager
.reset();
25 static ThinJavaUtilLogging
init() {
26 LogManager logManager
= LogManager
.getLogManager();
27 ThinJavaUtilLogging thinJul
= new ThinJavaUtilLogging(logManager
);
31 private static Level
fromJulLevel(java
.util
.logging
.Level julLevel
) {
32 if (java
.util
.logging
.Level
.ALL
.equals(julLevel
))
34 else if (java
.util
.logging
.Level
.FINER
.equals(julLevel
))
36 else if (java
.util
.logging
.Level
.FINE
.equals(julLevel
))
38 else if (java
.util
.logging
.Level
.INFO
.equals(julLevel
))
40 else if (java
.util
.logging
.Level
.WARNING
.equals(julLevel
))
42 else if (java
.util
.logging
.Level
.SEVERE
.equals(julLevel
))
44 else if (java
.util
.logging
.Level
.OFF
.equals(julLevel
))
47 throw new IllegalArgumentException("Unsupported JUL level " + julLevel
);
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
;
66 throw new IllegalArgumentException("Unsupported logging level " + level
);
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());
75 try (ByteArrayOutputStream out
= new ByteArrayOutputStream()) {
76 properties
.store(out
, null);
77 try (ByteArrayInputStream in
= new ByteArrayInputStream(out
.toByteArray())) {
78 logManager
.readConfiguration(in
);
80 } catch (IOException e
) {
81 throw new IllegalStateException("Cannot apply JUL configuration", e
);
83 logManager
.getLogger("").addHandler(new ThinHandler());
87 * A fallback {@link Handler} forwarding only messages and logger name (all
88 * other {@link LogRecord} information is lost.
90 private static class ThinHandler
extends Handler
{
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());
102 public void close() throws SecurityException
{