Skip to content
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
26 changes: 23 additions & 3 deletions src/main/java/in/maheshlangote/logdispatch/LogDispatchFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class LogDispatchFilter extends OncePerRequestFilter {
private final Set<String> maskedHeaders;
private final List<String> excludePaths;
private final Executor dispatchExecutor;
private final int maxStackFrames;

private static final AntPathMatcher ANT_PATH_MATCHER = new AntPathMatcher();

Expand Down Expand Up @@ -74,7 +75,12 @@ public LogDispatchFilter(String serverUrl, String apiKey, List<String> maskedHea
*/
public LogDispatchFilter(boolean enabled, String serverUrl, String apiKey, List<String> maskedHeaders,
List<String> excludePaths, int timeoutMs) {
this(enabled, serverUrl, apiKey, maskedHeaders, excludePaths, new RestTemplate(), null, timeoutMs);
this(enabled, serverUrl, apiKey, maskedHeaders, excludePaths, new RestTemplate(), null, timeoutMs, 100);
}

public LogDispatchFilter(boolean enabled, String serverUrl, String apiKey, List<String> maskedHeaders,
List<String> excludePaths, int timeoutMs, int maxStackFrames) {
this(enabled, serverUrl, apiKey, maskedHeaders, excludePaths, new RestTemplate(), null, timeoutMs, maxStackFrames);
}

LogDispatchFilter(String serverUrl, String apiKey, List<String> maskedHeaders, List<String> excludePaths,
Expand All @@ -84,10 +90,17 @@ public LogDispatchFilter(boolean enabled, String serverUrl, String apiKey, List<

LogDispatchFilter(boolean enabled, String serverUrl, String apiKey, List<String> maskedHeaders,
List<String> excludePaths, RestTemplate restTemplate, Executor dispatchExecutor, int timeoutMs) {
this(enabled, serverUrl, apiKey, maskedHeaders, excludePaths, restTemplate, dispatchExecutor, timeoutMs, 100);
}

LogDispatchFilter(boolean enabled, String serverUrl, String apiKey, List<String> maskedHeaders,
List<String> excludePaths, RestTemplate restTemplate, Executor dispatchExecutor, int timeoutMs,
int maxStackFrames) {
this.enabled = enabled;
this.serverUrl = serverUrl;
this.apiKey = apiKey;
this.timeoutMs = (timeoutMs > 0) ? timeoutMs : 3000;
this.maxStackFrames = maxStackFrames > 0 ? maxStackFrames : 100;
this.dispatchExecutor = dispatchExecutor;
this.restTemplate = Objects.requireNonNull(restTemplate, "restTemplate");

Expand Down Expand Up @@ -252,8 +265,15 @@ private void pushErrorAsync(HttpServletRequest request, int statusCode, Throwabl
String severity = (statusCode >= 500) ? "CRITICAL" : "WARNING";

StringBuilder stackTrace = new StringBuilder();
for (StackTraceElement element : ex.getStackTrace()) {
stackTrace.append(element.toString()).append("\n");
StackTraceElement[] frames = ex.getStackTrace();
int limit = Math.min(frames.length, maxStackFrames);
for (int i = 0; i < limit; i++) {
stackTrace.append(frames[i].toString()).append("\n");
}
if (frames.length > limit) {
stackTrace.append("... ")
.append(frames.length - limit)
.append(" more frames truncated\n");
}

LogDispatchPayload payload = new LogDispatchPayload(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public FilterRegistrationBean<LogDispatchFilter> logDispatchFilterRegistration(L
properties.getApiKey(),
properties.getMaskedHeaders(),
properties.getExcludePaths(),
properties.getTimeoutMs()
properties.getTimeoutMs(),
properties.getMaxStackFrames()
));
registrationBean.addUrlPatterns("/*");
// Use Highest Precedence to ensure it wraps everything including security filters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public class LogDispatchProperties {
private List<String> excludePaths = List.of();
private int timeoutMs = 3000;

/** Max stack frames included in error payloads (default 100). */
private int maxStackFrames = 100;

/**
* Returns whether LogDispatch is enabled.
*
Expand Down Expand Up @@ -155,4 +158,23 @@ public int getTimeoutMs() {
public void setTimeoutMs(int timeoutMs) {
this.timeoutMs = timeoutMs;
}

/**
* Returns the maximum number of stack frames included in error payloads.
*
* @return max stack frames (default 100)
*/
public int getMaxStackFrames() {
return maxStackFrames;
}

/**
* Sets the maximum number of stack frames included in error payloads.
*
* @param maxStackFrames positive frame cap; non-positive values fall back to 100
*/
public void setMaxStackFrames(int maxStackFrames) {
this.maxStackFrames = maxStackFrames > 0 ? maxStackFrames : 100;
}

}