Skip to content

Commit 696e422

Browse files
committed
feat: 로깅 추가
1 parent 93c19e6 commit 696e422

File tree

7 files changed

+92
-6
lines changed

7 files changed

+92
-6
lines changed

.github/workflows/deploy.yml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ on:
44
push:
55
branches:
66
- main
7+
- feat/logging
78

89
jobs:
910
deploy:
@@ -68,7 +69,9 @@ jobs:
6869
key: ${{ secrets.EC2_PRIVATE_KEY }}
6970
script_stop: true
7071
script: |
71-
docker pull ${{ steps.login-ecr.outputs.registry }}/music:${{env.date}}
72-
docker stop music || true
73-
docker rm music || true
74-
docker run -d -e TZ=Asia/Seoul --name music -p 8080:8080 ${{ steps.login-ecr.outputs.registry }}/music:${{env.date}}
72+
docker run -d \
73+
-e TZ=Asia/Seoul \
74+
--name music \
75+
-p 8080:8080 \
76+
-v /home/ubuntu/logs:/app/logs \
77+
${{ steps.login-ecr.outputs.registry }}/music:${{env.date}}

build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ dependencies {
5353

5454
// validation
5555
implementation 'org.springframework.boot:spring-boot-starter-validation'
56+
57+
// logstash
58+
implementation 'net.logstash.logback:logstash-logback-encoder:7.4'
5659
}
5760

5861
tasks.named('test') {

src/main/java/apptive/team5/config/SecurityConfig.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public CorsConfigurationSource corsConfigurationSource() {
7474
private final String[] whiteList =
7575
{
7676
"/api/jwt/exchange",
77-
"/api/oauth2/**"
77+
"/api/oauth2/**",
78+
"/api/users/throw"
7879
};
7980
}
Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
package apptive.team5.config;
22

3+
import apptive.team5.filter.LoggingFilter;
4+
import org.springframework.boot.web.servlet.FilterRegistrationBean;
5+
import org.springframework.context.annotation.Bean;
36
import org.springframework.context.annotation.Configuration;
47
import org.springframework.data.web.config.EnableSpringDataWebSupport;
8+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
59

610
import static org.springframework.data.web.config.EnableSpringDataWebSupport.PageSerializationMode.VIA_DTO;
711

812
@Configuration
913
@EnableSpringDataWebSupport(pageSerializationMode = VIA_DTO)
10-
public class WebConfig {
14+
public class WebConfig implements WebMvcConfigurer {
15+
16+
@Bean
17+
public FilterRegistrationBean loggingFilter() {
18+
FilterRegistrationBean registration = new FilterRegistrationBean();
19+
registration.setFilter(new LoggingFilter());
20+
registration.addUrlPatterns("/*");
21+
registration.setOrder(1);
22+
23+
return registration;
24+
}
1125
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package apptive.team5.filter;
2+
3+
import jakarta.servlet.*;
4+
import jakarta.servlet.http.HttpServletRequest;
5+
import jakarta.servlet.http.HttpServletResponse;
6+
import lombok.extern.slf4j.Slf4j;
7+
8+
import java.io.IOException;
9+
import java.util.UUID;
10+
11+
@Slf4j
12+
public class LoggingFilter implements Filter {
13+
@Override
14+
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
15+
16+
HttpServletRequest request = (HttpServletRequest) servletRequest;
17+
HttpServletResponse response = (HttpServletResponse) servletResponse;
18+
String loggingId = UUID.randomUUID().toString();
19+
20+
try {
21+
long startTime = System.currentTimeMillis();
22+
log.trace("[REQUEST] id:{}, request URI: {}, METHOD:{}", loggingId, request.getRequestURI(), request.getMethod());
23+
filterChain.doFilter(request, response);
24+
long endTime = System.currentTimeMillis();
25+
log.trace("[RESPONSE] id:{}, HTTP STATUS:{}, TIME:{}ms ", loggingId, response.getStatus(), endTime - startTime);
26+
} catch (Exception e) {
27+
log.error("[REQUEST] id:{}, request URI: {}, METHOD:{}", loggingId, request.getRequestURI(), request.getMethod());
28+
log.error("[RESPONSE] id:{}, ERROR:{}", loggingId, e.getMessage(), e);
29+
throw e;
30+
}
31+
}
32+
}

src/main/java/apptive/team5/user/controller/UserController.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,9 @@ public ResponseEntity<UserResponse> getMyInfo(@AuthenticationPrincipal String id
2424

2525
return ResponseEntity.status(HttpStatus.OK).body(response);
2626
}
27+
28+
@GetMapping("/throw")
29+
public ResponseEntity<?> throwException() {
30+
throw new RuntimeException("This is a test");
31+
}
2732
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<configuration>
2+
<property name="LOG_FILE" value="application.log"/>
3+
4+
<!-- 콘솔 출력 -->
5+
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
6+
<encoder>
7+
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level [%thread] %logger{36} - %msg%n</pattern>
8+
</encoder>
9+
</appender>
10+
11+
<!-- 파일 출력 -->
12+
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
13+
<file>${LOG_FILE}</file>
14+
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
15+
<fileNamePattern>application.%d{yyyy-MM-dd_HH-mm}.log.gz</fileNamePattern>
16+
<maxHistory>5</maxHistory>
17+
</rollingPolicy>
18+
<encoder>
19+
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level [%thread] %logger{36} - %msg%n</pattern>
20+
</encoder>
21+
</appender>
22+
23+
<!-- Logger 설정 -->
24+
<root level="info">
25+
<appender-ref ref="CONSOLE" />
26+
<appender-ref ref="FILE" />
27+
</root>
28+
</configuration>

0 commit comments

Comments
 (0)