org.apache.logging.log4j.core.config.AppenderControl Java Examples

The following examples show how to use org.apache.logging.log4j.core.config.AppenderControl. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FailoverAppender.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
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);
    }
}
 
Example #2
Source File: Log4J2Logger.java    From ignite with Apache License 2.0 5 votes vote down vote up
/** {@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 normalize(((FileAppender)innerApp).getFileName());

                        if (innerApp instanceof RollingFileAppender)
                            return normalize(((RollingFileAppender)innerApp).getFileName());
                    }
                }
                catch (IllegalAccessException | NoSuchFieldException e) {
                    error("Failed to get file name (was the implementation of log4j2 changed?).", e);
                }
            }
        }
    }

    return null;
}
 
Example #3
Source File: LoggerConfigBenchmark.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Setup
public void setup() {

    listAppender.start();
    final AppenderControl control = new AppenderControl(listAppender, Level.ALL, null);
    appenderSet.add(control);
}
 
Example #4
Source File: AsyncAppender.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
/**
 * Calls {@link AppenderControl#callAppender(LogEvent) callAppender} on all registered {@code AppenderControl}
 * objects, and returns {@code true} if at least one appender call was successful, {@code false} otherwise. Any
 * exceptions are silently ignored.
 *
 * @param event the event to forward to the registered appenders
 * @return {@code true} if at least one appender call succeeded, {@code false} otherwise
 */
boolean callAppenders(final LogEvent event) {
    boolean success = false;
    for (final AppenderControl control : appenders) {
        try {
            control.callAppender(event);
            success = true;
        } catch (final Exception ex) {
            // If no appender is successful the error appender will get it.
        }
    }
    return success;
}
 
Example #5
Source File: RewriteAppender.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public void start() {
    for (final AppenderRef ref : appenderRefs) {
        final String name = ref.getRef();
        final Appender appender = config.getAppender(name);
        if (appender != null) {
            final Filter filter = appender instanceof AbstractAppender ?
                ((AbstractAppender) appender).getFilter() : null;
            appenders.put(name, new AppenderControl(appender, ref.getLevel(), filter));
        } else {
            LOGGER.error("Appender " + ref + " cannot be located. Reference ignored");
        }
    }
    super.start();
}
 
Example #6
Source File: RewriteAppender.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
/**
 * Modifies the event and pass to the subordinate Appenders.
 * @param event The LogEvent.
 */
@Override
public void append(LogEvent event) {
    if (rewritePolicy != null) {
        event = rewritePolicy.rewrite(event);
    }
    for (final AppenderControl control : appenders.values()) {
        control.callAppender(event);
    }
}
 
Example #7
Source File: FailoverAppender.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public void start() {
    final Map<String, Appender> map = config.getAppenders();
    int errors = 0;
    final Appender appender = map.get(primaryRef);
    if (appender != null) {
        primary = new AppenderControl(appender, null, null);
    } else {
        LOGGER.error("Unable to locate primary Appender " + primaryRef);
        ++errors;
    }
    for (final String name : failovers) {
        final Appender foAppender = map.get(name);
        if (foAppender != null) {
            failoverAppenders.add(new AppenderControl(foAppender, null, null));
        } else {
            LOGGER.error("Failover appender " + name + " is not configured");
        }
    }
    if (failoverAppenders.isEmpty()) {
        LOGGER.error("No failover appenders are available");
        ++errors;
    }
    if (errors == 0) {
        super.start();
    }
}
 
Example #8
Source File: DefaultRouteScriptAppenderTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private ListAppender getListAppender() {
    final String key = "Service2";
    final RoutingAppender routingAppender = getRoutingAppender();
    Assert.assertTrue(routingAppender.isStarted());
    final Map<String, AppenderControl> appenders = routingAppender.getAppenders();
    final AppenderControl appenderControl = appenders.get(key);
    assertNotNull("No appender control generated for '" + key + "'; appenders = " + appenders, appenderControl);
    final ListAppender listAppender = (ListAppender) appenderControl.getAppender();
    return listAppender;
}
 
Example #9
Source File: RoutesScriptAppenderTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private ListAppender getListAppender() {
    final String key = "Service2";
    final RoutingAppender routingAppender = getRoutingAppender();
    Assert.assertTrue(routingAppender.isStarted());
    final Map<String, AppenderControl> appenders = routingAppender.getAppenders();
    final AppenderControl appenderControl = appenders.get(key);
    assertNotNull("No appender control generated for '" + key + "'; appenders = " + appenders, appenderControl);
    return (ListAppender) appenderControl.getAppender();
}
 
Example #10
Source File: LoggerConfigBenchmark.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
protected void callAppenders(final LogEvent event) {
    for (final AppenderControl control : appenderSet) {
        control.callAppender(event);
    }
}
 
Example #11
Source File: AsyncAppender.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
public AsyncThread(final List<AppenderControl> appenders, final BlockingQueue<LogEvent> queue) {
    super("AsyncAppender-" + THREAD_SEQUENCE.getAndIncrement());
    this.appenders = appenders;
    this.queue = queue;
    setDaemon(true);
}
 
Example #12
Source File: RoutingAppender.java    From logging-log4j2 with Apache License 2.0 2 votes vote down vote up
/**
 * Returns an unmodifiable view of the appenders created by this {@link RoutingAppender}.
 * Note that this map does not contain appenders that are routed by reference.
 */
public Map<String, AppenderControl> getAppenders() {
    return createdAppendersUnmodifiableView;
}