use of org.apache.logging.log4j.core.Appender 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;
}
use of org.apache.logging.log4j.core.Appender in project logging-log4j2 by apache.
the class XIncludeTest method testLogger.
@Test
public void testLogger() throws Exception {
final Logger logger = this.ctx.getLogger(LOGGER_NAME);
assertThat(logger, is(instanceOf(org.apache.logging.log4j.core.Logger.class)));
final org.apache.logging.log4j.core.Logger l = (org.apache.logging.log4j.core.Logger) logger;
assertThat(l.getLevel(), is(equalTo(Level.DEBUG)));
assertThat(l.filterCount(), is(equalTo(1)));
final Iterator<Filter> iterator = l.getFilters();
assertThat(iterator.hasNext(), is(true));
final Filter filter = iterator.next();
assertThat(filter, is(instanceOf(ThreadContextMapFilter.class)));
final Map<String, Appender> appenders = l.getAppenders();
assertThat(appenders, is(notNullValue()));
assertThat(appenders, hasSize(1));
final Appender appender = appenders.get(APPENDER_NAME);
assertThat(appender, is(notNullValue()));
assertThat(appender.getName(), is(equalTo("STDOUT")));
}
use of org.apache.logging.log4j.core.Appender in project logging-log4j2 by apache.
the class ConfigurationAssemblerTest method validate.
private void validate(final Configuration config) {
assertNotNull(config);
assertNotNull(config.getName());
assertFalse(config.getName().isEmpty());
assertNotNull("No configuration created", config);
assertEquals("Incorrect State: " + config.getState(), config.getState(), LifeCycle.State.STARTED);
final Map<String, Appender> appenders = config.getAppenders();
assertNotNull(appenders);
assertTrue("Incorrect number of Appenders: " + appenders.size(), appenders.size() == 2);
final KafkaAppender kafkaAppender = (KafkaAppender) appenders.get("Kafka");
final GelfLayout gelfLayout = (GelfLayout) kafkaAppender.getLayout();
final Map<String, LoggerConfig> loggers = config.getLoggers();
assertNotNull(loggers);
assertTrue("Incorrect number of LoggerConfigs: " + loggers.size(), loggers.size() == 2);
final LoggerConfig rootLoggerConfig = loggers.get("");
assertEquals(Level.ERROR, rootLoggerConfig.getLevel());
assertFalse(rootLoggerConfig.isIncludeLocation());
final LoggerConfig loggerConfig = loggers.get("org.apache.logging.log4j");
assertEquals(Level.DEBUG, loggerConfig.getLevel());
assertTrue(loggerConfig.isIncludeLocation());
final Filter filter = config.getFilter();
assertNotNull("No Filter", filter);
assertTrue("Not a Threshold Filter", filter instanceof ThresholdFilter);
final List<CustomLevelConfig> customLevels = config.getCustomLevels();
assertNotNull("No CustomLevels", filter);
assertEquals(1, customLevels.size());
final CustomLevelConfig customLevel = customLevels.get(0);
assertEquals("Panic", customLevel.getLevelName());
assertEquals(17, customLevel.getIntLevel());
final Logger logger = LogManager.getLogger(getClass());
logger.info("Welcome to Log4j!");
}
use of org.apache.logging.log4j.core.Appender in project logging-log4j2 by apache.
the class CompositeConfigurationTest method compositeLogger.
/*
@Test
public void compositeConfigurationUsed() {
final LoggerContextRule lcr = new LoggerContextRule(
"classpath:log4j-comp-appender.xml,log4j-comp-appender.json");
Statement test = new Statement() {
@Override
public void evaluate() throws Throwable {
assertTrue(lcr.getConfiguration() instanceof CompositeConfiguration);
}
};
runTest(lcr, test);
}
@Test
public void compositeProperties() {
final LoggerContextRule lcr = new LoggerContextRule(
"classpath:log4j-comp-properties.xml,log4j-comp-properties.json");
Statement test = new Statement() {
@Override
public void evaluate() throws Throwable {
CompositeConfiguration config = (CompositeConfiguration) lcr.getConfiguration();
assertEquals("json", config.getStrSubstitutor().replace("${propertyShared}"));
assertEquals("xml", config.getStrSubstitutor().replace("${propertyXml}"));
assertEquals("json", config.getStrSubstitutor().replace("${propertyJson}"));
}
};
runTest(lcr, test);
}
@Test
public void compositeAppenders() {
final LoggerContextRule lcr = new LoggerContextRule(
"classpath:log4j-comp-appender.xml,log4j-comp-appender.json");
Statement test = new Statement() {
@Override
public void evaluate() throws Throwable {
CompositeConfiguration config = (CompositeConfiguration) lcr.getConfiguration();
Map<String, Appender> appender = config.getAppenders();
assertEquals(3, appender.size());
assertTrue(appender.get("STDOUT") instanceof ConsoleAppender);
assertTrue(appender.get("File") instanceof FileAppender);
assertTrue(appender.get("Override") instanceof RollingFileAppender);
}
};
runTest(lcr, test);
}
*/
@Test
public void compositeLogger() {
final LoggerContextRule lcr = new LoggerContextRule("classpath:log4j-comp-logger.xml,log4j-comp-logger.json");
final Statement test = new Statement() {
@Override
public void evaluate() throws Throwable {
final CompositeConfiguration config = (CompositeConfiguration) lcr.getConfiguration();
Map<String, Appender> appendersMap = config.getLogger("cat1").getAppenders();
assertEquals("Expected 2 Appender references for cat1 but got " + appendersMap.size(), 2, appendersMap.size());
assertTrue(appendersMap.get("STDOUT") instanceof ConsoleAppender);
appendersMap = config.getLogger("cat2").getAppenders();
assertEquals("Expected 1 Appender reference for cat2 but got " + appendersMap.size(), 1, appendersMap.size());
assertTrue(appendersMap.get("File") instanceof FileAppender);
appendersMap = config.getLogger("cat3").getAppenders();
assertEquals("Expected 1 Appender reference for cat3 but got " + appendersMap.size(), 1, appendersMap.size());
assertTrue(appendersMap.get("File") instanceof FileAppender);
appendersMap = config.getRootLogger().getAppenders();
assertEquals("Expected 2 Appender references for the root logger but got " + appendersMap.size(), 2, appendersMap.size());
assertTrue(appendersMap.get("File") instanceof FileAppender);
assertTrue(appendersMap.get("STDOUT") instanceof ConsoleAppender);
}
};
runTest(lcr, test);
}
use of org.apache.logging.log4j.core.Appender in project logging-log4j2 by apache.
the class TestConfigurator method testInitialize_Name_LocationName.
@Test
public void testInitialize_Name_LocationName() throws Exception {
ctx = Configurator.initialize("Test1", "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());
}
Aggregations