-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
185 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/main/java/org/swmaestro/repl/gifthub/filter/LoggingFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package org.swmaestro.repl.gifthub.filter; | ||
|
||
import java.io.IOException; | ||
import java.io.UnsupportedEncodingException; | ||
import java.util.Enumeration; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.util.ContentCachingRequestWrapper; | ||
import org.springframework.web.util.ContentCachingResponseWrapper; | ||
import org.springframework.web.util.WebUtils; | ||
|
||
import jakarta.servlet.Filter; | ||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.ServletRequest; | ||
import jakarta.servlet.ServletResponse; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Component | ||
@Slf4j | ||
public class LoggingFilter implements Filter { | ||
|
||
@Override | ||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) | ||
throws IOException, ServletException { | ||
|
||
if (servletRequest instanceof HttpServletRequest && servletResponse instanceof HttpServletResponse) { | ||
HttpServletRequest request = (HttpServletRequest)servletRequest; | ||
HttpServletResponse response = (HttpServletResponse)servletResponse; | ||
|
||
HttpServletRequest requestToCache = new ContentCachingRequestWrapper(request); | ||
HttpServletResponse responseToCache = new ContentCachingResponseWrapper(response); | ||
|
||
chain.doFilter(requestToCache, responseToCache); | ||
|
||
log.info("request header: {}", getHeaders(requestToCache)); | ||
log.info("request body: {}", getRequestBody((ContentCachingRequestWrapper)requestToCache)); | ||
|
||
log.info("response body: {}", getResponseBody(responseToCache)); | ||
|
||
} else { | ||
chain.doFilter(servletRequest, servletResponse); | ||
} | ||
} | ||
|
||
private Map<String, Object> getHeaders(HttpServletRequest request) { | ||
Map<String, Object> headerMap = new HashMap<>(); | ||
|
||
Enumeration<String> headerArray = request.getHeaderNames(); | ||
while (headerArray.hasMoreElements()) { | ||
String headerName = headerArray.nextElement(); | ||
headerMap.put(headerName, request.getHeader(headerName)); | ||
} | ||
return headerMap; | ||
} | ||
|
||
private String getRequestBody(ContentCachingRequestWrapper request) { | ||
ContentCachingRequestWrapper wrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class); | ||
if (wrapper != null) { | ||
byte[] buf = wrapper.getContentAsByteArray(); | ||
if (buf.length > 0) { | ||
try { | ||
return new String(buf, 0, buf.length, wrapper.getCharacterEncoding()); | ||
} catch (UnsupportedEncodingException e) { | ||
return " - "; | ||
} | ||
} | ||
} | ||
return " - "; | ||
} | ||
|
||
private String getResponseBody(final HttpServletResponse response) throws IOException { | ||
String payload = null; | ||
ContentCachingResponseWrapper wrapper = WebUtils.getNativeResponse(response, ContentCachingResponseWrapper.class); | ||
if (wrapper != null) { | ||
wrapper.setCharacterEncoding("UTF-8"); | ||
byte[] buf = wrapper.getContentAsByteArray(); | ||
if (buf.length > 0) { | ||
payload = new String(buf, 0, buf.length, wrapper.getCharacterEncoding()); | ||
wrapper.copyBodyToResponse(); | ||
} | ||
} | ||
return null == payload ? " - " : payload; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<included> | ||
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder> | ||
<pattern>${LOG_PATTERN}</pattern> | ||
</encoder> | ||
</appender> | ||
</included> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<included> | ||
<appender name="FILE-ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender"> | ||
<file>./log/error/error-${BY_DATE}.log</file> | ||
<filter class="ch.qos.logback.classic.filter.LevelFilter"> | ||
<level>ERROR</level> | ||
<onMatch>ACCEPT</onMatch> | ||
<onMismatch>DENY</onMismatch> | ||
</filter> | ||
<encoder> | ||
<pattern>${LOG_PATTERN}</pattern> | ||
</encoder> | ||
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> | ||
<fileNamePattern>./backup/error/error-%d{yyyy-MM-dd}.%i.log</fileNamePattern> | ||
<maxFileSize>100MB</maxFileSize> | ||
<maxHistory>30</maxHistory> | ||
<totalSizeCap>3GB</totalSizeCap> | ||
</rollingPolicy> | ||
</appender> | ||
</included> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<included> | ||
<appender name="FILE-INFO" class="ch.qos.logback.core.rolling.RollingFileAppender"> | ||
<file>./log/info/info-${BY_DATE}.log</file> | ||
<filter class="ch.qos.logback.classic.filter.LevelFilter"> | ||
<level>INFO</level> | ||
<onMatch>ACCEPT</onMatch> | ||
<onMismatch>DENY</onMismatch> | ||
</filter> | ||
<encoder> | ||
<pattern>${LOG_PATTERN}</pattern> | ||
</encoder> | ||
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> | ||
<fileNamePattern>./backup/info/info-%d{yyyy-MM-dd}.%i.log</fileNamePattern> | ||
<maxFileSize>100MB</maxFileSize> | ||
<maxHistory>30</maxHistory> | ||
<totalSizeCap>3GB</totalSizeCap> | ||
</rollingPolicy> | ||
</appender> | ||
</included> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<included> | ||
<appender name="FILE-WARN" class="ch.qos.logback.core.rolling.RollingFileAppender"> | ||
<file>./log/warn/warn-${BY_DATE}.log</file> | ||
<filter class="ch.qos.logback.classic.filter.LevelFilter"> | ||
<level>WARN</level> | ||
<onMatch>ACCEPT</onMatch> | ||
<onMismatch>DENY</onMismatch> | ||
</filter> | ||
<encoder> | ||
<pattern>${LOG_PATTERN}</pattern> | ||
</encoder> | ||
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> | ||
<fileNamePattern>./backup/warn/warn-%d{yyyy-MM-dd}.%i.log</fileNamePattern> | ||
<maxFileSize>100MB</maxFileSize> | ||
<maxHistory>30</maxHistory> | ||
<totalSizeCap>3GB</totalSizeCap> | ||
</rollingPolicy> | ||
</appender> | ||
</included> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<configuration> | ||
<timestamp key="BY_DATE" datePattern="yyyy-MM-dd"/> | ||
<property name="LOG_PATTERN" | ||
value="[%d{yyyy-MM-dd HH:mm:ss}:%-4relative] %green([%thread]) %highlight(%-5level) %boldWhite([%C.%M:%yellow(%L)]) - %msg%n"/> | ||
|
||
<springProfile name="!prod"> | ||
<include resource="console-appender.xml"/> | ||
|
||
<root level="INFO"> | ||
<appender-ref ref="CONSOLE"/> | ||
</root> | ||
</springProfile> | ||
|
||
<springProfile name="prod"> | ||
<include resource="file-info-appender.xml"/> | ||
<include resource="file-warn-appender.xml"/> | ||
<include resource="file-error-appender.xml"/> | ||
|
||
<root level="INFO"> | ||
<appender-ref ref="FILE-INFO"/> | ||
<appender-ref ref="FILE-WARN"/> | ||
<appender-ref ref="FILE-ERROR"/> | ||
</root> | ||
</springProfile> | ||
</configuration> |