-
Notifications
You must be signed in to change notification settings - Fork 1
[BE-Feat] 로그 세팅 추가 #97
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
28a24c9
feat: logback-spring.xml 파일 설정
kwon204 a3a2463
refactor: Filter Configuration을 FilterConfig.java로 모음
kwon204 510b9d2
feat: LogFilter 추가
kwon204 7e7088c
feat: 응답 시간을 nanoTime으로 계산, 요청 쿼리 스트링도 출력하도록 추가
kwon204 543fd14
feat: 로그를 비동기로 처리하는 코드 추가
kwon204 128b11a
refactor: 허용하는 url 적용방식 수정
kwon204 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
backend/src/main/java/endolphin/backend/global/config/FilterConfig.java
This file contains hidden or 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,29 @@ | ||
| package endolphin.backend.global.config; | ||
|
|
||
| import endolphin.backend.global.logging.LogFilter; | ||
| import endolphin.backend.global.security.JwtAuthFilter; | ||
| import org.springframework.boot.web.servlet.FilterRegistrationBean; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| @Configuration | ||
| public class FilterConfig { | ||
|
|
||
| @Bean | ||
| public FilterRegistrationBean<LogFilter> logFilterFilterRegistrationBean() { | ||
| FilterRegistrationBean<LogFilter> filterRegistrationBean = new FilterRegistrationBean<>(); | ||
| filterRegistrationBean.setFilter(new LogFilter()); | ||
| filterRegistrationBean.addUrlPatterns("/api/v1/*"); | ||
| filterRegistrationBean.setOrder(1); | ||
| return filterRegistrationBean; | ||
| } | ||
|
|
||
| @Bean | ||
| public FilterRegistrationBean<JwtAuthFilter> jwtAuthFilterRegistration(JwtAuthFilter filter) { | ||
| FilterRegistrationBean<JwtAuthFilter> registration = new FilterRegistrationBean<>(); | ||
| registration.setFilter(filter); | ||
| registration.addUrlPatterns("/*"); | ||
| registration.setOrder(2); | ||
| return registration; | ||
| } | ||
| } |
36 changes: 36 additions & 0 deletions
36
backend/src/main/java/endolphin/backend/global/logging/LogFilter.java
This file contains hidden or 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,36 @@ | ||
| package endolphin.backend.global.logging; | ||
|
|
||
| import jakarta.servlet.FilterChain; | ||
| import jakarta.servlet.ServletException; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import jakarta.servlet.http.HttpServletResponse; | ||
| import java.io.IOException; | ||
| import java.util.UUID; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.slf4j.MDC; | ||
| import org.springframework.web.filter.OncePerRequestFilter; | ||
|
|
||
| @Slf4j | ||
| public class LogFilter extends OncePerRequestFilter { | ||
|
|
||
| @Override | ||
| protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, | ||
| FilterChain filterChain) throws ServletException, IOException { | ||
|
|
||
| long startTime = System.nanoTime(); | ||
| String correlationId = UUID.randomUUID().toString(); | ||
| MDC.put("correlationId", correlationId); | ||
| log.info("Incoming Request: [{}] {} {}", | ||
| request.getMethod(), request.getRequestURI(), | ||
| request.getQueryString() != null ? request.getQueryString() : ""); | ||
|
|
||
| filterChain.doFilter(request, response); | ||
|
|
||
| long endTime = System.nanoTime(); | ||
| log.info("Incoming Response: [{}] {}, {}, {} ms", | ||
| request.getMethod(), request.getRequestURI() , | ||
| response.getStatus(), (endTime - startTime) / 1_000_000.0); | ||
|
|
||
| MDC.clear(); | ||
| } | ||
| } | ||
19 changes: 0 additions & 19 deletions
19
backend/src/main/java/endolphin/backend/global/security/SecurityConfig.java
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,57 @@ | ||
| <?xml version="1.0" encoding="UTF-8" ?> | ||
| <configuration> | ||
|
|
||
| <property name="CONSOLE_LOG_PATTERN" | ||
| value="%d{yyyy-MM-dd HH:mm:ss}:%-4relative %highlight(%-5level) %magenta(${PID:-}) [%thread] %cyan(%logger{36}) %X{correlationId} - %msg%n" /> | ||
| <property name="FILE_LOG_PATTERN" | ||
| value="%d{yyyy-MM-dd HH:mm:ss}:%-4relative %-5level ${PID:-} [%thread] %logger{36} %X{correlationId} - %msg%n" /> | ||
| <property name="LOG_PATH" value="/var/logs/myapp"/> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider making log path configurable. The log path is hardcoded. Consider making it configurable through Spring properties: - <property name="LOG_PATH" value="/var/logs/myapp"/>
+ <springProperty scope="context" name="LOG_PATH" source="logging.file.path" defaultValue="/var/logs/myapp"/>Then add to your application.properties/yaml: logging:
file:
path: /var/logs/myapp |
||
|
|
||
| <springProfile name="!prod"> | ||
| <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> | ||
| <encoder> | ||
| <pattern>${CONSOLE_LOG_PATTERN}</pattern> | ||
| </encoder> | ||
| </appender> | ||
|
|
||
| <root level="info"> | ||
| <appender-ref ref="CONSOLE" /> | ||
| </root> | ||
|
|
||
| <logger name="org.springframework.web" level="info" /> | ||
| <logger name="endolphin.backend" level="debug" /> | ||
| <logger name="org.hibernate.SQL" level="debug" /> | ||
| </springProfile> | ||
|
|
||
| <springProfile name="prod"> | ||
| <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> | ||
| <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> | ||
| <fileNamePattern>${LOG_PATH}/myapp-%d{yyyy-MM-dd}.%i.log</fileNamePattern> | ||
| <maxHistory>30</maxHistory> | ||
| <maxFileSize>10MB</maxFileSize> | ||
| <totalSizeCap>500MB</totalSizeCap> | ||
| </rollingPolicy> | ||
|
|
||
| <encoder> | ||
| <pattern>${FILE_LOG_PATTERN}</pattern> | ||
| </encoder> | ||
| </appender> | ||
|
|
||
| <appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender"> | ||
| <appender-ref ref="FILE" /> | ||
| <queueSize>512</queueSize> | ||
| <discardingThreshhold>0</discardingThreshhold> | ||
| <includeCallerData>false</includeCallerData> | ||
| <neverBlock>true</neverBlock> | ||
| </appender> | ||
|
|
||
| <root level="info"> | ||
| <appender-ref ref="ASYNC" /> | ||
| </root> | ||
|
|
||
| <logger name="org.springframework.web" level="info" /> | ||
| <logger name="endolphin.backend" level="info" /> | ||
| <logger name="org.hibernate.SQL" level="warn" /> | ||
| </springProfile> | ||
|
|
||
| </configuration> | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
p2; Config에 url pattern으로 추가하는게 좋아 보입니다.