Search in sources :

Example 1 with LifeCycle2

use of org.apache.logging.log4j.core.LifeCycle2 in project logging-log4j2 by apache.

the class AbstractConfiguration method stop.

/**
     * Tear down the configuration.
     */
@Override
public boolean stop(final long timeout, final TimeUnit timeUnit) {
    this.setStopping();
    super.stop(timeout, timeUnit, false);
    LOGGER.trace("Stopping {}...", this);
    for (final LoggerConfig loggerConfig : loggerConfigs.values()) {
        loggerConfig.getReliabilityStrategy().beforeStopConfiguration(this);
    }
    root.getReliabilityStrategy().beforeStopConfiguration(this);
    final String cls = getClass().getSimpleName();
    LOGGER.trace("{} notified {} ReliabilityStrategies that config will be stopped.", cls, loggerConfigs.size() + 1);
    if (!loggerConfigs.isEmpty()) {
        LOGGER.trace("{} stopping {} LoggerConfigs.", cls, loggerConfigs.size());
        for (final LoggerConfig logger : loggerConfigs.values()) {
            logger.stop(timeout, timeUnit);
        }
    }
    LOGGER.trace("{} stopping root LoggerConfig.", cls);
    if (!root.isStopped()) {
        root.stop(timeout, timeUnit);
    }
    if (hasAsyncLoggers()) {
        LOGGER.trace("{} stopping AsyncLoggerConfigDisruptor.", cls);
        asyncLoggerConfigDisruptor.stop(timeout, timeUnit);
    }
    // Stop the appenders in reverse order in case they still have activity.
    final Appender[] array = appenders.values().toArray(new Appender[appenders.size()]);
    final List<Appender> async = getAsyncAppenders(array);
    if (!async.isEmpty()) {
        // LOG4J2-511, LOG4J2-392 stop AsyncAppenders first
        LOGGER.trace("{} stopping {} AsyncAppenders.", cls, async.size());
        for (final Appender appender : async) {
            if (appender instanceof LifeCycle2) {
                ((LifeCycle2) appender).stop(timeout, timeUnit);
            } else {
                appender.stop();
            }
        }
    }
    LOGGER.trace("{} notifying ReliabilityStrategies that appenders will be stopped.", cls);
    for (final LoggerConfig loggerConfig : loggerConfigs.values()) {
        loggerConfig.getReliabilityStrategy().beforeStopAppenders();
    }
    root.getReliabilityStrategy().beforeStopAppenders();
    LOGGER.trace("{} stopping remaining Appenders.", cls);
    int appenderCount = 0;
    for (int i = array.length - 1; i >= 0; --i) {
        if (array[i].isStarted()) {
            // then stop remaining Appenders
            if (array[i] instanceof LifeCycle2) {
                ((LifeCycle2) array[i]).stop(timeout, timeUnit);
            } else {
                array[i].stop();
            }
            appenderCount++;
        }
    }
    LOGGER.trace("{} stopped {} remaining Appenders.", cls, appenderCount);
    LOGGER.trace("{} cleaning Appenders from {} LoggerConfigs.", cls, loggerConfigs.size() + 1);
    for (final LoggerConfig loggerConfig : loggerConfigs.values()) {
        // LOG4J2-520, LOG4J2-392:
        // Important: do not clear appenders until after all AsyncLoggerConfigs
        // have been stopped! Stopping the last AsyncLoggerConfig will
        // shut down the disruptor and wait for all enqueued events to be processed.
        // Only *after this* the appenders can be cleared or events will be lost.
        loggerConfig.clearAppenders();
    }
    root.clearAppenders();
    if (watchManager.isStarted()) {
        watchManager.stop(timeout, timeUnit);
    }
    configurationScheduler.stop(timeout, timeUnit);
    if (advertiser != null && advertisement != null) {
        advertiser.unadvertise(advertisement);
    }
    setStopped();
    LOGGER.debug("Stopped {} OK", this);
    return true;
}
Also used : AsyncAppender(org.apache.logging.log4j.core.appender.AsyncAppender) Appender(org.apache.logging.log4j.core.Appender) ConsoleAppender(org.apache.logging.log4j.core.appender.ConsoleAppender) LifeCycle2(org.apache.logging.log4j.core.LifeCycle2) AsyncLoggerConfig(org.apache.logging.log4j.core.async.AsyncLoggerConfig)

