Search in sources :

Example 6 with PureLogWriter

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()));
}
Also used : StringWriter(java.io.StringWriter) PureLogWriter(org.apache.geode.internal.logging.PureLogWriter) Logger(org.apache.logging.log4j.Logger) PrintWriter(java.io.PrintWriter) Test(org.junit.Test) UnitTest(org.apache.geode.test.junit.categories.UnitTest)

Aggregations

PureLogWriter (org.apache.geode.internal.logging.PureLogWriter)6 PrintWriter (java.io.PrintWriter)3 StringWriter (java.io.StringWriter)3 UnitTest (org.apache.geode.test.junit.categories.UnitTest)3 Test (org.junit.Test)3 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 PrintStream (java.io.PrintStream)1 GemFireIOException (org.apache.geode.GemFireIOException)1 Logger (org.apache.logging.log4j.Logger)1 LogEvent (org.apache.logging.log4j.core.LogEvent)1 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)1 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)1