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));
}
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);
}
}
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";
}
Aggregations