Search in sources :

Example 21 with FileAppender

use of org.apache.logging.log4j.core.appender.FileAppender 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));
}
Also used : FileAppender(org.apache.logging.log4j.core.appender.FileAppender) XmlConfiguration(org.apache.logging.log4j.core.config.xml.XmlConfiguration) StatusConsoleListener(org.apache.logging.log4j.status.StatusConsoleListener) XmlConfiguration(org.apache.logging.log4j.core.config.xml.XmlConfiguration) StatusListener(org.apache.logging.log4j.status.StatusListener) Logger(org.apache.logging.log4j.Logger) StatusLogger(org.apache.logging.log4j.status.StatusLogger) Test(org.junit.jupiter.api.Test) LoggerContextSource(org.apache.logging.log4j.core.test.junit.LoggerContextSource)

Example 22 with FileAppender

use of org.apache.logging.log4j.core.appender.FileAppender in project logging-log4j2 by apache.

the class PropertiesConfigurationTest method testSystemProperties.

@Test
public void testSystemProperties() throws Exception {
    final String testPathLocation = "target";
    System.setProperty(TEST_KEY, testPathLocation);
    try (LoggerContext loggerContext = TestConfigurator.configure("target/test-classes/config-1.2/log4j-FileAppender-with-props.properties")) {
        // [LOG4J2-3312] Bridge does not convert properties.
        final Configuration configuration = loggerContext.getConfiguration();
        assertNotNull(configuration);
        final String name = "FILE_APPENDER";
        final Appender appender = configuration.getAppender(name);
        assertNotNull(name, appender);
        assertTrue(appender.getClass().getName(), appender instanceof FileAppender);
        final FileAppender fileAppender = (FileAppender) appender;
        // Two slashes because that's how the config file is setup.
        assertEquals(testPathLocation + "/hadoop.log", fileAppender.getFileName());
    } finally {
        System.clearProperty(TEST_KEY);
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ListAppender(org.apache.log4j.ListAppender) FileAppender(org.apache.logging.log4j.core.appender.FileAppender) FileAppender(org.apache.logging.log4j.core.appender.FileAppender) Configuration(org.apache.logging.log4j.core.config.Configuration) LoggerContext(org.apache.logging.log4j.core.LoggerContext) Test(org.junit.Test)

Example 23 with FileAppender

use of org.apache.logging.log4j.core.appender.FileAppender in project tokentool by RPTools.

the class TokenTool method getLoggerFileName.

public static String getLoggerFileName() {
    org.apache.logging.log4j.core.Logger loggerImpl = (org.apache.logging.log4j.core.Logger) log;
    Appender appender = loggerImpl.getAppenders().get("LogFile");
    if (appender != null)
        return ((FileAppender) appender).getFileName();
    else
        return "NOT_CONFIGURED";
}
Also used : Appender(org.apache.logging.log4j.core.Appender) FileAppender(org.apache.logging.log4j.core.appender.FileAppender) Logger(org.apache.logging.log4j.Logger)

Aggregations

FileAppender (org.apache.logging.log4j.core.appender.FileAppender)23 RollingFileAppender (org.apache.logging.log4j.core.appender.RollingFileAppender)12 Appender (org.apache.logging.log4j.core.Appender)11 LoggerContext (org.apache.logging.log4j.core.LoggerContext)8 Configuration (org.apache.logging.log4j.core.config.Configuration)8 RollingRandomAccessFileAppender (org.apache.logging.log4j.core.appender.RollingRandomAccessFileAppender)6 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)6 File (java.io.File)5 MemoryMappedFileAppender (org.apache.logging.log4j.core.appender.MemoryMappedFileAppender)5 RandomAccessFileAppender (org.apache.logging.log4j.core.appender.RandomAccessFileAppender)5 Test (org.junit.Test)5 HashSet (java.util.HashSet)4 Logger (org.slf4j.Logger)4 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Optional (java.util.Optional)2 Set (java.util.Set)2 Slf4j (lombok.extern.slf4j.Slf4j)2 Tailer (org.apache.commons.io.input.Tailer)2