use of org.apache.geode.internal.logging.PureLogWriter in project geode by apache.
the class LogWriterAppenderJUnitTest method testLogOutput.
/**
* Verifies that writing to a Log4j logger will end up in the LogWriter's output.
*/
@Test
public void testLogOutput() throws IOException {
// Create the appender
final StringWriter stringWriter = new StringWriter();
final PureLogWriter logWriter = new PureLogWriter(InternalLogWriter.FINEST_LEVEL, new PrintWriter(stringWriter), "");
final AppenderContext[] contexts = new AppenderContext[2];
// root context
contexts[0] = LogService.getAppenderContext();
// "org.apache"
contexts[1] = LogService.getAppenderContext(LogService.BASE_LOGGER_NAME);
// context
this.appender = LogWriterAppender.create(contexts, LogService.MAIN_LOGGER_NAME, logWriter, null);
final Logger logger = LogService.getLogger();
// set the level to TRACE
Configurator.setLevel(LogService.BASE_LOGGER_NAME, Level.TRACE);
Configurator.setLevel(LogService.MAIN_LOGGER_NAME, Level.TRACE);
assertEquals(Level.TRACE, logger.getLevel());
logger.trace("TRACE MESSAGE");
assertTrue(Pattern.compile(".*\\[finest .*TRACE MESSAGE.*", Pattern.DOTALL).matcher(stringWriter.toString()).matches());
stringWriter.getBuffer().setLength(0);
logger.debug("DEBUG MESSAGE");
assertTrue(Pattern.compile(".*\\[fine .*DEBUG MESSAGE.*", Pattern.DOTALL).matcher(stringWriter.toString()).matches());
stringWriter.getBuffer().setLength(0);
logger.info("INFO MESSAGE");
assertTrue(Pattern.compile(".*\\[info .*INFO MESSAGE.*", Pattern.DOTALL).matcher(stringWriter.toString()).matches());
stringWriter.getBuffer().setLength(0);
logger.warn("ExpectedStrings: WARNING MESSAGE");
assertTrue(Pattern.compile(".*\\[warning .*WARNING MESSAGE.*", Pattern.DOTALL).matcher(stringWriter.toString()).matches());
stringWriter.getBuffer().setLength(0);
logger.error("ExpectedStrings: ERROR MESSAGE");
assertTrue(Pattern.compile(".*\\[error .*ERROR MESSAGE.*", Pattern.DOTALL).matcher(stringWriter.toString()).matches());
stringWriter.getBuffer().setLength(0);
logger.fatal("ExpectedStrings: FATAL MESSAGE");
assertTrue(Pattern.compile(".*\\[severe .*FATAL MESSAGE.*", Pattern.DOTALL).matcher(stringWriter.toString()).matches());
stringWriter.getBuffer().setLength(0);
final Logger lowerLevelLogger = LogService.getLogger(LogService.BASE_LOGGER_NAME + ".subpackage");
lowerLevelLogger.fatal("ExpectedStrings: FATAL MESSAGE");
assertTrue(Pattern.compile(".*\\[severe .*FATAL MESSAGE.*", Pattern.DOTALL).matcher(stringWriter.toString()).matches());
stringWriter.getBuffer().setLength(0);
this.appender.destroy();
assertFalse(Configurator.getLoggerConfig(LogService.BASE_LOGGER_NAME).getAppenders().containsKey(this.appender.getName()));
}
Aggregations