Skip to content

Commit 500b1a7

Browse files
committed
JUL: Implementation of changing log level.
1 parent b47261f commit 500b1a7

1 file changed

Lines changed: 66 additions & 13 deletions

File tree

src/main/java/pl/mjaron/tinyloki/TinyLokiJulHandler.java

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package pl.mjaron.tinyloki;
22

3+
import java.util.Arrays;
34
import java.util.HashMap;
45
import java.util.Map;
56
import java.util.Objects;
@@ -14,27 +15,79 @@
1415
*/
1516
public class TinyLokiJulHandler extends java.util.logging.Handler {
1617

18+
/**
19+
* Provides the JUL logger with an empty name.
20+
*
21+
* @return Root JUL logger.
22+
* @since 1.1.6
23+
*/
24+
public static java.util.logging.Logger getJulRootLogger() {
25+
return java.util.logging.LogManager.getLogManager().getLogger("");
26+
}
27+
1728
/**
1829
* Attaches the logger to root logger.
1930
*
20-
* @param settings The {@link TinyLoki} library {@link Settings}.
21-
* @return Created {@link TinyLoki} instance.
31+
* @param loki The {@link TinyLoki} instance which is already {@link TinyLoki#open(Settings) open}.
32+
* @return The same {@link TinyLoki} object provided as argument.
33+
* @since 1.1.7
2234
*/
23-
public static TinyLoki install(Settings settings) {
24-
java.util.logging.Logger rootLogger = java.util.logging.LogManager.getLogManager().getLogger("");
25-
TinyLokiJulHandler handler = new TinyLokiJulHandler(settings);
35+
public static TinyLoki install(final TinyLoki loki) {
36+
final java.util.logging.Logger rootLogger = getJulRootLogger();
37+
final TinyLokiJulHandler handler = new TinyLokiJulHandler(loki);
2638
rootLogger.addHandler(handler);
2739
return handler.getLoki();
2840
}
2941

30-
public static void install(String url) {
42+
/**
43+
* Attaches the logger to root logger.
44+
*
45+
* @param settings The {@link TinyLoki} library {@link Settings}.
46+
* @return Created {@link TinyLoki} instance.
47+
* @since 1.1.7
48+
*/
49+
public static TinyLoki install(final Settings settings) {
50+
return install(settings.open());
51+
}
52+
53+
/**
54+
* Install the new instance of {@link TinyLoki} with default settings and given URL.
55+
*
56+
* @param url Destination where to send the logs.
57+
* @since 1.1.6
58+
*/
59+
public static void install(final String url) {
3160
install(TinyLoki.withUrl(url));
3261
}
3362

34-
public static void install(String url, String user, String pass) {
63+
/**
64+
* Install the new instance of {@link TinyLoki} with default settings and given URL and basic auth credentials.
65+
*
66+
* @param url Destination where to send the logs.
67+
* @param user Basic auth user.
68+
* @param pass Basic auth password.
69+
* @since 1.1.6
70+
*/
71+
public static void install(final String url, final String user, final String pass) {
3572
install(TinyLoki.withUrl(url).withBasicAuth(user, pass));
3673
}
3774

75+
/**
76+
* Sets the log level of whole JUL framework:
77+
* <ul>
78+
* <li>The root logger.</li>
79+
* <li>All log handlers of root logger.</li>
80+
* </ul>
81+
*
82+
* @param newLevel The new level.
83+
* @since 1.1.7
84+
*/
85+
public static void setJulLogLevel(final java.util.logging.Level newLevel) {
86+
java.util.logging.Logger rootLogger = getJulRootLogger();
87+
rootLogger.setLevel(newLevel);
88+
Arrays.stream(rootLogger.getHandlers()).forEach(h -> h.setLevel(newLevel));
89+
}
90+
3891
private static class StreamKey {
3992
final String tag;
4093
final Level level;
@@ -47,7 +100,7 @@ private StreamKey(String tag, Level level) {
47100
}
48101

49102
@Override
50-
public boolean equals(Object o) {
103+
public boolean equals(final Object o) {
51104
if (this == o) return true;
52105
if (o == null || getClass() != o.getClass()) return false;
53106
StreamKey that = (StreamKey) o;
@@ -60,7 +113,7 @@ public int hashCode() {
60113
}
61114
}
62115

63-
private static String toTinyLokiLogLevel(final Level level) {
116+
public static String toTinyLokiLogLevel(final Level level) {
64117
final int value = level.intValue();
65118
if (value >= 1000) { // SEVERE
66119
return Labels.FATAL;
@@ -76,7 +129,7 @@ private static String toTinyLokiLogLevel(final Level level) {
76129
}
77130

78131
private final TinyLoki loki;
79-
private Map<StreamKey, ILogStream> streams = new HashMap<>();
132+
private final Map<StreamKey, ILogStream> streams = new HashMap<>();
80133

81134
public TinyLokiJulHandler(TinyLoki loki) {
82135
this.loki = loki;
@@ -91,7 +144,7 @@ public TinyLoki getLoki() {
91144
}
92145

93146
@Override
94-
synchronized public void publish(LogRecord logRecord) {
147+
synchronized public void publish(final LogRecord logRecord) {
95148
StreamKey key = new StreamKey(logRecord.getLoggerName(), logRecord.getLevel());
96149
ILogStream stream = streams.get(key);
97150
if (stream == null) {
@@ -106,7 +159,7 @@ synchronized public void publish(LogRecord logRecord) {
106159
public void flush() {
107160
try {
108161
loki.sync();
109-
} catch (InterruptedException ignored) {
162+
} catch (final InterruptedException ignored) {
110163
Thread.currentThread().interrupt();
111164
}
112165
}
@@ -115,7 +168,7 @@ public void flush() {
115168
public void close() throws SecurityException {
116169
try {
117170
loki.closeSync();
118-
} catch (InterruptedException ignored) {
171+
} catch (final InterruptedException ignored) {
119172
Thread.currentThread().interrupt();
120173
}
121174
}

0 commit comments

Comments
 (0)