Skip to content

Commit 86bbe5a

Browse files
committed
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 <[email protected]>
1 parent 40fada8 commit 86bbe5a

File tree

3 files changed

+45
-20
lines changed

3 files changed

+45
-20
lines changed

common/src/main/java/ch/qos/logback/access/common/servlet/TeeFilter.java

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,29 +43,20 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
4343
throws IOException, ServletException {
4444

4545
if (active && request instanceof HttpServletRequest) {
46-
try {
47-
TeeHttpServletRequest teeRequest = new TeeHttpServletRequest((HttpServletRequest) request);
48-
TeeHttpServletResponse teeResponse = new TeeHttpServletResponse((HttpServletResponse) response);
46+
TeeHttpServletRequest teeRequest = new TeeHttpServletRequest((HttpServletRequest) request);
47+
TeeHttpServletResponse teeResponse = new TeeHttpServletResponse((HttpServletResponse) response);
4948

50-
// System.out.println("BEFORE TeeFilter. filterChain.doFilter()");
49+
try {
5150
filterChain.doFilter(teeRequest, teeResponse);
52-
// System.out.println("AFTER TeeFilter. filterChain.doFilter()");
53-
51+
} finally {
5452
teeResponse.finish();
5553
// let the output contents be available for later use by
5654
// logback-access-logging
5755
teeRequest.setAttribute(AccessConstants.LB_OUTPUT_BUFFER, teeResponse.getOutputBuffer());
58-
} catch (IOException e) {
59-
e.printStackTrace();
60-
throw e;
61-
} catch (ServletException e) {
62-
e.printStackTrace();
63-
throw e;
6456
}
6557
} else {
6658
filterChain.doFilter(request, response);
6759
}
68-
6960
}
7061

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

7768
active = computeActivation(localhostName, includeListAsStr, excludeListAsStr);
7869
if (active)
79-
System.out.println("TeeFilter will be ACTIVE on this host [" + localhostName + "]");
70+
logInfo("TeeFilter will be ACTIVE on this host [" + localhostName + "]");
8071
else
81-
System.out.println("TeeFilter will be DISABLED on this host [" + localhostName + "]");
72+
logInfo("TeeFilter will be DISABLED on this host [" + localhostName + "]");
8273

8374
}
8475

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

104-
static String getLocalhostName() {
95+
String getLocalhostName() {
10596
String hostname = "127.0.0.1";
10697

10798
try {
10899
hostname = InetAddress.getLocalHost().getHostName();
109100
} catch (UnknownHostException uhe) {
110-
uhe.printStackTrace();
101+
logWarn("Unknown host", uhe);
111102
}
112103
return hostname;
113104
}
@@ -132,4 +123,26 @@ static boolean mathesExcludesList(String hostname, List<String> excludesList) {
132123
return excludesList.contains(hostname);
133124
}
134125

126+
/**
127+
* Log a warning.
128+
*
129+
* Can be overwritten to use a logger.
130+
*
131+
* @param msg The message.
132+
* @param ex The exception.
133+
*/
134+
protected void logWarn(String msg, Throwable ex) {
135+
System.err.println(msg + ": " + ex);
136+
}
137+
138+
/**
139+
* Log an info message.
140+
*
141+
* Can be overwritten to use a logger.
142+
*
143+
* @param msg The message to log.
144+
*/
145+
protected void logInfo(String msg) {
146+
System.out.println(msg);
147+
}
135148
}

tomcat_11_0_blackbox/src/main/java/ch/qos/logback/access/tomcat_11_0/EmbeddedTomcatTest.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package ch.qos.logback.access.tomcat_11_0;
22

3+
import ch.qos.logback.access.common.servlet.TeeFilter;
34
import ch.qos.logback.access.common.spi.IAccessEvent;
45
import ch.qos.logback.access.tomcat.LogbackValve;
56
import org.apache.catalina.Context;
67
import org.apache.catalina.LifecycleException;
78
import org.apache.catalina.Server;
89
import org.apache.catalina.connector.Connector;
910
import org.apache.catalina.startup.Tomcat;
11+
import org.apache.tomcat.util.descriptor.web.FilterDef;
12+
import org.apache.tomcat.util.descriptor.web.FilterMap;
1013
import org.junit.jupiter.api.AfterEach;
1114
import org.junit.jupiter.api.BeforeEach;
1215
import org.junit.jupiter.api.Test;
@@ -31,7 +34,6 @@ public class EmbeddedTomcatTest {
3134

3235
@BeforeEach
3336
public void embed() throws LifecycleException {
34-
3537
tomcat.setBaseDir("/tmp");
3638
//tomcat.setPort(port);
3739
Connector connector = tomcat.getConnector();
@@ -40,6 +42,16 @@ public void embed() throws LifecycleException {
4042
String contextPath = "";
4143
String docBase = new File(".").getAbsolutePath();
4244
Context context = tomcat.addContext(contextPath, docBase);
45+
FilterDef filterDef = new FilterDef();
46+
filterDef.setFilterName(TeeFilter.class.getSimpleName());
47+
filterDef.setFilterClass(TeeFilter.class.getName());
48+
context.addFilterDef(filterDef);
49+
50+
FilterMap myFilterMap = new FilterMap();
51+
myFilterMap.setFilterName(TeeFilter.class.getSimpleName());
52+
myFilterMap.addURLPattern("/*");
53+
context.addFilterMap(myFilterMap);
54+
4355
String servletName = "SampleServlet";
4456
String urlPattern = "/*";
4557

tomcat_11_0_blackbox/src/main/resources/logback-stdout.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<configuration>
22
<statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" />
33

4-
<appender name="LIST" class="ch.qos.logback.access.jakarta.tomcat_10_1.ListAppender"/>
4+
<appender name="LIST" class="ch.qos.logback.access.tomcat_11_0.ListAppender"/>
55
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
66
<encoder>
7-
<pattern>%h %l %u %user %date "%r" %s %b</pattern>
7+
<pattern>%h %l %u %user %date "%r" %s %b %n%n%fullResponse</pattern>
88
</encoder>
99
</appender>
1010

0 commit comments

Comments
 (0)