Search in sources :

Example 21 with LoggerContextSource

use of org.apache.logging.log4j.core.test.junit.LoggerContextSource in project logging-log4j2 by apache.

the class CustomConfigurationTest method testConfig.

@Test
@LoggerContextSource("log4j-props.xml")
public void testConfig(final LoggerContext ctx) throws IOException {
    // don't bother using "error" since that's the default; try another level
    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().setPattern(PatternLayout.SIMPLE_CONVERSION_PATTERN).setConfiguration(config).build();
    // @formatter:off
    final FileAppender appender = FileAppender.newBuilder().setFileName(LOG_FILE.toString()).setAppend(false).setName("File").setIgnoreExceptions(false).setBufferSize(4000).setBufferedIo(false).setLayout(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);
    logger.info("This is a test");
    assertTrue(Files.exists(LOG_FILE));
    assertThat(Files.size(LOG_FILE), greaterThan(0L));
}
Also used : FileAppender(org.apache.logging.log4j.core.appender.FileAppender) XmlConfiguration(org.apache.logging.log4j.core.config.xml.XmlConfiguration) StatusConsoleListener(org.apache.logging.log4j.status.StatusConsoleListener) XmlConfiguration(org.apache.logging.log4j.core.config.xml.XmlConfiguration) StatusListener(org.apache.logging.log4j.status.StatusListener) Logger(org.apache.logging.log4j.Logger) StatusLogger(org.apache.logging.log4j.status.StatusLogger) Test(org.junit.jupiter.api.Test) LoggerContextSource(org.apache.logging.log4j.core.test.junit.LoggerContextSource)

Example 22 with LoggerContextSource

use of org.apache.logging.log4j.core.test.junit.LoggerContextSource in project logging-log4j2 by apache.

the class MemoryMappedFileAppenderTest method testMemMapExtendsIfNeeded.

@Test
@LoggerContextSource("MemoryMappedFileAppenderRemapTest.xml")
public void testMemMapExtendsIfNeeded(final LoggerContext context) throws Exception {
    final Logger log = context.getLogger(getClass());
    final Path logFile = Paths.get("target", "MemoryMappedFileAppenderRemapTest.log");
    final char[] text = new char[256];
    Arrays.fill(text, 'A');
    final String str = new String(text);
    try {
        log.warn("Test log1");
        assertTrue(Files.exists(logFile));
        assertEquals(256, Files.size(logFile));
        log.warn(str);
        assertEquals(2 * 256, Files.size(logFile));
        log.warn(str);
        assertEquals(3 * 256, Files.size(logFile));
    } finally {
        context.stop();
    }
    assertEquals(521 + 3 * System.lineSeparator().length(), Files.size(logFile), "Expected file size to shrink");
    final List<String> lines = Files.readAllLines(logFile);
    assertThat(lines, both(hasSize(3)).and(contains("Test log1", str, str)));
}
Also used : Path(java.nio.file.Path) Logger(org.apache.logging.log4j.Logger) Test(org.junit.jupiter.api.Test) LoggerContextSource(org.apache.logging.log4j.core.test.junit.LoggerContextSource)

Example 23 with LoggerContextSource

use of org.apache.logging.log4j.core.test.junit.LoggerContextSource in project logging-log4j2 by apache.

the class OutputStreamManagerTest method narrow.

@Test
@LoggerContextSource("multipleIncompatibleAppendersTest.xml")
public void narrow(final LoggerContext context) {
    final Logger logger = context.getLogger(OutputStreamManagerTest.class);
    logger.info("test");
    final List<StatusData> statusData = StatusLogger.getLogger().getStatusData();
    StatusData data = statusData.get(0);
    if (data.getMessage().getFormattedMessage().contains("WindowsAnsiOutputStream")) {
        data = statusData.get(1);
    }
    assertEquals(Level.ERROR, data.getLevel());
    assertEquals("Could not create plugin of type class org.apache.logging.log4j.core.appender.RollingRandomAccessFileAppender for element RollingRandomAccessFile", data.getMessage().getFormattedMessage());
    assertEquals("org.apache.logging.log4j.core.config.ConfigurationException: Configuration has multiple incompatible Appenders pointing to the same resource 'target/multiIncompatibleAppender.log'", data.getThrowable().toString());
}
Also used : Logger(org.apache.logging.log4j.Logger) StatusLogger(org.apache.logging.log4j.status.StatusLogger) StatusData(org.apache.logging.log4j.status.StatusData) Test(org.junit.jupiter.api.Test) LoggerContextSource(org.apache.logging.log4j.core.test.junit.LoggerContextSource)

