package ch.qos.logback.classic.turbo;
import java.io.File;
import java.net.URL;
import java.util.List;
import ch.qos.logback.classic.gaffer.GafferUtil;
import ch.qos.logback.classic.util.EnvUtil;
import ch.qos.logback.core.CoreConstants;
import ch.qos.logback.core.joran.event.SaxEvent;
import ch.qos.logback.core.joran.spi.ConfigurationWatchList;
import ch.qos.logback.core.joran.util.ConfigurationWatchListUtil;
import ch.qos.logback.core.status.StatusUtil;
import org.slf4j.Marker;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.spi.FilterReply;
import static ch.qos.logback.core.CoreConstants.MILLIS_IN_ONE_SECOND;
public class ReconfigureOnChangeFilter extends TurboFilter {
public final static long DEFAULT_REFRESH_PERIOD = 60 * MILLIS_IN_ONE_SECOND;
long refreshPeriod = DEFAULT_REFRESH_PERIOD;
URL mainConfigurationURL;
protected volatile long nextCheck;
ConfigurationWatchList configurationWatchList;
@Override
public void start() {
configurationWatchList = ConfigurationWatchListUtil.getConfigurationWatchList(context);
if (configurationWatchList != null) {
mainConfigurationURL = configurationWatchList.getMainURL();
if (mainConfigurationURL == null) {
addWarn("Due to missing top level configuration file, automatic reconfiguration is impossible.");
return;
}
List<File> watchList = configurationWatchList.getCopyOfFileWatchList();
long inSeconds = refreshPeriod / 1000;
addInfo("Will scan for changes in [" + watchList + "] every " + inSeconds + " seconds. ");
synchronized (configurationWatchList) {
updateNextCheck(System.currentTimeMillis());
}
super.start();
} else {
addWarn("Empty ConfigurationWatchList in context");
}
}
@Override
public String toString() {
return "ReconfigureOnChangeFilter{" + "invocationCounter=" + invocationCounter + '}';
}
private long invocationCounter = 0;
private volatile long mask = 0xF;
private volatile long lastMaskCheck = System.currentTimeMillis();
@Override
public FilterReply decide(Marker marker, Logger logger, Level level, String format, Object[] params, Throwable t) {
if (!isStarted()) {
return FilterReply.NEUTRAL;
}
if (((invocationCounter++) & mask) != mask) {
return FilterReply.NEUTRAL;
}
long now = System.currentTimeMillis();
synchronized (configurationWatchList) {
updateMaskIfNecessary(now);
if (changeDetected(now)) {
disableSubsequentReconfiguration();
detachReconfigurationToNewThread();
}
}
return FilterReply.NEUTRAL;
}
private static final int MAX_MASK = 0xFFFF;
private static final long MASK_INCREASE_THRESHOLD = 100;
private static final long MASK_DECREASE_THRESHOLD = MASK_INCREASE_THRESHOLD * 8;
private void updateMaskIfNecessary(long now) {
final long timeElapsedSinceLastMaskUpdateCheck = now - lastMaskCheck;
lastMaskCheck = now;
if (timeElapsedSinceLastMaskUpdateCheck < MASK_INCREASE_THRESHOLD && (mask < MAX_MASK)) {
mask = (mask << 1) | 1;
} else if (timeElapsedSinceLastMaskUpdateCheck > MASK_DECREASE_THRESHOLD) {
mask = mask >>> 2;
}
}
void detachReconfigurationToNewThread() {
addInfo("Detected change in [" + configurationWatchList.getCopyOfFileWatchList() + "]");
context.getExecutorService().submit(new ReconfiguringThread());
}
void updateNextCheck(long now) {
nextCheck = now + refreshPeriod;
}
protected boolean changeDetected(long now) {
if (now >= nextCheck) {
updateNextCheck(now);
return configurationWatchList.changeDetected();
}
return false;
}
void disableSubsequentReconfiguration() {
nextCheck = Long.MAX_VALUE;
}
public long getRefreshPeriod() {
return refreshPeriod;
}
public void setRefreshPeriod(long refreshPeriod) {
this.refreshPeriod = refreshPeriod;
}
class ReconfiguringThread implements Runnable {
public void run() {
if (mainConfigurationURL == null) {
addInfo("Due to missing top level configuration file, skipping reconfiguration");
return;
}
LoggerContext lc = (LoggerContext) context;
addInfo(CoreConstants.RESET_MSG_PREFIX + "named [" + context.getName() + "]");
if (mainConfigurationURL.toString().endsWith("xml")) {
performXMLConfiguration(lc);
} else if (mainConfigurationURL.toString().endsWith("groovy")) {
if (EnvUtil.isGroovyAvailable()) {
lc.reset();
GafferUtil.runGafferConfiguratorOn(lc, this, mainConfigurationURL);
} else {
addError("Groovy classes are not available on the class path. ABORTING INITIALIZATION.");
}
}
}
private void performXMLConfiguration(LoggerContext lc) {
JoranConfigurator jc = new JoranConfigurator();
jc.setContext(context);
StatusUtil statusUtil = new StatusUtil(context);
List<SaxEvent> eventList = jc.recallSafeConfiguration();
URL mainURL = ConfigurationWatchListUtil.getMainWatchURL(context);
lc.reset();
long threshold = System.currentTimeMillis();
try {
jc.doConfigure(mainConfigurationURL);
if (statusUtil.hasXMLParsingErrors(threshold)) {
fallbackConfiguration(lc, eventList, mainURL);
}
} catch (JoranException e) {
fallbackConfiguration(lc, eventList, mainURL);
}
}
private void fallbackConfiguration(LoggerContext lc, List<SaxEvent> eventList, URL mainURL) {
JoranConfigurator joranConfigurator = new JoranConfigurator();
joranConfigurator.setContext(context);
if (eventList != null) {
addWarn("Falling back to previously registered safe configuration.");
try {
lc.reset();
JoranConfigurator.informContextOfURLUsedForConfiguration(context, mainURL);
joranConfigurator.doConfigure(eventList);
addInfo("Re-registering previous fallback configuration once more as a fallback configuration point");
joranConfigurator.registerSafeConfiguration(eventList);
} catch (JoranException e) {
addError("Unexpected exception thrown by a configuration considered safe.", e);
}
} else {
addWarn("No previous configuration to fall back on.");
}
}
}
}