Skip to content

Commit

Permalink
Merge pull request #126 from WE-ARE-RACCOONS/develop
Browse files Browse the repository at this point in the history
RAC-315 refactor : ERROR 로그 추가
  • Loading branch information
ywj9811 authored Feb 22, 2024
2 parents 42c4652 + 038d7b0 commit 525fcbd
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
Expand All @@ -21,6 +22,7 @@

@Component
@RequiredArgsConstructor
@Slf4j
public class CustomAccessDeniedHandler implements AccessDeniedHandler {
@Value("${log.Type}")
private String env;
Expand All @@ -34,6 +36,7 @@ public void handle(HttpServletRequest request, HttpServletResponse response, Acc
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
response.setCharacterEncoding("UTF-8");
logService.save(new LogRequest(env, CustomAccessDeniedHandler.class.getSimpleName(), PERMISSION_DENIED.getMessage()));
log.error("AccessDenied : {}", PERMISSION_DENIED.getMessage());
objectMapper.writeValue(
response.getOutputStream(),
new ErrorResponse(AUTH_DENIED.getCode(), PERMISSION_DENIED.getMessage())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
Expand All @@ -22,6 +23,7 @@

@Component
@RequiredArgsConstructor
@Slf4j
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {
@Value("${log.Type}")
private String env;
Expand All @@ -35,6 +37,7 @@ public void commence(HttpServletRequest request, HttpServletResponse response, A
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
response.setCharacterEncoding("UTF-8");
logService.save(new LogRequest(env, CustomAuthenticationEntryPoint.class.getSimpleName(), FAILED_AUTH.getMessage()));
log.error("AuthenticationEntryPoint : {}", FAILED_AUTH.getMessage());
objectMapper.writeValue(
response.getOutputStream(),
new ErrorResponse(AUTH_FAILED.getCode(), FAILED_AUTH.getMessage())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ private void jwtExceptionHandler(HttpStatus status, HttpServletResponse response
response.setStatus(status.value());
response.setContentType(CONTENT_TYPE);
response.setCharacterEncoding(CHARACTER_ENCODING);
log.error("errorCode {}, errorMessage {}", ex.getErrorCode(), ex.getMessage());
try {
logService.save(new LogRequest(env, JwtFilter.class.getSimpleName(), ex.getMessage()));
String json = new ObjectMapper().writeValueAsString(ResponseDto.create(ex.getErrorCode(), ex.getMessage()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@ private Object getObject(ProceedingJoinPoint joinPoint) throws Throwable {
return result;
}catch (ApplicationException e) {
if (traceStatus != null) {
logTrace.exception(e, traceStatus);
log.error("ErrorCode {} errorMessage {}",e.getErrorCode(), e.getMessage());
log.error("{}", e.getStackTrace());
logService.save(new LogRequest(env, traceStatus.threadId(), traceStatus.methodName(), e.getMessage()));
}
throw e;
}catch (Exception e) {
if (traceStatus != null) {
logTrace.exception(e, traceStatus);
log.error("500 ERROR {}", e.getMessage());
log.error("{}", e.getStackTrace());
logService.save(new LogRequest(env, traceStatus.threadId(), traceStatus.methodName(), e.getMessage()));
}
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ public class LogService {
@Value("${log.uri}")
private String logUri;

public void save(LogRequest logRequest) throws IOException {
log.info("log save");
public void save(LogRequest logRequest) {
webClient.post()
.uri(logUri)
.accept(MediaType.APPLICATION_JSON)
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/logback-spring.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>50MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>30</maxHistory>
<maxHistory>14</maxHistory>
</rollingPolicy>
</appender>

Expand Down

0 comments on commit 525fcbd

Please sign in to comment.