use of org.apache.logging.log4j.core.util.CronExpression 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();
assertTrue("Incorrect triggering policy", expression.getCronExpression().equals("* * * ? * *"));
}
use of org.apache.logging.log4j.core.util.CronExpression in project logging-log4j2 by apache.
the class RollingAppenderCronOnceADayTest 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, waiting for rolling");
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 cron expresion", cronExpression, expression.getCronExpression());
logger.debug("Cron expression will be {}", expression.getCronExpression());
// force a reconfiguration
for (int i = 1; i <= 20; ++i) {
logger.debug("Adding first event {}", i);
}
Thread.sleep(remainingTime);
final File dir = new File(DIR);
assertTrue("Directory not created", dir.exists() && dir.listFiles().length > 0);
for (int i = 1; i < 5; i++) {
logger.debug("Adding some more event {}", i);
Thread.sleep(1000);
}
final Matcher<File> hasGzippedFile = hasName(that(endsWith(".gz")));
int count = 0;
final File[] files = dir.listFiles();
for (final File generatedFile : files) {
if (hasGzippedFile.matches(generatedFile)) {
count++;
}
}
assertNotEquals("No compressed files found", 0, count);
assertEquals("Multiple files found", 1, count);
}
use of org.apache.logging.log4j.core.util.CronExpression in project logging-log4j2 by apache.
the class CronTriggeringPolicy method createPolicy.
/**
* Creates a ScheduledTriggeringPolicy.
*
* @param configuration
* the Configuration.
* @param evaluateOnStartup
* check if the file should be rolled over immediately.
* @param schedule
* the cron expression.
* @return a ScheduledTriggeringPolicy.
*/
@PluginFactory
public static CronTriggeringPolicy createPolicy(@PluginConfiguration final Configuration configuration, @PluginAttribute("evaluateOnStartup") final String evaluateOnStartup, @PluginAttribute("schedule") final String schedule) {
CronExpression cronExpression;
final boolean checkOnStartup = Boolean.parseBoolean(evaluateOnStartup);
if (schedule == null) {
LOGGER.info("No schedule specified, defaulting to Daily");
cronExpression = getSchedule(defaultSchedule);
} else {
cronExpression = getSchedule(schedule);
if (cronExpression == null) {
LOGGER.error("Invalid expression specified. Defaulting to Daily");
cronExpression = getSchedule(defaultSchedule);
}
}
return new CronTriggeringPolicy(cronExpression, checkOnStartup, configuration);
}
Aggregations