Class CallLogger

java.lang.Object
org.apache.juneau.rest.logger.CallLogger
Direct Known Subclasses:
BasicCallLogger, BasicDisabledCallLogger, BasicTestCallLogger, BasicTestCaptureCallLogger, CallLogger.Void

public class CallLogger extends Object
Basic implementation of a CallLogger for logging HTTP requests.

Provides the following capabilities:

  • Allows incoming HTTP requests to be logged at various detail levels.
  • Allows rules to be defined to handle request logging differently depending on the resulting status code.
  • Allows use of stack trace hashing to eliminate duplication of stack traces in log files.
  • Allows customization of handling of where requests are logged to.
  • Allows configuration via system properties or environment variables.

The following is an example of a logger that logs errors only when debugging is not enabled, and everything when logging is enabled.

Example:

CallLogger logger = CallLogger .create() .logger("MyLogger") // Use MyLogger Java logger. .normalRules( // Rules when debugging is not enabled. createRule() // Log 500+ errors with status-line and header information. .statusFilter(x -> x >= 500) .level(SEVERE) .requestDetail(HEADER) .responseDetail(HEADER) .build(), createRule() // Log 400-500 errors with just status-line information. .statusFilter(x -> x >= 400) .level(WARNING) .requestDetail(STATUS_LINE) .responseDetail(STATUS_LINE) .build() ) .debugRules( // Rules when debugging is enabled. createRule() // Log everything with full details. .level(SEVERE) .requestDetail(ENTITY) .responseDetail(ENTITY) .build() ) .build() ;

See Also:
  • Field Details

  • Constructor Details

    • CallLogger

      public CallLogger(BeanStore beanStore)
      Constructor.

      Subclasses typically override the init(BeanStore) method when using this constructor.

      Parameters:
      beanStore - The bean store containing injectable beans for this logger.
    • CallLogger

      public CallLogger(CallLogger.Builder builder)
      Constructor.
      Parameters:
      builder - The builder for this logger.
  • Method Details

    • create

      public static CallLogger.Builder create(BeanStore beanStore)
      Static creator.
      Parameters:
      beanStore - The bean store to use for creating beans.
      Returns:
      A new builder for this object.
    • init

      protected CallLogger.Builder init(BeanStore beanStore)
      Initializer.

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

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

      public void log(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse res)
      Called at the end of a servlet request to log the request.
      Parameters:
      req - The servlet request.
      res - The servlet response.
    • getRule

      protected CallLoggerRule getRule(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse res)
      Given the specified servlet request/response, find the rule that applies to it.

      This method can be overridden to provide specialized logic for finding rules.

      Parameters:
      req - The servlet request.
      res - The servlet response.
      Returns:
      The applicable logging rule, or the default rule if not found. Never null.
    • isDebug

      protected boolean isDebug(jakarta.servlet.http.HttpServletRequest req)
      Returns true if debug is enabled on this request.

      Looks for the request attribute "Debug" to determine whether debug is enabled.

      This method can be overridden to provide specialized logic for determining whether debug mode is enabled on a request.

      Parameters:
      req - The HTTP request being logged.
      Returns:
      true if debug is enabled on this request.
      See Also:
    • isEnabled

      protected boolean isEnabled(CallLoggerRule rule, jakarta.servlet.http.HttpServletRequest req)
      Returns true if logging is enabled for this request.

      Uses the enabled and enabled-test settings on the matched rule and this logger to determine whether a REST call should be logged.

      This method can be overridden to provide specialized logic for determining whether a REST call should be logged.

      Parameters:
      rule - The first matching rule. Never null.
      req - The HTTP request.
      Returns:
      true if logging is enabled for this request.
    • getLogger

      protected Logger getLogger()
      Returns the logger to use for logging REST calls.

      Returns the logger specified in the builder, or Logger.getGlobal() if it wasn't specified.

      This method can be overridden in subclasses to provide a different logger.

      Returns:
      The logger to use for logging REST calls.
    • log

      protected void log(Level level, String msg, Throwable e)
      Logs the specified message to the logger.

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

      Parameters:
      level - The log level.
      msg - The log message.
      e - The exception.
    • toString

      public String toString()
      Overrides:
      toString in class Object