ch.qos.logback.core.sift.AppenderTracker Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of virtdata-lib-realer Show documentation
Show all versions of virtdata-lib-realer Show documentation
With inspiration from other libraries
/**
* Logback: the reliable, generic, fast and flexible logging framework.
* Copyright (C) 1999-2015, QOS.ch. All rights reserved.
*
* This program and the accompanying materials are dual-licensed under
* either the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation
*
* or (per the licensee's choosing)
*
* under the terms of the GNU Lesser General Public License version 2.1
* as published by the Free Software Foundation.
*/
package ch.qos.logback.core.sift;
import ch.qos.logback.core.Appender;
import ch.qos.logback.core.Context;
import ch.qos.logback.core.CoreConstants;
import ch.qos.logback.core.helpers.NOPAppender;
import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.spi.AbstractComponentTracker;
import ch.qos.logback.core.spi.ContextAwareImpl;
/**
* Track appenders by key. When an appender is not used for
* longer than {@link #DEFAULT_TIMEOUT} it is stopped and removed.
*
* @author Tommy Becker
* @author Ceki Gulcu
* @author David Roussel
*/
public class AppenderTracker extends AbstractComponentTracker> {
int nopaWarningCount = 0;
final Context context;
final AppenderFactory appenderFactory;
final ContextAwareImpl contextAware;
public AppenderTracker(Context context, AppenderFactory appenderFactory) {
super();
this.context = context;
this.appenderFactory = appenderFactory;
this.contextAware = new ContextAwareImpl(context, this);
}
@Override
protected void processPriorToRemoval(Appender component) {
component.stop();
}
@Override
protected Appender buildComponent(String key) {
Appender appender = null;
try {
appender = appenderFactory.buildAppender(context, key);
} catch (JoranException je) {
contextAware.addError("Error while building appender with discriminating value [" + key + "]");
}
if (appender == null) {
appender = buildNOPAppender(key);
}
return appender;
}
private NOPAppender buildNOPAppender(String key) {
if (nopaWarningCount < CoreConstants.MAX_ERROR_COUNT) {
nopaWarningCount++;
contextAware.addError("Building NOPAppender for discriminating value [" + key + "]");
}
NOPAppender nopa = new NOPAppender();
nopa.setContext(context);
nopa.start();
return nopa;
}
@Override
protected boolean isComponentStale(Appender appender) {
return !appender.isStarted();
}
}