use of org.apache.logging.log4j.Logger in project logging-log4j2 by apache.
the class RollingAppenderCronEvery2DirectTest method testAppender.
@Test
public void testAppender() throws Exception {
// TODO Is there a better way to test than putting the thread to sleep all over the place?
final Logger logger = loggerContextRule.getLogger();
final long end = System.currentTimeMillis() + 5000;
final Random rand = new SecureRandom();
rand.setSeed(end);
int count = 1;
do {
logger.debug("Log Message {}", count++);
Thread.sleep(10 * rand.nextInt(100));
} while (System.currentTimeMillis() < end);
final File dir = new File(DIR);
assertTrue("Directory not created", dir.exists() && dir.listFiles().length > 0);
final int MAX_TRIES = 20;
final Matcher<File[]> hasGzippedFile = hasItemInArray(that(hasName(that(endsWith(".gz")))));
boolean succeeded = false;
for (int i = 0; i < MAX_TRIES; i++) {
final File[] files = dir.listFiles();
if (hasGzippedFile.matches(files)) {
succeeded = true;
break;
}
logger.debug("Sleeping #" + i);
// Allow time for rollover to complete
Thread.sleep(100);
}
if (!succeeded) {
final File[] files = dir.listFiles();
for (final File dirFile : files) {
logger.error("Found file: " + dirFile.getPath());
}
fail("No compressed files found");
}
}
use of org.apache.logging.log4j.Logger in project logging-log4j2 by apache.
the class RollingAppenderCronOnStartupTest method testAppender.
@Test
public void testAppender() throws Exception {
final File dir = new File(DIR);
if (dir.exists()) {
cleanDir(dir);
} else {
dir.mkdirs();
}
final File file = new File(FILE);
String today = formatter.format(LocalDate.now());
final File rolled = new File(DIR + "/test1-" + today + ".log");
PrintStream ps = new PrintStream(new FileOutputStream(file));
ps.println("This is a line2");
ps.close();
ps = new PrintStream(new FileOutputStream(rolled));
ps.println("This is a line 1");
ps.close();
assertTrue("Log file does not exist", file.exists());
assertTrue("Log file does not exist", rolled.exists());
LoggerContext lc = Configurator.initialize("Test", CONFIG);
final Logger logger = lc.getLogger(RollingAppenderCronOnStartupTest.class);
logger.info("This is line 3");
File[] files = dir.listFiles();
assertNotNull("No files", files);
assertEquals("Unexpected number of files. Expected 2 but found " + files.length, 2, files.length);
List<String> lines = Files.readAllLines(file.toPath());
assertEquals("Unexpected number of lines. Expected 2: Actual: " + lines.size(), 2, lines.size());
lines = Files.readAllLines(rolled.toPath());
assertEquals("Unexpected number of lines. Expected 1: Actual: " + lines.size(), 1, lines.size());
}
use of org.apache.logging.log4j.Logger in project logging-log4j2 by apache.
the class RollingRandomAccessFileAppenderRolloverTest method testRollover.
@Test
@Ignore
public void testRollover() throws Exception {
final File file = new File("target", "RollingRandomAccessFileAppenderTest.log");
// System.out.println(f.getAbsolutePath());
final File after1 = new File("target", "afterRollover-1.log");
file.delete();
after1.delete();
final Logger log = LogManager.getLogger("com.foo.Bar");
final String msg = "First a short message that does not trigger rollover";
log.info(msg);
Thread.sleep(50);
BufferedReader reader = new BufferedReader(new FileReader(file));
final String line1 = reader.readLine();
assertTrue(line1.contains(msg));
reader.close();
assertFalse("afterRollover-1.log not created yet", after1.exists());
String exceed = "Long message that exceeds rollover size... ";
final char[] padding = new char[250];
Arrays.fill(padding, 'X');
exceed += new String(padding);
log.warn(exceed);
assertFalse("exceeded size but afterRollover-1.log not created yet", after1.exists());
final String trigger = "This message triggers rollover.";
log.warn(trigger);
Thread.sleep(100);
log.warn(trigger);
// stop async thread
CoreLoggerContexts.stopLoggerContext();
// stop async thread
CoreLoggerContexts.stopLoggerContext(false);
final int MAX_ATTEMPTS = 50;
int count = 0;
while (!after1.exists() && count++ < MAX_ATTEMPTS) {
Thread.sleep(50);
}
assertTrue("afterRollover-1.log created", after1.exists());
reader = new BufferedReader(new FileReader(file));
final String new1 = reader.readLine();
assertTrue("after rollover only new msg", new1.contains(trigger));
assertNull("No more lines", reader.readLine());
reader.close();
file.delete();
reader = new BufferedReader(new FileReader(after1));
final String old1 = reader.readLine();
assertTrue("renamed file line 1", old1.contains(msg));
final String old2 = reader.readLine();
assertTrue("renamed file line 2", old2.contains(exceed));
String line = reader.readLine();
if (line != null) {
assertTrue("strange...", line.contains("This message triggers rollover."));
line = reader.readLine();
}
assertNull("No more lines", line);
reader.close();
after1.delete();
}
use of org.apache.logging.log4j.Logger in project logging-log4j2 by apache.
the class RollingAppenderCronTest method testAppender.
@Test
public void testAppender() throws Exception {
// TODO Is there a better way to test than putting the thread to sleep all over the place?
final Logger logger = loggerContextRule.getLogger();
final File file = new File(FILE);
assertTrue("Log file does not exist", file.exists());
logger.debug("This is test message number 1");
Thread.sleep(2500);
final File dir = new File(DIR);
assertTrue("Directory not created", dir.exists() && dir.listFiles().length > 0);
final int MAX_TRIES = 20;
final Matcher<File[]> hasGzippedFile = hasItemInArray(that(hasName(that(endsWith(".gz")))));
boolean succeeded = false;
for (int i = 0; i < MAX_TRIES; i++) {
final File[] files = dir.listFiles();
if (hasGzippedFile.matches(files)) {
succeeded = true;
break;
}
logger.debug("Sleeping #" + i);
// Allow time for rollover to complete
Thread.sleep(100);
}
if (!succeeded) {
final File[] files = dir.listFiles();
for (final File dirFile : files) {
logger.error("Found file: " + dirFile.getPath());
}
fail("No compressed files found");
}
final Path src = FileSystems.getDefault().getPath("target/test-classes/log4j-rolling-cron2.xml");
try (final OutputStream os = new FileOutputStream("target/test-classes/log4j-rolling-cron.xml")) {
Files.copy(src, os);
}
Thread.sleep(5000);
// force a reconfiguration
for (int i = 0; i < MAX_TRIES; ++i) {
logger.debug("Adding new event {}", i);
}
Thread.sleep(1000);
final RollingFileAppender app = (RollingFileAppender) loggerContextRule.getLoggerContext().getConfiguration().getAppender("RollingFile");
final TriggeringPolicy policy = app.getManager().getTriggeringPolicy();
assertNotNull("No triggering policy", policy);
assertTrue("Incorrect policy type", policy instanceof CronTriggeringPolicy);
final CronExpression expression = ((CronTriggeringPolicy) policy).getCronExpression();
assertEquals("Incorrect triggering policy", "* * * ? * *", expression.getCronExpression());
}
use of org.apache.logging.log4j.Logger in project logging-log4j2 by apache.
the class RollingAppenderDeleteAccumulatedSizeTest method testAppender.
@Test
public void testAppender() throws Exception {
final Logger logger = loggerContextRule.getLogger();
for (int i = 0; i < 10; ++i) {
// 30 chars per message: each message triggers a rollover
// 30 chars:
logger.debug("This is a test message number " + i);
}
// Allow time for rollover to complete
Thread.sleep(100);
final File dir = new File(DIR);
assertTrue("Dir " + DIR + " should exist", dir.exists());
assertTrue("Dir " + DIR + " should contain files", dir.listFiles().length > 0);
final File[] files = dir.listFiles();
for (final File file : files) {
System.out.println(file + " (" + file.length() + "B) " + FixedDateFormat.create(FixedFormat.ABSOLUTE).format(file.lastModified()));
}
assertEquals(Arrays.toString(files), 4, files.length);
long total = 0;
for (final File file : files) {
// sometimes test-6.log remains
assertTrue("unexpected file " + file, file.getName().startsWith("test-"));
total += file.length();
}
assertTrue("accumulatedSize=" + total, total <= 500);
}
Aggregations