From a599e3692b5f57cf6090fa1ed8182fb5202594c8 Mon Sep 17 00:00:00 2001 From: Jinwoo Lee Date: Fri, 22 Sep 2023 01:01:36 +0900 Subject: [PATCH 1/4] chore: add dependency for logging --- build.gradle | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/build.gradle b/build.gradle index f8379c6b..3975a96e 100644 --- a/build.gradle +++ b/build.gradle @@ -65,6 +65,10 @@ dependencies { // firebase implementation group: 'com.google.firebase', name: 'firebase-admin', version: '8.1.0' + + // logging + testImplementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.4.5' + implementation group: 'org.slf4j', name: 'slf4j-api', version: '2.0.7' } tasks.named('test') { From e0f0045abca940491800fb0d5c673aea8dfe9798 Mon Sep 17 00:00:00 2001 From: Jinwoo Lee Date: Mon, 25 Sep 2023 21:51:29 +0900 Subject: [PATCH 2/4] docs: define log files --- src/main/resources/console-appender.xml | 7 ++++++ src/main/resources/file-error-appender.xml | 19 +++++++++++++++ src/main/resources/file-info-appender.xml | 19 +++++++++++++++ src/main/resources/file-warn-appender.xml | 19 +++++++++++++++ src/main/resources/logback-spring.xml | 27 ++++++++++++++++++++++ 5 files changed, 91 insertions(+) create mode 100644 src/main/resources/console-appender.xml create mode 100644 src/main/resources/file-error-appender.xml create mode 100644 src/main/resources/file-info-appender.xml create mode 100644 src/main/resources/file-warn-appender.xml create mode 100644 src/main/resources/logback-spring.xml diff --git a/src/main/resources/console-appender.xml b/src/main/resources/console-appender.xml new file mode 100644 index 00000000..48ec4fcb --- /dev/null +++ b/src/main/resources/console-appender.xml @@ -0,0 +1,7 @@ + + + + ${LOG_PATTERN} + + + diff --git a/src/main/resources/file-error-appender.xml b/src/main/resources/file-error-appender.xml new file mode 100644 index 00000000..b0392406 --- /dev/null +++ b/src/main/resources/file-error-appender.xml @@ -0,0 +1,19 @@ + + + ./log/error/error-${BY_DATE}.log + + ERROR + ACCEPT + DENY + + + ${LOG_PATTERN} + + + ./backup/error/error-%d{yyyy-MM-dd}.%i.log + 100MB + 30 + 3GB + + + diff --git a/src/main/resources/file-info-appender.xml b/src/main/resources/file-info-appender.xml new file mode 100644 index 00000000..cd973490 --- /dev/null +++ b/src/main/resources/file-info-appender.xml @@ -0,0 +1,19 @@ + + + ./log/info/info-${BY_DATE}.log + + INFO + ACCEPT + DENY + + + ${LOG_PATTERN} + + + ./backup/info/info-%d{yyyy-MM-dd}.%i.log + 100MB + 30 + 3GB + + + \ No newline at end of file diff --git a/src/main/resources/file-warn-appender.xml b/src/main/resources/file-warn-appender.xml new file mode 100644 index 00000000..b00a606a --- /dev/null +++ b/src/main/resources/file-warn-appender.xml @@ -0,0 +1,19 @@ + + + ./log/warn/warn-${BY_DATE}.log + + WARN + ACCEPT + DENY + + + ${LOG_PATTERN} + + + ./backup/warn/warn-%d{yyyy-MM-dd}.%i.log + 100MB + 30 + 3GB + + + \ No newline at end of file diff --git a/src/main/resources/logback-spring.xml b/src/main/resources/logback-spring.xml new file mode 100644 index 00000000..0504fba3 --- /dev/null +++ b/src/main/resources/logback-spring.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file From 735459c3ad5e900a4cde25c69fdcadd54fd2b59a Mon Sep 17 00:00:00 2001 From: Jinwoo Lee Date: Tue, 26 Sep 2023 02:36:35 +0900 Subject: [PATCH 3/4] feat: implement logging-filter --- .../repl/gifthub/filter/LoggingFilter.java | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 src/main/java/org/swmaestro/repl/gifthub/filter/LoggingFilter.java diff --git a/src/main/java/org/swmaestro/repl/gifthub/filter/LoggingFilter.java b/src/main/java/org/swmaestro/repl/gifthub/filter/LoggingFilter.java new file mode 100644 index 00000000..87fe0d25 --- /dev/null +++ b/src/main/java/org/swmaestro/repl/gifthub/filter/LoggingFilter.java @@ -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 getHeaders(HttpServletRequest request) { + Map headerMap = new HashMap<>(); + + Enumeration 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; + } +} + From 0483366a0427b91d46c7cad96f6611a91fd41507 Mon Sep 17 00:00:00 2001 From: Jinwoo Lee Date: Tue, 26 Sep 2023 02:42:08 +0900 Subject: [PATCH 4/4] cicd: delete creation resource directory --- .github/workflows/on_pull_request.yml | 1 - .github/workflows/on_push.yml | 1 - 2 files changed, 2 deletions(-) diff --git a/.github/workflows/on_pull_request.yml b/.github/workflows/on_pull_request.yml index 02c8fc59..613da09f 100644 --- a/.github/workflows/on_pull_request.yml +++ b/.github/workflows/on_pull_request.yml @@ -47,7 +47,6 @@ jobs: # 3. Spring 환경 변수 설정 - name: Set up application.yml run: | - mkdir ./src/main/resources cd ./src/main/resources touch ./application.yml echo "${{ secrets.APPLICATION }}" > ./application.yml diff --git a/.github/workflows/on_push.yml b/.github/workflows/on_push.yml index 6cc9d369..56ce2097 100644 --- a/.github/workflows/on_push.yml +++ b/.github/workflows/on_push.yml @@ -49,7 +49,6 @@ jobs: # yml 파일 생성 - name: Set up application.yml run: | - mkdir ./src/main/resources cd ./src/main/resources touch ./application.yml echo "${{ secrets.APPLICATION }}" > ./application.yml