Search in sources :

Example 36 with LoggerContext

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();
    }
}
Also used : XmlConfiguration(org.apache.logging.log4j.core.config.xml.XmlConfiguration) XmlConfiguration(org.apache.logging.log4j.core.config.xml.XmlConfiguration) File(java.io.File) LoggerContext(org.apache.logging.log4j.core.LoggerContext) BeforeClass(org.junit.BeforeClass)

Example 37 with LoggerContext

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());
}
Also used : LoggerContext(org.apache.logging.log4j.core.LoggerContext) Map(java.util.Map) Test(org.junit.Test)

Example 38 with LoggerContext

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)));
}
Also used : FileAppender(org.apache.logging.log4j.core.appender.FileAppender) XmlConfiguration(org.apache.logging.log4j.core.config.xml.XmlConfiguration) XmlConfiguration(org.apache.logging.log4j.core.config.xml.XmlConfiguration) Logger(org.apache.logging.log4j.Logger) StatusLogger(org.apache.logging.log4j.status.StatusLogger) LoggerContext(org.apache.logging.log4j.core.LoggerContext) StatusConsoleListener(org.apache.logging.log4j.status.StatusConsoleListener) StatusListener(org.apache.logging.log4j.status.StatusListener) File(java.io.File) Test(org.junit.Test)

Example 39 with LoggerContext

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();
}
Also used : LoggerContext(org.apache.logging.log4j.core.LoggerContext) Map(java.util.Map) Test(org.junit.Test)

Example 40 with LoggerContext

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);
}
Also used : AppenderComponentBuilder(org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder) BuiltConfiguration(org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration) LayoutComponentBuilder(org.apache.logging.log4j.core.config.builder.api.LayoutComponentBuilder) BuiltConfiguration(org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration) ComponentBuilder(org.apache.logging.log4j.core.config.builder.api.ComponentBuilder) LayoutComponentBuilder(org.apache.logging.log4j.core.config.builder.api.LayoutComponentBuilder) AppenderComponentBuilder(org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder) LoggerContext(org.apache.logging.log4j.core.LoggerContext) Test(org.junit.Test)

Aggregations

LoggerContext (org.apache.logging.log4j.core.LoggerContext)163 Configuration (org.apache.logging.log4j.core.config.Configuration)57 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)36 Test (org.junit.Test)33 Appender (org.apache.logging.log4j.core.Appender)18 File (java.io.File)12 IOException (java.io.IOException)12 Logger (org.apache.logging.log4j.Logger)11 BeforeClass (org.junit.BeforeClass)11 Map (java.util.Map)10 Level (org.apache.logging.log4j.Level)8 LogEvent (org.apache.logging.log4j.core.LogEvent)8 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)7 PatternLayout (org.apache.logging.log4j.core.layout.PatternLayout)7 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)7 Logger (org.apache.logging.log4j.core.Logger)6 AbstractConfiguration (org.apache.logging.log4j.core.config.AbstractConfiguration)5 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)4 RoutingAppender (org.apache.logging.log4j.core.appender.routing.RoutingAppender)4 BuiltConfiguration (org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration)4