Class UsageLog
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidstatic StringclosingMessageForRequestContext(int resCode, long resSizeInBytes) Return a log message appropriate for logging at the completion of the contexts HTTP request.static StringReturn a log message appropriate for logging at the close of the non-request context.static StringGather context information for the current non-request thread and return a log message appropriate for logging.static StringsetupRequestContext(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.
-
Constructor Details
-
UsageLog
public UsageLog()
-
-
Method Details
-
setupRequestContext
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
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
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
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
-