|
| 1 | +package pl.mjaron.tinyloki; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | +import java.util.Objects; |
| 6 | +import java.util.logging.Level; |
| 7 | +import java.util.logging.LogRecord; |
| 8 | + |
| 9 | +/** |
| 10 | + * The log appender which writes logs from the {@link java.util.logging.Logger java.util.logging} library. |
| 11 | + * It may be initialized with one of the {@link #install(Settings)} static methods. |
| 12 | + * |
| 13 | + * @since 1.1.6 |
| 14 | + */ |
| 15 | +public class TinyLokiJulHandler extends java.util.logging.Handler { |
| 16 | + |
| 17 | + /** |
| 18 | + * Attaches the logger to root logger. |
| 19 | + * |
| 20 | + * @param settings The {@link TinyLoki} library {@link Settings}. |
| 21 | + * @return Created {@link TinyLoki} instance. |
| 22 | + */ |
| 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); |
| 26 | + rootLogger.addHandler(handler); |
| 27 | + return handler.getLoki(); |
| 28 | + } |
| 29 | + |
| 30 | + public static void install(String url) { |
| 31 | + install(TinyLoki.withUrl(url)); |
| 32 | + } |
| 33 | + |
| 34 | + public static void install(String url, String user, String pass) { |
| 35 | + install(TinyLoki.withUrl(url).withBasicAuth(user, pass)); |
| 36 | + } |
| 37 | + |
| 38 | + private static class StreamKey { |
| 39 | + final String tag; |
| 40 | + final Level level; |
| 41 | + final int hash; |
| 42 | + |
| 43 | + private StreamKey(String tag, Level level) { |
| 44 | + this.tag = tag; |
| 45 | + this.level = level; |
| 46 | + this.hash = Objects.hash(tag, level); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public boolean equals(Object o) { |
| 51 | + if (this == o) return true; |
| 52 | + if (o == null || getClass() != o.getClass()) return false; |
| 53 | + StreamKey that = (StreamKey) o; |
| 54 | + return tag.equals(that.tag) && level == that.level; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public int hashCode() { |
| 59 | + return this.hash; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private static String toTinyLokiLogLevel(final Level level) { |
| 64 | + final int value = level.intValue(); |
| 65 | + if (value >= 1000) { // SEVERE |
| 66 | + return Labels.FATAL; |
| 67 | + } else if (value >= 900) { // WARNING |
| 68 | + return Labels.WARN; |
| 69 | + } else if (value >= 800) { // INFO |
| 70 | + return Labels.INFO; |
| 71 | + } else if (value >= 700) { // CONFIG |
| 72 | + return Labels.DEBUG; |
| 73 | + } else { |
| 74 | + return Labels.VERBOSE; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private final TinyLoki loki; |
| 79 | + private Map<StreamKey, ILogStream> streams = new HashMap<>(); |
| 80 | + |
| 81 | + public TinyLokiJulHandler(TinyLoki loki) { |
| 82 | + this.loki = loki; |
| 83 | + } |
| 84 | + |
| 85 | + public TinyLokiJulHandler(Settings settings) { |
| 86 | + loki = TinyLoki.open(settings); |
| 87 | + } |
| 88 | + |
| 89 | + public TinyLoki getLoki() { |
| 90 | + return loki; |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + synchronized public void publish(LogRecord logRecord) { |
| 95 | + StreamKey key = new StreamKey(logRecord.getLoggerName(), logRecord.getLevel()); |
| 96 | + ILogStream stream = streams.get(key); |
| 97 | + if (stream == null) { |
| 98 | + Labels streamLabels = Labels.of().l("tag", logRecord.getLoggerName()).l(Labels.LEVEL, toTinyLokiLogLevel(logRecord.getLevel())); |
| 99 | + stream = loki.openStream(streamLabels); |
| 100 | + streams.put(key, stream); |
| 101 | + } |
| 102 | + stream.log(logRecord.getMessage()); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public void flush() { |
| 107 | + try { |
| 108 | + loki.sync(); |
| 109 | + } catch (InterruptedException ignored) { |
| 110 | + Thread.currentThread().interrupt(); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public void close() throws SecurityException { |
| 116 | + try { |
| 117 | + loki.closeSync(); |
| 118 | + } catch (InterruptedException ignored) { |
| 119 | + Thread.currentThread().interrupt(); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments