Search in sources :

Example 11 with RollingFileAppender

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;
}
Also used : LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) RollingRandomAccessFileAppender(org.apache.logging.log4j.core.appender.RollingRandomAccessFileAppender) RequestParam(org.springframework.web.bind.annotation.RequestParam) LoggerContext(org.apache.logging.log4j.core.LoggerContext) LoggerFactory(org.slf4j.LoggerFactory) ToStringStyle(org.apache.commons.lang3.builder.ToStringStyle) Level(org.apache.logging.log4j.Level) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) AuditActionContext(org.apereo.inspektr.audit.AuditActionContext) StringUtils(org.apache.commons.lang3.StringUtils) Configuration(org.apache.logging.log4j.core.config.Configuration) HashSet(java.util.HashSet) HttpServletRequest(javax.servlet.http.HttpServletRequest) Pair(org.apache.commons.lang3.tuple.Pair) Map(java.util.Map) GetMapping(org.springframework.web.bind.annotation.GetMapping) Log4jLoggerFactory(org.apache.logging.slf4j.Log4jLoggerFactory) Resource(org.springframework.core.io.Resource) PostMapping(org.springframework.web.bind.annotation.PostMapping) Logger(org.slf4j.Logger) ResourceLoader(org.springframework.core.io.ResourceLoader) Collection(java.util.Collection) HttpServletResponse(javax.servlet.http.HttpServletResponse) Set(java.util.Set) IOException(java.io.IOException) DelegatingAuditTrailManager(org.apereo.cas.audit.spi.DelegatingAuditTrailManager) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) ControllerUtils(org.apereo.cas.web.report.util.ControllerUtils) MemoryMappedFileAppender(org.apache.logging.log4j.core.appender.MemoryMappedFileAppender) ModelAndView(org.springframework.web.servlet.ModelAndView) FileAppender(org.apache.logging.log4j.core.appender.FileAppender) Environment(org.springframework.core.env.Environment) ToStringBuilder(org.apache.commons.lang3.builder.ToStringBuilder) PostConstruct(javax.annotation.PostConstruct) RandomAccessFileAppender(org.apache.logging.log4j.core.appender.RandomAccessFileAppender) ILoggerFactory(org.slf4j.ILoggerFactory) RollingFileAppender(org.apache.logging.log4j.core.appender.RollingFileAppender) Assert(org.springframework.util.Assert) RollingRandomAccessFileAppender(org.apache.logging.log4j.core.appender.RollingRandomAccessFileAppender) MemoryMappedFileAppender(org.apache.logging.log4j.core.appender.MemoryMappedFileAppender) FileAppender(org.apache.logging.log4j.core.appender.FileAppender) RandomAccessFileAppender(org.apache.logging.log4j.core.appender.RandomAccessFileAppender) RollingFileAppender(org.apache.logging.log4j.core.appender.RollingFileAppender) RollingRandomAccessFileAppender(org.apache.logging.log4j.core.appender.RollingRandomAccessFileAppender) RandomAccessFileAppender(org.apache.logging.log4j.core.appender.RandomAccessFileAppender) HashMap(java.util.HashMap) RollingFileAppender(org.apache.logging.log4j.core.appender.RollingFileAppender) MemoryMappedFileAppender(org.apache.logging.log4j.core.appender.MemoryMappedFileAppender) ToStringBuilder(org.apache.commons.lang3.builder.ToStringBuilder) HashMap(java.util.HashMap) Map(java.util.Map) RollingRandomAccessFileAppender(org.apache.logging.log4j.core.appender.RollingRandomAccessFileAppender) HashSet(java.util.HashSet) GetMapping(org.springframework.web.bind.annotation.GetMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 12 with RollingFileAppender

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());
    }
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) RollingFileAppender(org.apache.logging.log4j.core.appender.RollingFileAppender) LoggerContext(org.apache.logging.log4j.core.LoggerContext) File(java.io.File) Test(org.junit.Test)

Example 13 with RollingFileAppender

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());
}
Also used : RollingFileAppender(org.apache.logging.log4j.core.appender.RollingFileAppender) Test(org.junit.Test)

Example 14 with RollingFileAppender

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);
}
Also used : RollingFileAppender(org.apache.logging.log4j.core.appender.RollingFileAppender)

Example 15 with RollingFileAppender

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);
}
Also used : RollingFileAppender(org.apache.logging.log4j.core.appender.RollingFileAppender) CronExpression(org.apache.logging.log4j.core.util.CronExpression) Logger(org.apache.logging.log4j.Logger) StatusLogger(org.apache.logging.log4j.status.StatusLogger) File(java.io.File) Test(org.junit.Test)

Aggregations

RollingFileAppender (org.apache.logging.log4j.core.appender.RollingFileAppender)17 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)8 Test (org.junit.Test)8 Appender (org.apache.logging.log4j.core.Appender)7 Configuration (org.apache.logging.log4j.core.config.Configuration)7 File (java.io.File)6 IOException (java.io.IOException)3 LoggerContext (org.apache.logging.log4j.core.LoggerContext)3 ConsoleAppender (org.apache.logging.log4j.core.appender.ConsoleAppender)3 CompositeTriggeringPolicy (org.apache.logging.log4j.core.appender.rolling.CompositeTriggeringPolicy)3 SizeBasedTriggeringPolicy (org.apache.logging.log4j.core.appender.rolling.SizeBasedTriggeringPolicy)3 TimeBasedTriggeringPolicy (org.apache.logging.log4j.core.appender.rolling.TimeBasedTriggeringPolicy)3 TriggeringPolicy (org.apache.logging.log4j.core.appender.rolling.TriggeringPolicy)3 Path (java.nio.file.Path)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 FileSystem (org.apache.hadoop.fs.FileSystem)2 Path (org.apache.hadoop.fs.Path)2 DriverContext (org.apache.hadoop.hive.ql.DriverContext)2 LogHelper (org.apache.hadoop.hive.ql.session.SessionState.LogHelper)2