Class BasicTestCaptureCallLogger

java.lang.Object
org.apache.juneau.rest.logger.CallLogger
org.apache.juneau.rest.logger.BasicTestCaptureCallLogger

Implementation of a CallLogger that captures log entries for testing logging itself.

Instead of logging messages to a log file, messages are simply kept in an internal atomic string reference. Once a message has been logged, you can use the getMessage() or assertMessage() methods to access and evaluate it.

The following example shows how the capture logger can be associated with a REST class so that logged messages can be examined.

public class MyTests { private static final CaptureLogger LOGGER = new CaptureLogger(); public static class CaptureLogger extends BasicTestCaptureCallLogger { // How our REST class will get the logger. public static CaptureLogger getInstance() { return LOGGER; } } @Rest(callLogger=CaptureLogger.class) public static class TestRest implements BasicRestServlet { @RestGet public boolean bad() throws InternalServerError { throw new InternalServerError("foo"); } } @Test public void testBadRequestLogging() throws Exception { // Create client that won't throw exceptions. RestClient client = MockRestClient.create(TestRest.class).ignoreErrors().build(); // Make the REST call. client.get("/bad").run().assertStatusCode(500).assertContent().contains("foo"); // Make sure the message was logged in our expected format. LOGGER.assertMessageAndReset().contains("[500] HTTP GET /bad"); } }

See Also:
  • Constructor Details

  • Method Details

    • init

      protected CallLogger.Builder init(BeanStore beanStore)
      Description copied from class: CallLogger
      Initializer.

      Subclasses should override this method to make modifications to the builder used to create this logger.

      Overrides:
      init in class CallLogger
      Parameters:
      beanStore - The bean store containing injectable beans for this logger.
      Returns:
      A new builder object.
    • log

      protected void log(Level level, String msg, Throwable e)
      Description copied from class: CallLogger
      Logs the specified message to the logger.

      Subclasses can override this method to capture messages being sent to the logger and handle it differently.

      Overrides:
      log in class CallLogger
      Parameters:
      level - The log level.
      msg - The log message.
      e - The exception.
    • getMessage

      public String getMessage()
      Returns the last logged message.
      Returns:
      The last logged message, or null if nothing was logged.
    • getMessageAndReset

      Returns the last logged message and then deletes it internally.
      Returns:
      The last logged message.
    • assertMessage

      Returns an assertion of the last logged message.
      Returns:
      The last logged message as an assertion object. Never null.
    • assertMessageAndReset

      Returns an assertion of the last logged message and then deletes it internally.
      Returns:
      The last logged message as an assertion object. Never null.
    • getLevel

      public Level getLevel()
      Returns the last logged message level.
      Returns:
      The last logged message level, or null if nothing was logged.
    • getThrown

      public Throwable getThrown()
      Returns the last logged message level.
      Returns:
      The last logged message level, or null if nothing was logged.
    • assertThrown

      Returns the last logged message level.
      Returns:
      The last logged message level, or null if nothing was logged.
    • reset

      Resets the internal message buffer.
      Returns:
      This object.