Logging functions for the test framework

The framework provides functions that support logging. The logging functions are based on the org.slf4j.Logger object. Refer to the SLF4J (Simple Logging Facade for Java) documentation for details.

Method: addLoggingAppender

addLoggingAppender(logger : Logger, appender : LogAppender) : void

The addLoggingAppender method adds a logging output destination to a Logger object.

The logger argument references the Logger object to receive the new output destination. The appender argument specifies the new logging output destination. The LogAppender object is based on Log4j. Refer to the Log4J documentation for details on using an appender to define a new output destination.

Method: getLogger

getLogger() : Logger

The getLogger method returns the org.slf4j.Logger object used by the test framework for logging.

Method: setLoggerLevel

setLoggerLevel(logger : Logger, level : LogLevel) : void

The setLoggerLevel method sets the types of messages to log.

The logger argument specifies the Logger object used by the test framework. The object can be retrieved by the getLogger method.

The level argument specifies the log level. The following hierarchical levels are supported. Each level includes the levels below it. For example, the INFO level will log messages of the types INFO, WARN, and ERROR.

  • ALL - Log all message types.
  • TRACE - Verbose logging mode.
  • DEBUG - Log debugging messages.
  • INFO - Log informational messages about executing operations. INFO is the default log level.
  • WARN - Log potential problems.
  • ERROR - Log error conditions.
  • OFF - Disable logging.
uses gw.logging.LogLevel

setLoggerLevel(getLogger(), LogLevel.DEBUG)

A test can log a message by calling the Logger method related to the relevant log level. Refer to the Logger documentation of the SLF4J documentation for details.

class MySampleServerTest extends PCServerTestClassBase {

  function testMySampleServerTest() {
    Logger.info("Running sample server test")       // Log a message at the INFO level

    // ... Perform test

    // ... Log messages at other levels
    Logger.debug("Debug information")               // Log a message at the DEBUG level
    Logger.error("Error information")               // Log a message at the ERROR level
    Logger.trace("Details about the test")          // Log a message at the TRACE level
    Logger.warn("Potential issue in the test")      // Log a message at the WARN level
  }
}

Method: startLogCapture

startLogCapture(logger : Logger) : CapturingLogger

The startLogCapture method creates a new logging object that captures subsequent logging events.

The method returns a CapturingLogger object.

uses gw.testharness.CapturingLogger

class MySampleServerTest extends PCServerTestClassBase {

  function testMySampleServerTest() {
    var logger : CapturingLogger

    try {
      logger = startLogCapture(getLogger())         // Start capturing the test logger

      assertThat(logger.getCapturedEvents())        // Verify that log is initially empty
            .as("Log should be initially empty")
            .isEmpty()

      // ... Perform test here

      assertThat(logger.getCapturedEvents())        // Verify that log is no longer empty
            .as("Should have logged messages")
            .isNotEmpty()
  }
}