All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ch.qos.logback.core.sift.AppenderTracker Maven / Gradle / Ivy

There is a newer version: 2.12.15
Show newest version
/**
 * 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();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy