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 cff41f6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 18 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);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package ch.qos.logback.access.tomcat_11_0;

import ch.qos.logback.access.common.servlet.TeeFilter;
import ch.qos.logback.access.common.spi.IAccessEvent;
import ch.qos.logback.access.tomcat.LogbackValve;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.Server;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.startup.Tomcat;
import org.apache.tomcat.util.descriptor.web.FilterDef;
import org.apache.tomcat.util.descriptor.web.FilterMap;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -31,7 +34,6 @@ public class EmbeddedTomcatTest {

@BeforeEach
public void embed() throws LifecycleException {

tomcat.setBaseDir("/tmp");
//tomcat.setPort(port);
Connector connector = tomcat.getConnector();
Expand All @@ -40,6 +42,16 @@ public void embed() throws LifecycleException {
String contextPath = "";
String docBase = new File(".").getAbsolutePath();
Context context = tomcat.addContext(contextPath, docBase);
FilterDef filterDef = new FilterDef();
filterDef.setFilterName(TeeFilter.class.getSimpleName());
filterDef.setFilterClass(TeeFilter.class.getName());
context.addFilterDef(filterDef);

FilterMap myFilterMap = new FilterMap();
myFilterMap.setFilterName(TeeFilter.class.getSimpleName());
myFilterMap.addURLPattern("/*");
context.addFilterMap(myFilterMap);

String servletName = "SampleServlet";
String urlPattern = "/*";

Expand Down

0 comments on commit cff41f6

Please sign in to comment.