Skip to content

Commit

Permalink
feat(TeeFilter): Remove printing stacktraces in the filter
Browse files Browse the repository at this point in the history
The filter should not print the Stacktrace and then rethrow the exceptions.
Other handlers will handle the Servlet Exceptions anyways.

Additionally allow users to overwrite the logging output method in the filter.

Signed-off-by: flx5 <[email protected]>
  • Loading branch information
flx5 committed Apr 18, 2024
1 parent 40fada8 commit bbff896
Showing 1 changed file with 30 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,20 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
throws IOException, ServletException {

if (active && request instanceof HttpServletRequest) {
try {
TeeHttpServletRequest teeRequest = new TeeHttpServletRequest((HttpServletRequest) request);
TeeHttpServletResponse teeResponse = new TeeHttpServletResponse((HttpServletResponse) response);
TeeHttpServletRequest teeRequest = new TeeHttpServletRequest((HttpServletRequest) request);
TeeHttpServletResponse teeResponse = new TeeHttpServletResponse((HttpServletResponse) response);

// System.out.println("BEFORE TeeFilter. filterChain.doFilter()");
try {
filterChain.doFilter(teeRequest, teeResponse);
// System.out.println("AFTER TeeFilter. filterChain.doFilter()");

} finally {
teeResponse.finish();
// let the output contents be available for later use by
// logback-access-logging
teeRequest.setAttribute(AccessConstants.LB_OUTPUT_BUFFER, teeResponse.getOutputBuffer());
} catch (IOException e) {
e.printStackTrace();
throw e;
} catch (ServletException e) {
e.printStackTrace();
throw e;
}
} else {
filterChain.doFilter(request, response);
}

}

@Override
Expand All @@ -76,9 +67,9 @@ public void init(FilterConfig filterConfig) throws ServletException {

active = computeActivation(localhostName, includeListAsStr, excludeListAsStr);
if (active)
System.out.println("TeeFilter will be ACTIVE on this host [" + localhostName + "]");
logInfo("TeeFilter will be ACTIVE on this host [" + localhostName + "]");
else
System.out.println("TeeFilter will be DISABLED on this host [" + localhostName + "]");
logInfo("TeeFilter will be DISABLED on this host [" + localhostName + "]");

}

Expand All @@ -101,13 +92,13 @@ public static List<String> extractNameList(String nameListAsStr) {
return nameList;
}

static String getLocalhostName() {
String getLocalhostName() {
String hostname = "127.0.0.1";

try {
hostname = InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException uhe) {
uhe.printStackTrace();
logWarn("Unknown host", uhe);
}
return hostname;
}
Expand All @@ -132,4 +123,26 @@ static boolean mathesExcludesList(String hostname, List<String> excludesList) {
return excludesList.contains(hostname);
}

/**
* Log a warning.
*
* Can be overwritten to use a logger.
*
* @param msg The message.
* @param ex The exception.
*/
protected void logWarn(String msg, Throwable ex) {
System.err.println(msg + ": " + ex);
}

/**
* Log an info message.
*
* Can be overwritten to use a logger.
*
* @param msg The message to log.
*/
protected void logInfo(String msg) {
System.out.println(msg);
}
}

0 comments on commit bbff896

Please sign in to comment.