use of org.apache.logging.log4j.core.LoggerContext in project logging-log4j2 by apache.
the class AdvertiserTest method setupClass.
@BeforeClass
public static void setupClass() {
final File file = new File(STATUS_LOG);
file.delete();
System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);
final LoggerContext ctx = LoggerContext.getContext();
final Configuration config = ctx.getConfiguration();
if (config instanceof XmlConfiguration) {
final String name = config.getName();
if (name == null || !name.equals("XMLConfigTest")) {
ctx.reconfigure();
}
} else {
ctx.reconfigure();
}
}
use of org.apache.logging.log4j.core.LoggerContext in project logging-log4j2 by apache.
the class AdvertiserTest method testAdvertisementsAddedOnReconfigAfterStop.
@Test
public void testAdvertisementsAddedOnReconfigAfterStop() {
verifyExpectedEntriesAdvertised(InMemoryAdvertiser.getAdvertisedEntries());
final LoggerContext ctx = LoggerContext.getContext();
ctx.stop();
final Map<Object, Map<String, String>> entries = InMemoryAdvertiser.getAdvertisedEntries();
assertTrue("Entries found: " + entries, entries.isEmpty());
ctx.start();
verifyExpectedEntriesAdvertised(InMemoryAdvertiser.getAdvertisedEntries());
}
use of org.apache.logging.log4j.core.LoggerContext in project logging-log4j2 by apache.
the class CustomConfigurationTest method testConfig.
@Test
public void testConfig() {
// don't bother using "error" since that's the default; try another level
final LoggerContext ctx = this.init.getLoggerContext();
ctx.reconfigure();
final Configuration config = ctx.getConfiguration();
assertThat(config, instanceOf(XmlConfiguration.class));
for (final StatusListener listener : StatusLogger.getLogger().getListeners()) {
if (listener instanceof StatusConsoleListener) {
assertSame(listener.getStatusLevel(), Level.INFO);
break;
}
}
final Layout<? extends Serializable> layout = PatternLayout.newBuilder().withPattern(PatternLayout.SIMPLE_CONVERSION_PATTERN).withConfiguration(config).build();
// @formatter:off
final FileAppender appender = FileAppender.newBuilder().withFileName(LOG_FILE).withAppend(false).withName("File").withIgnoreExceptions(false).withBufferSize(4000).withBufferedIo(false).withLayout(layout).build();
// @formatter:on
appender.start();
config.addAppender(appender);
final AppenderRef ref = AppenderRef.createAppenderRef("File", null, null);
final AppenderRef[] refs = new AppenderRef[] { ref };
final LoggerConfig loggerConfig = LoggerConfig.createLogger(false, Level.INFO, "org.apache.logging.log4j", "true", refs, null, config, null);
loggerConfig.addAppender(appender, null, null);
config.addLogger("org.apache.logging.log4j", loggerConfig);
ctx.updateLoggers();
final Logger logger = ctx.getLogger(CustomConfigurationTest.class.getName());
logger.info("This is a test");
final File file = new File(LOG_FILE);
assertThat(file, exists());
assertThat(file, hasLength(greaterThan(0L)));
}
use of org.apache.logging.log4j.core.LoggerContext in project logging-log4j2 by apache.
the class AdvertiserTest method testAdvertisementsRemovedOnConfigStop.
@Test
public void testAdvertisementsRemovedOnConfigStop() {
verifyExpectedEntriesAdvertised(InMemoryAdvertiser.getAdvertisedEntries());
final LoggerContext ctx = LoggerContext.getContext();
ctx.stop();
final Map<Object, Map<String, String>> entries = InMemoryAdvertiser.getAdvertisedEntries();
assertTrue("Entries found: " + entries, entries.isEmpty());
//reconfigure for subsequent testing
ctx.start();
}
use of org.apache.logging.log4j.core.LoggerContext in project logging-log4j2 by apache.
the class TestConfigurator method testRolling.
@Test
public void testRolling() throws Exception {
final ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
builder.setStatusLevel(Level.ERROR);
builder.setConfigurationName("RollingBuilder");
// create the console appender
AppenderComponentBuilder appenderBuilder = builder.newAppender("Stdout", "CONSOLE").addAttribute("target", ConsoleAppender.Target.SYSTEM_OUT);
appenderBuilder.add(builder.newLayout("PatternLayout").addAttribute("pattern", "%d [%t] %-5level: %msg%n%throwable"));
builder.add(appenderBuilder);
final LayoutComponentBuilder layoutBuilder = builder.newLayout("PatternLayout").addAttribute("pattern", "%d [%t] %-5level: %msg%n");
final ComponentBuilder triggeringPolicy = builder.newComponent("Policies").addComponent(builder.newComponent("CronTriggeringPolicy").addAttribute("schedule", "0 0 0 * * ?")).addComponent(builder.newComponent("SizeBasedTriggeringPolicy").addAttribute("size", "100M"));
appenderBuilder = builder.newAppender("rolling", "RollingFile").addAttribute("fileName", "target/rolling.log").addAttribute("filePattern", "target/archive/rolling-%d{MM-dd-yy}.log.gz").add(layoutBuilder).addComponent(triggeringPolicy);
builder.add(appenderBuilder);
// create the new logger
builder.add(builder.newLogger("TestLogger", Level.DEBUG).add(builder.newAppenderRef("rolling")).addAttribute("additivity", false));
builder.add(builder.newRootLogger(Level.DEBUG).add(builder.newAppenderRef("rolling")));
final Configuration config = builder.build();
config.initialize();
assertNotNull("No rolling file appender", config.getAppender("rolling"));
assertEquals("Unexpected Configuration", "RollingBuilder", config.getName());
// Initialize the new configuration
final LoggerContext ctx = Configurator.initialize(config);
Configurator.shutdown(ctx);
}
Aggregations