-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[REFACTOR] 로깅 고도화 1차
- Loading branch information
Showing
9 changed files
with
322 additions
and
38 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
72 changes: 72 additions & 0 deletions
72
biengual-core/src/main/java/com/biengual/core/util/GlobalExceptionLogSchema.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,72 @@ | ||
package com.biengual.core.util; | ||
|
||
import com.biengual.core.response.ApiCustomResponse; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import lombok.Builder; | ||
|
||
@Builder | ||
public record GlobalExceptionLogSchema( | ||
String server, | ||
String ip, | ||
String contentType, | ||
String userAgent, | ||
String user, | ||
String httpMethod, | ||
String uri, | ||
String params, | ||
Object requestBody, | ||
String code, | ||
String message | ||
) { | ||
public static GlobalExceptionLogSchema of( | ||
String server, String user, HttpServletRequest request, ApiCustomResponse response | ||
) { | ||
Object optionalRequestBody = request.getAttribute("requestBody"); | ||
boolean existsRequestBody = optionalRequestBody.toString().isEmpty(); | ||
|
||
return GlobalExceptionLogSchema.builder() | ||
.server(server) | ||
.ip(request.getRemoteAddr()) | ||
.contentType(request.getContentType()) | ||
.userAgent(request.getHeader("User-Agent")) | ||
.user(user) | ||
.httpMethod(request.getMethod()) | ||
.uri(request.getRequestURI()) | ||
.params(request.getQueryString()) | ||
.requestBody(existsRequestBody ? null : optionalRequestBody) | ||
.code(response.code()) | ||
.message(response.message()) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return """ | ||
[GLOBAL EXCEPTION] | ||
server: %s, | ||
ip: %s, | ||
contentType: %s, | ||
userAgent: %s, | ||
user: %s, | ||
httpMethod: %s, | ||
uri: %s, | ||
params: %s, | ||
requestBody: %s, | ||
code: %s, | ||
message: %s | ||
""" | ||
.formatted( | ||
this.server, | ||
this.ip, | ||
this.contentType, | ||
this.userAgent, | ||
this.user, | ||
this.httpMethod, | ||
this.uri, | ||
this.params, | ||
this.requestBody, | ||
this.code, | ||
this.message | ||
); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
biengual-core/src/main/java/com/biengual/core/util/RestControllerSuccessLogSchema.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,76 @@ | ||
package com.biengual.core.util; | ||
|
||
import com.biengual.core.response.ApiCustomResponse; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import lombok.Builder; | ||
|
||
@Builder | ||
public record RestControllerSuccessLogSchema( | ||
String server, | ||
String ip, | ||
String contentType, | ||
String userAgent, | ||
String user, | ||
String httpMethod, | ||
String uri, | ||
String params, | ||
Object requestBody, | ||
String code, | ||
String message, | ||
Long responseTime | ||
) { | ||
public static RestControllerSuccessLogSchema of( | ||
String server, String user, Long responseTime, HttpServletRequest request, ApiCustomResponse response | ||
) { | ||
Object optionalRequestBody = request.getAttribute("requestBody"); | ||
boolean existsRequestBody = optionalRequestBody.toString().isEmpty(); | ||
|
||
return RestControllerSuccessLogSchema.builder() | ||
.server(server) | ||
.ip(request.getRemoteAddr()) | ||
.contentType(request.getContentType()) | ||
.userAgent(request.getHeader("User-Agent")) | ||
.user(user) | ||
.httpMethod(request.getMethod()) | ||
.uri(request.getRequestURI()) | ||
.params(request.getQueryString()) | ||
.requestBody(existsRequestBody ? null : optionalRequestBody) | ||
.code(response.code()) | ||
.message(response.message()) | ||
.responseTime(responseTime) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return """ | ||
[REST API INFO] | ||
server: %s, | ||
ip: %s, | ||
contentType: %s, | ||
userAgent: %s, | ||
user: %s, | ||
httpMethod: %s, | ||
uri: %s, | ||
params: %s, | ||
requestBody: %s, | ||
code: %s, | ||
message: %s, | ||
responseTime: %sms | ||
""" | ||
.formatted( | ||
this.server, | ||
this.ip, | ||
this.contentType, | ||
this.userAgent, | ||
this.user, | ||
this.httpMethod, | ||
this.uri, | ||
this.params, | ||
this.requestBody, | ||
this.code, | ||
this.message, | ||
this.responseTime | ||
); | ||
} | ||
} |
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
19 changes: 19 additions & 0 deletions
19
user-api/src/main/java/com/biengual/userapi/config/HttpRequestFilterConfig.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,19 @@ | ||
package com.biengual.userapi.config; | ||
|
||
import com.biengual.userapi.filter.ReadableRequestWrapperFilter; | ||
import org.springframework.boot.web.servlet.FilterRegistrationBean; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class HttpRequestFilterConfig { | ||
|
||
@Bean | ||
public FilterRegistrationBean<ReadableRequestWrapperFilter> readableRequestWrapperFilter() { | ||
FilterRegistrationBean<ReadableRequestWrapperFilter> registrationBean = new FilterRegistrationBean<>(); | ||
registrationBean.setFilter(new ReadableRequestWrapperFilter()); | ||
registrationBean.addUrlPatterns("/*"); | ||
registrationBean.setName("readableRequestWrapperFilter"); | ||
return registrationBean; | ||
} | ||
} |
12 changes: 11 additions & 1 deletion
12
user-api/src/main/java/com/biengual/userapi/config/ScheduleConfig.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 |
---|---|---|
@@ -1,10 +1,20 @@ | ||
package com.biengual.userapi.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.scheduling.TaskScheduler; | ||
import org.springframework.scheduling.annotation.EnableScheduling; | ||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; | ||
|
||
@Configuration | ||
@EnableScheduling | ||
public class ScheduleConfig { | ||
} | ||
|
||
@Bean | ||
public TaskScheduler taskScheduler() { | ||
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler(); | ||
scheduler.setPoolSize(5); | ||
scheduler.setThreadNamePrefix("scheduled-task-"); | ||
return scheduler; | ||
} | ||
} |
Oops, something went wrong.