Search in sources :

Example 26 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 27 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 28 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 29 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)

Example 30 with LoggerContext

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

the class AbstractJpaAppenderTest method tearDown.

public void tearDown() throws SQLException {
    final LoggerContext context = LoggerContext.getContext(false);
    try {
        final Appender appender = context.getConfiguration().getAppender("databaseAppender");
        assertNotNull("The appender should not be null.", appender);
        assertTrue("The appender should be a JpaAppender.", appender instanceof JpaAppender);
        ((JpaAppender) appender).getManager().close();
    } finally {
        System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
        context.reconfigure();
        StatusLogger.getLogger().reset();
        try (Statement statement = this.connection.createStatement()) {
            statement.execute("SHUTDOWN");
        }
        this.connection.close();
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) Statement(java.sql.Statement) LoggerContext(org.apache.logging.log4j.core.LoggerContext)

Aggregations

LoggerContext (org.apache.logging.log4j.core.LoggerContext)156 Configuration (org.apache.logging.log4j.core.config.Configuration)53 Test (org.junit.Test)33 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)32 Appender (org.apache.logging.log4j.core.Appender)15 File (java.io.File)12 IOException (java.io.IOException)11 Logger (org.apache.logging.log4j.Logger)11 BeforeClass (org.junit.BeforeClass)11 Map (java.util.Map)9 Level (org.apache.logging.log4j.Level)8 LogEvent (org.apache.logging.log4j.core.LogEvent)8 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)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 AppenderRef (org.apache.logging.log4j.core.config.AppenderRef)5 PatternLayout (org.apache.logging.log4j.core.layout.PatternLayout)5 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)4 BuiltConfiguration (org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration)4