73 lines
2.0 KiB
Java
73 lines
2.0 KiB
Java
package io.gitlab.jfronny.commons.logging;
|
|
|
|
import io.gitlab.jfronny.commons.ref.WeakSet;
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
import java.util.Objects;
|
|
import java.util.Set;
|
|
import java.util.function.Function;
|
|
|
|
public class HotSwappingDelegateLogger extends DelegateLogger {
|
|
public static Function<String, Logger> LOGGER_BUILDER;
|
|
private static Level MINIMUM_LEVEL = Level.INFO;
|
|
private static final Set<HotSwappingDelegateLogger> KNOWN_LOGGERS = new WeakSet<>();
|
|
protected boolean minimumLevelChanged = false;
|
|
|
|
static {
|
|
resetStrategy();
|
|
}
|
|
|
|
public static void resetStrategy() {
|
|
LOGGER_BUILDER = StdoutLogger::fancy;
|
|
}
|
|
|
|
public static void updateStrategy(@NotNull Function<String, Logger> factory) {
|
|
LOGGER_BUILDER = Objects.requireNonNull(factory);
|
|
synchronized (KNOWN_LOGGERS) {
|
|
for (HotSwappingDelegateLogger ref : KNOWN_LOGGERS) {
|
|
ref.updateStrategy();
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void updateMinimumLevel(Level minimumLevel) {
|
|
MINIMUM_LEVEL = Objects.requireNonNull(minimumLevel);
|
|
synchronized (KNOWN_LOGGERS) {
|
|
for (HotSwappingDelegateLogger ref : KNOWN_LOGGERS) {
|
|
ref.updateMinimumLevel();
|
|
}
|
|
}
|
|
}
|
|
|
|
public static HotSwappingDelegateLogger forName(@NotNull String name) {
|
|
return new HotSwappingDelegateLogger(name);
|
|
}
|
|
|
|
private final String name;
|
|
|
|
private HotSwappingDelegateLogger(String name) {
|
|
this.name = name;
|
|
synchronized (KNOWN_LOGGERS) {
|
|
updateStrategy();
|
|
updateMinimumLevel();
|
|
KNOWN_LOGGERS.add(this);
|
|
}
|
|
}
|
|
|
|
private void updateStrategy() {
|
|
delegate = LOGGER_BUILDER.apply(name);
|
|
}
|
|
|
|
private void updateMinimumLevel() {
|
|
if (!minimumLevelChanged) {
|
|
minimumLevel = MINIMUM_LEVEL;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void setMinimumLevel(Level minLevel) {
|
|
super.setMinimumLevel(minLevel);
|
|
this.minimumLevelChanged = true;
|
|
}
|
|
}
|