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());
}
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());
}
}
}
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();
}
}
Aggregations