Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(TeeFilter): Remove printing stacktraces in the filter #9

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 2 additions & 2 deletions tomcat_11_0_blackbox/src/main/resources/logback-stdout.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<configuration>
<statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" />

<appender name="LIST" class="ch.qos.logback.access.jakarta.tomcat_10_1.ListAppender"/>
<appender name="LIST" class="ch.qos.logback.access.tomcat_11_0.ListAppender"/>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%h %l %u %user %date "%r" %s %b</pattern>
<pattern>%h %l %u %user %date "%r" %s %b %n%n%fullResponse</pattern>
</encoder>
</appender>

Expand Down