Search in sources :

Example 1 with AppenderControl

use of org.apache.logging.log4j.core.config.AppenderControl in project ignite by apache.

the class Log4J2Logger method fileName.

/** {@inheritDoc} */
@Nullable
@Override
public String fileName() {
    for (Logger log = impl; log != null; log = log.getParent()) {
        for (Appender a : log.getAppenders().values()) {
            if (a instanceof FileAppender)
                return ((FileAppender) a).getFileName();
            if (a instanceof RollingFileAppender)
                return ((RollingFileAppender) a).getFileName();
            if (a instanceof RoutingAppender) {
                try {
                    RoutingAppender routing = (RoutingAppender) a;
                    Field appsFiled = routing.getClass().getDeclaredField("appenders");
                    appsFiled.setAccessible(true);
                    Map<String, AppenderControl> appenders = (Map<String, AppenderControl>) appsFiled.get(routing);
                    for (AppenderControl control : appenders.values()) {
                        Appender innerApp = control.getAppender();
                        if (innerApp instanceof FileAppender)
                            return normilize(((FileAppender) innerApp).getFileName());
                        if (innerApp instanceof RollingFileAppender)
                            return normilize(((RollingFileAppender) innerApp).getFileName());
                    }
                } catch (IllegalAccessException | NoSuchFieldException e) {
                    error("Failed to get file name (was the implementation of log4j2 changed?).", e);
                }
            }
        }
    }
    return null;
}
Also used : Appender(org.apache.logging.log4j.core.Appender) RoutingAppender(org.apache.logging.log4j.core.appender.routing.RoutingAppender) ConsoleAppender(org.apache.logging.log4j.core.appender.ConsoleAppender) FileAppender(org.apache.logging.log4j.core.appender.FileAppender) RollingFileAppender(org.apache.logging.log4j.core.appender.RollingFileAppender) FileAppender(org.apache.logging.log4j.core.appender.FileAppender) RollingFileAppender(org.apache.logging.log4j.core.appender.RollingFileAppender) AppenderControl(org.apache.logging.log4j.core.config.AppenderControl) RollingFileAppender(org.apache.logging.log4j.core.appender.RollingFileAppender) IgniteLogger(org.apache.ignite.IgniteLogger) Logger(org.apache.logging.log4j.core.Logger) Field(java.lang.reflect.Field) RoutingAppender(org.apache.logging.log4j.core.appender.routing.RoutingAppender) Map(java.util.Map) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with AppenderControl

use of org.apache.logging.log4j.core.config.AppenderControl 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 AppenderControl

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

the class RoutingAppender method getControl.

private synchronized AppenderControl getControl(final String key, final LogEvent event) {
    AppenderControl control = appenders.get(key);
    if (control != null) {
        return control;
    }
    Route route = null;
    for (final Route r : routes.getRoutes()) {
        if (r.getAppenderRef() == null && key.equals(r.getKey())) {
            route = r;
            break;
        }
    }
    if (route == null) {
        route = defaultRoute;
        control = appenders.get(DEFAULT_KEY);
        if (control != null) {
            return control;
        }
    }
    if (route != null) {
        final Appender app = createAppender(route, event);
        if (app == null) {
            return null;
        }
        control = new AppenderControl(app, null, null);
        appenders.put(key, control);
    }
    return control;
}
Also used : AbstractAppender(org.apache.logging.log4j.core.appender.AbstractAppender) Appender(org.apache.logging.log4j.core.Appender) AppenderControl(org.apache.logging.log4j.core.config.AppenderControl)

Example 4 with AppenderControl

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

the class AsyncAppender method start.

@Override
public void start() {
    final Map<String, Appender> map = config.getAppenders();
    final List<AppenderControl> appenders = new ArrayList<>();
    for (final AppenderRef appenderRef : appenderRefs) {
        final Appender appender = map.get(appenderRef.getRef());
        if (appender != null) {
            appenders.add(new AppenderControl(appender, appenderRef.getLevel(), appenderRef.getFilter()));
        } else {
            LOGGER.error("No appender named {} was configured", appenderRef);
        }
    }
    if (errorRef != null) {
        final Appender appender = map.get(errorRef);
        if (appender != null) {
            errorAppender = new AppenderControl(appender, null, null);
        } else {
            LOGGER.error("Unable to set up error Appender. No appender named {} was configured", errorRef);
        }
    }
    if (appenders.size() > 0) {
        thread = new AsyncThread(appenders, queue);
        thread.setName("AsyncAppender-" + getName());
    } else if (errorRef == null) {
        throw new ConfigurationException("No appenders are available for AsyncAppender " + getName());
    }
    asyncQueueFullPolicy = AsyncQueueFullPolicyFactory.create();
    thread.start();
    super.start();
}
Also used : Appender(org.apache.logging.log4j.core.Appender) AppenderControl(org.apache.logging.log4j.core.config.AppenderControl) ConfigurationException(org.apache.logging.log4j.core.config.ConfigurationException) ArrayList(java.util.ArrayList) AppenderRef(org.apache.logging.log4j.core.config.AppenderRef)

Example 5 with AppenderControl

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

the class FailoverAppender method failover.

private void failover(final LogEvent event, final Exception ex) {
    final RuntimeException re = ex != null ? (ex instanceof LoggingException ? (LoggingException) ex : new LoggingException(ex)) : null;
    boolean written = false;
    Exception failoverException = null;
    for (final AppenderControl control : failoverAppenders) {
        try {
            control.callAppender(event);
            written = true;
            break;
        } catch (final Exception fex) {
            if (failoverException == null) {
                failoverException = fex;
            }
        }
    }
    if (!written && !ignoreExceptions()) {
        if (re != null) {
            throw re;
        }
        throw new LoggingException("Unable to write to failover appenders", failoverException);
    }
}
Also used : LoggingException(org.apache.logging.log4j.LoggingException) AppenderControl(org.apache.logging.log4j.core.config.AppenderControl) LoggingException(org.apache.logging.log4j.LoggingException)

Aggregations

AppenderControl (org.apache.logging.log4j.core.config.AppenderControl)14 Appender (org.apache.logging.log4j.core.Appender)8 AbstractAppender (org.apache.logging.log4j.core.appender.AbstractAppender)5 Map (java.util.Map)2 AppenderRef (org.apache.logging.log4j.core.config.AppenderRef)2 ListAppender (org.apache.logging.log4j.test.appender.ListAppender)2 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 Bindings (javax.script.Bindings)1 IgniteLogger (org.apache.ignite.IgniteLogger)1 LoggingException (org.apache.logging.log4j.LoggingException)1 Filter (org.apache.logging.log4j.core.Filter)1 LifeCycle2 (org.apache.logging.log4j.core.LifeCycle2)1 Logger (org.apache.logging.log4j.core.Logger)1 ConsoleAppender (org.apache.logging.log4j.core.appender.ConsoleAppender)1 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)1 RandomAccessFileAppender (org.apache.logging.log4j.core.appender.RandomAccessFileAppender)1 RollingFileAppender (org.apache.logging.log4j.core.appender.RollingFileAppender)1