Class UsageLog

java.lang.Object
thredds.servlet.UsageLog

public class UsageLog extends Object
Utility class for gathering context information for use in log messages. Includes methods appropriate when the context thread is an individual HTTP request and when the context thread is an initialization thread. The context information is contained in a key/value map.

Uses the SLF4J MDC framework (see @link{org.slf4j.MDC} for more details).

If properly configured, each log entry within the context thread will include the gathered context information. For instance, in log4j and slf4j, the appender pattern would contain strings with the form "%X{<contextKey>}", where "<contextKey>" is a context key value. The context key strings are given in each setup method below.

Since:
Jan 9, 2009
See Also:
  • MDC
  • Constructor Details

    • UsageLog

      public UsageLog()
  • Method Details

    • setupRequestContext

      public static String setupRequestContext(jakarta.servlet.http.HttpServletRequest req)
      Gather context information for the given HTTP request and return a log message appropriate for logging at the start of the request.

      The following context information is gathered:

      • "ID" - an identifier for the current thread;
      • "host" - the remote host (IP address or host name);
      • "userid" - the id of the remote user;
      • "startTime" - the system time in millis when this request is started (i.e., when this method is called); and
      • "request" - The HTTP request, e.g., "GET /index.html HTTP/1.1".

      Call this method at the start of each HttpServlet doXXX() method (e.g., doGet(), doPut()) or Spring MVC Controller handle() method.

      Parameters:
      req - the current request
      Returns:
      a log message appropriate for the start of the request.
    • closingMessageForRequestContext

      public static String closingMessageForRequestContext(int resCode, long resSizeInBytes)
      Return a log message appropriate for logging at the completion of the contexts HTTP request.

      Call this method at every exit point in each HttpServlet doXXX() method (e.g., doGet(), doPut()) or Spring MVC Controller handle() method.

      Parameters:
      resCode - - the result code for this request.
      resSizeInBytes - - the number of bytes returned in this result, -1 if unknown.
      Returns:
      closing log message
    • setupNonRequestContext

      public static String setupNonRequestContext()
      Gather context information for the current non-request thread and return a log message appropriate for logging.

      The following context information is gathered:

      • "ID" - an identifier for the current thread; and
      • "startTime" - the system time in millis when this method is called.

      Call this method only for non-request servlet activities, e.g., during init() or destroy().

      Returns:
      starting log message
    • closingMessageNonRequestContext

      public static String closingMessageNonRequestContext()
      Return a log message appropriate for logging at the close of the non-request context.
      Returns:
      a log message appropriate for logging at the close of the non-request context.
    • add2map

      public void add2map(String key, String value)