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));
}
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)));
}
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());
}
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);
}
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()));
}
Aggregations