Search in sources :

Example 81 with Configuration

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

the class Log4j1ConfigurationFactoryTest method testFile.

private Layout<?> testFile(final String configResource) throws Exception {
    final Configuration configuration = getConfiguration(configResource);
    final FileAppender appender = configuration.getAppender("File");
    assertNotNull(appender);
    assertEquals("target/mylog.txt", appender.getFileName());
    //
    final LoggerConfig loggerConfig = configuration.getLoggerConfig("com.example.foo");
    assertNotNull(loggerConfig);
    assertEquals(Level.DEBUG, loggerConfig.getLevel());
    configuration.start();
    configuration.stop();
    return appender.getLayout();
}
Also used : FileAppender(org.apache.logging.log4j.core.appender.FileAppender) RollingFileAppender(org.apache.logging.log4j.core.appender.RollingFileAppender) Configuration(org.apache.logging.log4j.core.config.Configuration) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Example 82 with Configuration

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

the class Log4j1ConfigurationFactoryTest method testSystemProperties2.

@Test
public void testSystemProperties2() throws Exception {
    final Configuration configuration = getConfiguration("config-1.2/log4j-system-properties-2.properties");
    final RollingFileAppender appender = configuration.getAppender("RFA");
    assertEquals("${java.io.tmpdir}/hadoop.log", appender.getFileName());
    appender.stop(10, TimeUnit.SECONDS);
    Path path = new File(appender.getFileName()).toPath();
    Files.deleteIfExists(path);
    path = new File("${java.io.tmpdir}").toPath();
    Files.deleteIfExists(path);
}
Also used : Path(java.nio.file.Path) Configuration(org.apache.logging.log4j.core.config.Configuration) RollingFileAppender(org.apache.logging.log4j.core.appender.RollingFileAppender) File(java.io.File) Test(org.junit.Test)

Example 83 with Configuration

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

the class Log4j1ConfigurationFactoryTest method testSystemProperties1.

@Test
public void testSystemProperties1() throws Exception {
    final Configuration configuration = getConfiguration("config-1.2/log4j-system-properties-1.properties");
    final RollingFileAppender appender = configuration.getAppender("RFA");
    final String tempFileName = System.getProperty("java.io.tmpdir") + "/hadoop.log";
    System.out.println("expected: " + tempFileName + " Actual: " + appender.getFileName());
    assertEquals(tempFileName, appender.getFileName());
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) RollingFileAppender(org.apache.logging.log4j.core.appender.RollingFileAppender) Test(org.junit.Test)

Example 84 with Configuration

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

the class OutputStreamAppenderTest method addAppender.

/**
     * Tests that you can add an output stream appender dynamically.
     */
private void addAppender(final OutputStream outputStream, final String outputStreamName) {
    final LoggerContext context = LoggerContext.getContext(false);
    final Configuration config = context.getConfiguration();
    final PatternLayout layout = PatternLayout.createDefaultLayout(config);
    final Appender appender = OutputStreamAppender.createAppender(layout, null, outputStream, outputStreamName, false, true);
    appender.start();
    config.addAppender(appender);
    ConfigurationTestUtils.updateLoggers(appender, config);
}
Also used : Appender(org.apache.logging.log4j.core.Appender) Configuration(org.apache.logging.log4j.core.config.Configuration) PatternLayout(org.apache.logging.log4j.core.layout.PatternLayout) LoggerContext(org.apache.logging.log4j.core.LoggerContext)

Example 85 with Configuration

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

the class JsonLayoutTest method testEscapeLayout.

@Test
public void testEscapeLayout() throws Exception {
    final Map<String, Appender> appenders = this.rootLogger.getAppenders();
    for (final Appender appender : appenders.values()) {
        this.rootLogger.removeAppender(appender);
    }
    final Configuration configuration = rootLogger.getContext().getConfiguration();
    // set up appender
    final boolean propertiesAsList = false;
    // @formatter:off
    final AbstractJacksonLayout layout = JsonLayout.newBuilder().setConfiguration(configuration).setLocationInfo(true).setProperties(true).setPropertiesAsList(propertiesAsList).setComplete(true).setCompact(false).setEventEol(false).setIncludeStacktrace(true).build();
    // @formatter:on
    final ListAppender appender = new ListAppender("List", null, layout, true, false);
    appender.start();
    // set appender on root and set level to debug
    this.rootLogger.addAppender(appender);
    this.rootLogger.setLevel(Level.DEBUG);
    // output starting message
    this.rootLogger.debug("Here is a quote ' and then a double quote \"");
    appender.stop();
    final List<String> list = appender.getMessages();
    this.checkAt("[", 0, list);
    this.checkAt("{", 1, list);
    this.checkContains("\"level\" : \"DEBUG\",", list);
    this.checkContains("\"message\" : \"Here is a quote ' and then a double quote \\\"\",", list);
    this.checkContains("\"loggerFqcn\" : \"" + AbstractLogger.class.getName() + "\",", list);
    for (final Appender app : appenders.values()) {
        this.rootLogger.addAppender(app);
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ListAppender(org.apache.logging.log4j.test.appender.ListAppender) Configuration(org.apache.logging.log4j.core.config.Configuration) ListAppender(org.apache.logging.log4j.test.appender.ListAppender) AbstractLogger(org.apache.logging.log4j.spi.AbstractLogger) Test(org.junit.Test)

Aggregations

Configuration (org.apache.logging.log4j.core.config.Configuration)105 LoggerContext (org.apache.logging.log4j.core.LoggerContext)54 Test (org.junit.Test)43 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)39 Appender (org.apache.logging.log4j.core.Appender)21 DefaultConfiguration (org.apache.logging.log4j.core.config.DefaultConfiguration)15 File (java.io.File)12 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)9 RollingFileAppender (org.apache.logging.log4j.core.appender.RollingFileAppender)9 Path (java.nio.file.Path)7 Level (org.apache.logging.log4j.Level)7 NullConfiguration (org.apache.logging.log4j.core.config.NullConfiguration)7 ArrayList (java.util.ArrayList)6 Logger (org.apache.logging.log4j.Logger)6 Filter (org.apache.logging.log4j.core.Filter)6 AbstractLogger (org.apache.logging.log4j.spi.AbstractLogger)6 PatternLayout (org.apache.logging.log4j.core.layout.PatternLayout)5 ListAppender (org.apache.logging.log4j.test.appender.ListAppender)5 ConsoleAppender (org.apache.logging.log4j.core.appender.ConsoleAppender)4 AppenderRef (org.apache.logging.log4j.core.config.AppenderRef)4