Example 24 with LoggerContextSource

use of org.apache.logging.log4j.core.test.junit.LoggerContextSource in project logging-log4j2 by apache.

the class FileAppenderPermissionsTest method testFilePermissions.

@Test
@LoggerContextSource(value = "log4j-posix.xml", timeout = 10)
void testFilePermissions(final LoggerContext context) throws IOException {
    final ExtendedLogger logger = context.getLogger(getClass());
    for (int i = 0; i < 1000; i++) {
        logger.debug("This is test message number {}", box(i));
    }
    final String permissions = PosixFilePermissions.toString(Files.getPosixFilePermissions(Paths.get("target/permissions1/AppenderTest-1.log")));
    assertEquals("rw-------", permissions);
}
Also used : ExtendedLogger(org.apache.logging.log4j.spi.ExtendedLogger) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) LoggerContextSource(org.apache.logging.log4j.core.test.junit.LoggerContextSource)

Example 25 with LoggerContextSource

use of org.apache.logging.log4j.core.test.junit.LoggerContextSource in project logging-log4j2 by apache.

the class ThreadLocalRecyclerNestedLoggingTest method nested_logging_should_not_pollute_thread_local.

@Test
@LoggerContextSource("threadLocalRecyclerNestedLogging.xml")
public void nested_logging_should_not_pollute_thread_local(final LoggerContext loggerContext, @Named(value = "List1") final ListAppender appender1, @Named(value = "List2") final ListAppender appender2) {
    final Logger logger = loggerContext.getLogger(ThreadLocalRecyclerNestedLoggingTest.class);
    logger.error("A", new ThrowableLoggingInGetMessage(logger));
    final List<String> messages1 = readAppendedMessages(appender1);
    final List<String> messages2 = readAppendedMessages(appender2);
    Assertions.assertThat(messages1).containsExactlyInAnyOrderElementsOf(messages2).containsExactlyInAnyOrderElementsOf(Stream.of("['B',null]", "['A','C']").map(json -> json.replaceAll("'", "\"")).collect(Collectors.toList()));
}
Also used : Logger(org.apache.logging.log4j.Logger) Test(org.junit.jupiter.api.Test) LoggerContextSource(org.apache.logging.log4j.core.test.junit.LoggerContextSource)

Aggregations

LoggerContextSource (org.apache.logging.log4j.core.test.junit.LoggerContextSource)26 Test (org.junit.jupiter.api.Test)26 Logger (org.apache.logging.log4j.Logger)14 Path (java.nio.file.Path)9 Filter (org.apache.logging.log4j.core.Filter)7 Appender (org.apache.logging.log4j.core.Appender)4 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)4 ThresholdFilter (org.apache.logging.log4j.core.filter.ThresholdFilter)4 ListAppender (org.apache.logging.log4j.core.test.appender.ListAppender)4 Collection (java.util.Collection)2 ExtendedLogger (org.apache.logging.log4j.spi.ExtendedLogger)2 StatusLogger (org.apache.logging.log4j.status.StatusLogger)2 IndexedReadOnlyStringMap (org.apache.logging.log4j.util.IndexedReadOnlyStringMap)2 Tag (org.junit.jupiter.api.Tag)2 Row (com.datastax.driver.core.Row)1 Session (com.datastax.driver.core.Session)1 Field (java.lang.reflect.Field)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 UUID (java.util.UUID)1