From 86bbe5a08fb759284669bb2d81069b0ab01c41a6 Mon Sep 17 00:00:00 2001 From: flx5 <1330854+flx5@users.noreply.github.com> Date: Thu, 18 Apr 2024 21:05:11 +0200 Subject: [PATCH] feat(TeeFilter): Remove printing stacktraces in the filter 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 <1330854+flx5@users.noreply.github.com> --- .../access/common/servlet/TeeFilter.java | 47 ++++++++++++------- .../tomcat_11_0/EmbeddedTomcatTest.java | 14 +++++- .../src/main/resources/logback-stdout.xml | 4 +- 3 files changed, 45 insertions(+), 20 deletions(-) diff --git a/common/src/main/java/ch/qos/logback/access/common/servlet/TeeFilter.java b/common/src/main/java/ch/qos/logback/access/common/servlet/TeeFilter.java index 22b69d0..9271135 100644 --- a/common/src/main/java/ch/qos/logback/access/common/servlet/TeeFilter.java +++ b/common/src/main/java/ch/qos/logback/access/common/servlet/TeeFilter.java @@ -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 @@ -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 + "]"); } @@ -101,13 +92,13 @@ public static List 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; } @@ -132,4 +123,26 @@ static boolean mathesExcludesList(String hostname, List 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); + } } diff --git a/tomcat_11_0_blackbox/src/main/java/ch/qos/logback/access/tomcat_11_0/EmbeddedTomcatTest.java b/tomcat_11_0_blackbox/src/main/java/ch/qos/logback/access/tomcat_11_0/EmbeddedTomcatTest.java index 9bfb064..30c504e 100644 --- a/tomcat_11_0_blackbox/src/main/java/ch/qos/logback/access/tomcat_11_0/EmbeddedTomcatTest.java +++ b/tomcat_11_0_blackbox/src/main/java/ch/qos/logback/access/tomcat_11_0/EmbeddedTomcatTest.java @@ -1,5 +1,6 @@ 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; @@ -7,6 +8,8 @@ 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; @@ -31,7 +34,6 @@ public class EmbeddedTomcatTest { @BeforeEach public void embed() throws LifecycleException { - tomcat.setBaseDir("/tmp"); //tomcat.setPort(port); Connector connector = tomcat.getConnector(); @@ -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 = "/*"; diff --git a/tomcat_11_0_blackbox/src/main/resources/logback-stdout.xml b/tomcat_11_0_blackbox/src/main/resources/logback-stdout.xml index 7eec38c..7d001e7 100644 --- a/tomcat_11_0_blackbox/src/main/resources/logback-stdout.xml +++ b/tomcat_11_0_blackbox/src/main/resources/logback-stdout.xml @@ -1,10 +1,10 @@ - + - %h %l %u %user %date "%r" %s %b + %h %l %u %user %date "%r" %s %b %n%n%fullResponse