|
| 1 | +package org.echocat.tjl; |
| 2 | + |
| 3 | +import org.echocat.tjl.util.ExtendedLogRecord; |
| 4 | + |
| 5 | +import java.io.OutputStream; |
| 6 | +import java.security.PrivilegedAction; |
| 7 | +import java.util.Optional; |
| 8 | +import java.util.logging.*; |
| 9 | + |
| 10 | +import static java.lang.ClassLoader.getSystemClassLoader; |
| 11 | +import static java.lang.System.getProperty; |
| 12 | +import static java.lang.System.getenv; |
| 13 | +import static java.security.AccessController.doPrivileged; |
| 14 | +import static java.util.Optional.ofNullable; |
| 15 | +import static java.util.logging.Level.ALL; |
| 16 | +import static java.util.logging.Level.parse; |
| 17 | +import static java.util.logging.LogManager.getLogManager; |
| 18 | + |
| 19 | +public class Handler extends StreamHandler { |
| 20 | + |
| 21 | + public Handler() { |
| 22 | + this(System.out); |
| 23 | + } |
| 24 | + |
| 25 | + public Handler(OutputStream out) { |
| 26 | + super(out, new JsonFormatter()); |
| 27 | + init(); |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public synchronized void publish(LogRecord record) { |
| 32 | + final ExtendedLogRecord extended = new ExtendedLogRecord(record); |
| 33 | + extended.detectAndSetSource(className -> !getClass().getName().equals(className)); |
| 34 | + super.publish(extended); |
| 35 | + flush(); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public synchronized void close() throws SecurityException { |
| 40 | + flush(); |
| 41 | + } |
| 42 | + |
| 43 | + protected void init() { |
| 44 | + final Formatter formatter = determineFormatter(); |
| 45 | + final Level level = determineLevel(); |
| 46 | + patchGlobalLevelIfRequired(); |
| 47 | + |
| 48 | + doPrivileged((PrivilegedAction<Void>) () -> { |
| 49 | + setLevel(level); |
| 50 | + setFormatter(formatter); |
| 51 | + return null; |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + protected Formatter determineFormatter() { |
| 56 | + return findSystemProperty("log.format", "LOG_FORMAT") |
| 57 | + .map(String::trim) |
| 58 | + .map(String::toUpperCase) |
| 59 | + .map(this::formatToFormatter) |
| 60 | + .orElseGet(() -> findProperty(getClass(), "formatter") |
| 61 | + .map(this::newInstanceOf) |
| 62 | + .filter(candidate -> candidate instanceof Formatter) |
| 63 | + .map(candidate -> (Formatter) candidate) |
| 64 | + .orElseGet(JsonFormatter::new) |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + protected Level determineLevel() { |
| 69 | + return findProperty(getClass(), "level") |
| 70 | + .map(value -> parse(value.trim())) |
| 71 | + .orElse(ALL); |
| 72 | + } |
| 73 | + |
| 74 | + protected void patchGlobalLevelIfRequired() { |
| 75 | + findSystemProperty("log.level", "LOG_LEVEL") |
| 76 | + .map(String::trim) |
| 77 | + .map(String::toUpperCase) |
| 78 | + .map(Level::parse) |
| 79 | + .ifPresent(level -> getLogManager().getLogger("").setLevel(level)); |
| 80 | + } |
| 81 | + |
| 82 | + protected Object newInstanceOf(String className) { |
| 83 | + try { |
| 84 | + //noinspection deprecation |
| 85 | + return getSystemClassLoader().loadClass(className).newInstance(); |
| 86 | + } catch (final Exception e) { |
| 87 | + return new IllegalStateException("Cannot create instance of '" + className + "'.", e); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + protected Formatter formatToFormatter(String format) { |
| 92 | + if (format.equals("JSON")) { |
| 93 | + return new JsonFormatter(); |
| 94 | + } |
| 95 | + if (format.equals("TEXT")) { |
| 96 | + return new TextFormatter(); |
| 97 | + } |
| 98 | + throw new IllegalArgumentException("Illegal format: " + format); |
| 99 | + } |
| 100 | + |
| 101 | + protected static Optional<String> findProperty(Class<?> owner, String name) { |
| 102 | + final String propertyName = owner.getName() + "." + name; |
| 103 | + return ofNullable(getLogManager().getProperty(propertyName)); |
| 104 | + } |
| 105 | + |
| 106 | + protected static Optional<String> findSystemProperty(String propName, String envName) { |
| 107 | + String value = getProperty(propName); |
| 108 | + if (value == null) { |
| 109 | + value = getenv(envName); |
| 110 | + } |
| 111 | + return ofNullable(value); |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments