Search in sources :

Example 96 with Appender

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

the class TestConfigurator method testInitialize_Name_PathName.

@Test
public void testInitialize_Name_PathName() throws Exception {
    ctx = Configurator.initialize("Test1", "target/test-classes/log4j2-config.xml");
    LogManager.getLogger("org.apache.test.TestConfigurator");
    Configuration config = ctx.getConfiguration();
    assertNotNull("No configuration", config);
    assertEquals("Incorrect Configuration.", CONFIG_NAME, config.getName());
    final Map<String, Appender> map = config.getAppenders();
    assertNotNull("Appenders map should not be null.", map);
    assertThat(map, hasSize(greaterThan(0)));
    assertThat("Wrong configuration", map, hasKey("List"));
    Configurator.shutdown(ctx);
    config = ctx.getConfiguration();
    assertEquals("Unexpected Configuration.", NullConfiguration.NULL_NAME, config.getName());
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ConsoleAppender(org.apache.logging.log4j.core.appender.ConsoleAppender) BuiltConfiguration(org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration) Test(org.junit.Test)

Example 97 with Appender

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

the class Server method registerAppenders.

private static void registerAppenders(final LoggerContext ctx, final MBeanServer mbs, final Executor executor) throws InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException {
    final Map<String, Appender> map = ctx.getConfiguration().getAppenders();
    for (final String name : map.keySet()) {
        final Appender appender = map.get(name);
        if (appender instanceof AsyncAppender) {
            final AsyncAppender async = ((AsyncAppender) appender);
            final AsyncAppenderAdmin mbean = new AsyncAppenderAdmin(ctx.getName(), async);
            register(mbs, mbean, mbean.getObjectName());
        } else {
            final AppenderAdmin mbean = new AppenderAdmin(ctx.getName(), appender);
            register(mbs, mbean, mbean.getObjectName());
        }
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) AsyncAppender(org.apache.logging.log4j.core.appender.AsyncAppender) AsyncAppender(org.apache.logging.log4j.core.appender.AsyncAppender)

Example 98 with Appender

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

the class ServletAppenderTest method testAppender.

@Test
public void testAppender() throws Exception {
    ContextAnchor.THREAD_CONTEXT.remove();
    final ServletContext servletContext = new MockServletContext();
    servletContext.setAttribute("TestAttr", "AttrValue");
    servletContext.setInitParameter("TestParam", "ParamValue");
    servletContext.setAttribute("Name1", "Ben");
    servletContext.setInitParameter("Name2", "Jerry");
    servletContext.setInitParameter(Log4jWebSupport.LOG4J_CONFIG_LOCATION, CONFIG);
    final Log4jWebLifeCycle initializer = WebLoggerContextUtils.getWebLifeCycle(servletContext);
    try {
        initializer.start();
        initializer.setLoggerContext();
        final LoggerContext ctx = ContextAnchor.THREAD_CONTEXT.get();
        assertNotNull("No LoggerContext", ctx);
        assertNotNull("No ServletContext", ctx.getExternalContext());
        final Configuration configuration = ctx.getConfiguration();
        assertNotNull("No configuration", configuration);
        final Appender appender = configuration.getAppender("Servlet");
        assertNotNull("No ServletAppender", appender);
        final Logger logger = LogManager.getLogger("Test");
        logger.info("This is a test");
        logger.error("This is a test 2", new IllegalStateException().fillInStackTrace());
    } catch (final IllegalStateException e) {
        fail("Failed to initialize Log4j properly." + e.getMessage());
    } finally {
        initializer.stop();
        ContextAnchor.THREAD_CONTEXT.remove();
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) Configuration(org.apache.logging.log4j.core.config.Configuration) ServletContext(javax.servlet.ServletContext) MockServletContext(org.springframework.mock.web.MockServletContext) Logger(org.apache.logging.log4j.Logger) LoggerContext(org.apache.logging.log4j.core.LoggerContext) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.Test)

Aggregations

Appender (org.apache.logging.log4j.core.Appender)98 Test (org.junit.Test)50 ConsoleAppender (org.apache.logging.log4j.core.appender.ConsoleAppender)25 Configuration (org.apache.logging.log4j.core.config.Configuration)21 ListAppender (org.apache.logging.log4j.test.appender.ListAppender)18 LoggerContext (org.apache.logging.log4j.core.LoggerContext)15 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)13 Logger (org.apache.logging.log4j.Logger)12 BuiltConfiguration (org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration)12 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)11 AbstractAppender (org.apache.logging.log4j.core.appender.AbstractAppender)9 RollingFileAppender (org.apache.logging.log4j.core.appender.RollingFileAppender)9 Map (java.util.Map)8 Filter (org.apache.logging.log4j.core.Filter)8 AppenderControl (org.apache.logging.log4j.core.config.AppenderControl)8 URL (java.net.URL)7 IOException (java.io.IOException)5 LogEvent (org.apache.logging.log4j.core.LogEvent)5 Logger (org.apache.logging.log4j.core.Logger)5 AsyncAppender (org.apache.logging.log4j.core.appender.AsyncAppender)5