Search in sources :

Example 36 with Appender

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

the class SocketAppenderTest method removeAndStopAppenders.

void removeAndStopAppenders() {
    final Map<String, Appender> map = logger.getAppenders();
    for (final Map.Entry<String, Appender> entry : map.entrySet()) {
        final Appender appender = entry.getValue();
        logger.removeAppender(appender);
        appender.stop();
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) Map(java.util.Map)

Example 37 with Appender

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

the class KafkaAppenderTest method testAppend.

@Test
public void testAppend() throws Exception {
    final Appender appender = ctx.getRequiredAppender("KafkaAppender");
    appender.append(createLogEvent());
    final List<ProducerRecord<byte[], byte[]>> history = kafka.history();
    assertEquals(1, history.size());
    final ProducerRecord<byte[], byte[]> item = history.get(0);
    assertNotNull(item);
    assertEquals(TOPIC_NAME, item.topic());
    assertNull(item.key());
    assertEquals(LOG_MESSAGE, new String(item.value(), StandardCharsets.UTF_8));
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) Test(org.junit.Test)

Example 38 with Appender

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

the class WebLookupTest method testLookup2.

@Test
public void testLookup2() throws Exception {
    ContextAnchor.THREAD_CONTEXT.remove();
    final ServletContext servletContext = new MockServletContext();
    servletContext.setAttribute("TestAttr", "AttrValue");
    servletContext.setInitParameter("myapp.logdir", "target");
    servletContext.setAttribute("Name1", "Ben");
    servletContext.setInitParameter("Name2", "Jerry");
    servletContext.setInitParameter("log4jConfiguration", "WEB-INF/classes/log4j-webvar.xml");
    final Log4jWebLifeCycle initializer = WebLoggerContextUtils.getWebLifeCycle(servletContext);
    initializer.start();
    initializer.setLoggerContext();
    final LoggerContext ctx = ContextAnchor.THREAD_CONTEXT.get();
    assertNotNull("No LoggerContext", ctx);
    assertNotNull("No ServletContext", ctx.getExternalContext());
    final Configuration config = ctx.getConfiguration();
    assertNotNull("No Configuration", config);
    final Map<String, Appender> appenders = config.getAppenders();
    for (final Map.Entry<String, Appender> entry : appenders.entrySet()) {
        if (entry.getKey().equals("file")) {
            final FileAppender fa = (FileAppender) entry.getValue();
            assertEquals("target/myapp.log", fa.getFileName());
        }
    }
    initializer.stop();
    ContextAnchor.THREAD_CONTEXT.remove();
}
Also used : FileAppender(org.apache.logging.log4j.core.appender.FileAppender) Appender(org.apache.logging.log4j.core.Appender) FileAppender(org.apache.logging.log4j.core.appender.FileAppender) Configuration(org.apache.logging.log4j.core.config.Configuration) ServletContext(javax.servlet.ServletContext) MockServletContext(org.springframework.mock.web.MockServletContext) LoggerContext(org.apache.logging.log4j.core.LoggerContext) Map(java.util.Map) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.Test)

Example 39 with Appender

use of org.apache.logging.log4j.core.Appender 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 40 with Appender

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

the class FailoverAppender method start.

@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();
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) AppenderControl(org.apache.logging.log4j.core.config.AppenderControl)

Aggregations

Appender (org.apache.logging.log4j.core.Appender)105 Test (org.junit.Test)52 ConsoleAppender (org.apache.logging.log4j.core.appender.ConsoleAppender)25 Configuration (org.apache.logging.log4j.core.config.Configuration)23 LoggerContext (org.apache.logging.log4j.core.LoggerContext)18 ListAppender (org.apache.logging.log4j.test.appender.ListAppender)18 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)15 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)14 Logger (org.apache.logging.log4j.Logger)13 BuiltConfiguration (org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration)12 AbstractAppender (org.apache.logging.log4j.core.appender.AbstractAppender)10 RollingFileAppender (org.apache.logging.log4j.core.appender.RollingFileAppender)10 AppenderControl (org.apache.logging.log4j.core.config.AppenderControl)10 Map (java.util.Map)8 Filter (org.apache.logging.log4j.core.Filter)8 URL (java.net.URL)7 IOException (java.io.IOException)6 LogEvent (org.apache.logging.log4j.core.LogEvent)6 File (java.io.File)5 Logger (org.apache.logging.log4j.core.Logger)5