Example 2 with LifeCycle2

use of org.apache.logging.log4j.core.LifeCycle2 in project logging-log4j2 by apache.

the class RoutingAppender method stop.

@Override
public boolean stop(final long timeout, final TimeUnit timeUnit) {
    setStopping();
    super.stop(timeout, timeUnit, false);
    final Map<String, Appender> map = configuration.getAppenders();
    for (final Map.Entry<String, AppenderControl> entry : appenders.entrySet()) {
        final Appender appender = entry.getValue().getAppender();
        if (!map.containsKey(appender.getName())) {
            if (appender instanceof LifeCycle2) {
                ((LifeCycle2) appender).stop(timeout, timeUnit);
            } else {
                appender.stop();
            }
        }
    }
    setStopped();
    return true;
}
Also used : AbstractAppender(org.apache.logging.log4j.core.appender.AbstractAppender) Appender(org.apache.logging.log4j.core.Appender) LifeCycle2(org.apache.logging.log4j.core.LifeCycle2) AppenderControl(org.apache.logging.log4j.core.config.AppenderControl) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map)

Example 3 with LifeCycle2

use of org.apache.logging.log4j.core.LifeCycle2 in project logging-log4j2 by apache.

the class Log4jServletContextListener method contextDestroyed.

@Override
public void contextDestroyed(final ServletContextEvent event) {
    if (this.servletContext == null || this.initializer == null) {
        LOGGER.warn("Context destroyed before it was initialized.");
        return;
    }
    LOGGER.debug("Log4jServletContextListener ensuring that Log4j shuts down properly.");
    // the application is finished
    this.initializer.clearLoggerContext();
    // shutting down now
    if (initializer instanceof LifeCycle2) {
        final String stopTimeoutStr = servletContext.getInitParameter(KEY_STOP_TIMEOUT);
        final long stopTimeout = Strings.isEmpty(stopTimeoutStr) ? DEFAULT_STOP_TIMEOUT : Long.parseLong(stopTimeoutStr);
        final String timeoutTimeUnitStr = servletContext.getInitParameter(KEY_STOP_TIMEOUT_TIMEUNIT);
        final TimeUnit timeoutTimeUnit = Strings.isEmpty(timeoutTimeUnitStr) ? DEFAULT_STOP_TIMEOUT_TIMEUNIT : TimeUnit.valueOf(timeoutTimeUnitStr.toUpperCase(Locale.ROOT));
        ((LifeCycle2) this.initializer).stop(stopTimeout, timeoutTimeUnit);
    } else {
        this.initializer.stop();
    }
}
Also used : LifeCycle2(org.apache.logging.log4j.core.LifeCycle2) TimeUnit(java.util.concurrent.TimeUnit)

Example 4 with LifeCycle2

use of org.apache.logging.log4j.core.LifeCycle2 in project logging-log4j2 by apache.

the class CompositeFilter method stop.

@Override
public boolean stop(final long timeout, final TimeUnit timeUnit) {
    this.setStopping();
    for (final Filter filter : filters) {
        if (filter instanceof LifeCycle2) {
            ((LifeCycle2) filter).stop(timeout, timeUnit);
        } else {
            filter.stop();
        }
    }
    setStopped();
    return true;
}
Also used : LifeCycle2(org.apache.logging.log4j.core.LifeCycle2) Filter(org.apache.logging.log4j.core.Filter)

Aggregations

LifeCycle2 (org.apache.logging.log4j.core.LifeCycle2)4 Appender (org.apache.logging.log4j.core.Appender)2 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 TimeUnit (java.util.concurrent.TimeUnit)1 Filter (org.apache.logging.log4j.core.Filter)1 AbstractAppender (org.apache.logging.log4j.core.appender.AbstractAppender)1 AsyncAppender (org.apache.logging.log4j.core.appender.AsyncAppender)1 ConsoleAppender (org.apache.logging.log4j.core.appender.ConsoleAppender)1 AsyncLoggerConfig (org.apache.logging.log4j.core.async.AsyncLoggerConfig)1 AppenderControl (org.apache.logging.log4j.core.config.AppenderControl)1