use of org.apache.logging.log4j.core.appender.RollingFileAppender in project cas by apereo.
the class LoggingConfigController method getConfiguration.
/**
* Gets configuration as JSON.
* Depends on the log4j core API.
*
* @param request the request
* @param response the response
* @return the configuration
* @throws Exception the exception
*/
@GetMapping(value = "/getConfiguration")
@ResponseBody
public Map<String, Object> getConfiguration(final HttpServletRequest request, final HttpServletResponse response) throws Exception {
ensureEndpointAccessIsAuthorized(request, response);
Assert.notNull(this.loggerContext);
final Collection<Map<String, Object>> configuredLoggers = new HashSet<>();
getLoggerConfigurations().forEach(config -> {
final Map<String, Object> loggerMap = new HashMap<>();
loggerMap.put("name", StringUtils.defaultIfBlank(config.getName(), LOGGER_NAME_ROOT));
loggerMap.put("state", config.getState());
if (config.getProperties() != null) {
loggerMap.put("properties", config.getProperties());
}
loggerMap.put("additive", config.isAdditive());
loggerMap.put("level", config.getLevel().name());
final Collection<String> appenders = new HashSet<>();
config.getAppenders().keySet().stream().map(key -> config.getAppenders().get(key)).forEach(appender -> {
final ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.JSON_STYLE);
builder.append("name", appender.getName());
builder.append("state", appender.getState());
builder.append("layoutFormat", appender.getLayout().getContentFormat());
builder.append("layoutContentType", appender.getLayout().getContentType());
if (appender instanceof FileAppender) {
builder.append(FILE_PARAM, ((FileAppender) appender).getFileName());
builder.append(FILE_PATTERN_PARAM, "(none)");
}
if (appender instanceof RandomAccessFileAppender) {
builder.append(FILE_PARAM, ((RandomAccessFileAppender) appender).getFileName());
builder.append(FILE_PATTERN_PARAM, "(none)");
}
if (appender instanceof RollingFileAppender) {
builder.append(FILE_PARAM, ((RollingFileAppender) appender).getFileName());
builder.append(FILE_PATTERN_PARAM, ((RollingFileAppender) appender).getFilePattern());
}
if (appender instanceof MemoryMappedFileAppender) {
builder.append(FILE_PARAM, ((MemoryMappedFileAppender) appender).getFileName());
builder.append(FILE_PATTERN_PARAM, "(none)");
}
if (appender instanceof RollingRandomAccessFileAppender) {
builder.append(FILE_PARAM, ((RollingRandomAccessFileAppender) appender).getFileName());
builder.append(FILE_PATTERN_PARAM, ((RollingRandomAccessFileAppender) appender).getFilePattern());
}
appenders.add(builder.build());
});
loggerMap.put("appenders", appenders);
configuredLoggers.add(loggerMap);
});
final Map<String, Object> responseMap = new HashMap<>();
responseMap.put("loggers", configuredLoggers);
return responseMap;
}
use of org.apache.logging.log4j.core.appender.RollingFileAppender in project logging-log4j2 by apache.
the class RollingFileAppenderAccessTest method testAccessManagerWithBuilder.
/**
* Not a real test, just make sure we can compile access to the typed manager.
*
* @throws IOException
*/
@Test
public void testAccessManagerWithBuilder() throws IOException {
try (final LoggerContext ctx = LoggerContext.getContext(false)) {
final Configuration config = ctx.getConfiguration();
final File file = File.createTempFile("RollingFileAppenderAccessTest", ".tmp");
file.deleteOnExit();
// @formatter:off
final RollingFileAppender appender = RollingFileAppender.newBuilder().withFileName(file.getCanonicalPath()).withFilePattern("FilePattern").withName("Name").withPolicy(OnStartupTriggeringPolicy.createPolicy(1)).setConfiguration(config).build();
// @formatter:on
final RollingFileManager manager = appender.getManager();
// Since the RolloverStrategy and TriggeringPolicy are immutable, we could also use generics to type their
// access.
Assert.assertNotNull(manager.getRolloverStrategy());
Assert.assertNotNull(manager.getTriggeringPolicy());
}
}
use of org.apache.logging.log4j.core.appender.RollingFileAppender in project logging-log4j2 by apache.
the class RollingAppenderSizeTest method testIsCreateOnDemand.
@Test
public void testIsCreateOnDemand() {
final RollingFileAppender rfAppender = loggerContextRule.getRequiredAppender("RollingFile", RollingFileAppender.class);
final RollingFileManager manager = rfAppender.getManager();
Assert.assertNotNull(manager);
Assert.assertEquals(createOnDemand, manager.isCreateOnDemand());
}
use of org.apache.logging.log4j.core.appender.RollingFileAppender in project logging-log4j2 by apache.
the class CronTriggeringPolicyTest method testBuilder.
private void testBuilder() {
// @formatter:off
final RollingFileAppender raf = RollingFileAppender.newBuilder().withName("test1").withFileName("target/testcmd1.log").withFilePattern("target/testcmd1.log.%d{yyyy-MM-dd}").withPolicy(createPolicy()).withStrategy(createStrategy()).setConfiguration(configuration).build();
// @formatter:on
Assert.assertNotNull(raf);
}
use of org.apache.logging.log4j.core.appender.RollingFileAppender 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);
}
Aggregations