-
Notifications
You must be signed in to change notification settings - Fork 1
로깅 전략
배대준 edited this page Dec 13, 2019
·
2 revisions
-
@ExceptionHandler
가 붙은 메소드 에러 메시지 출력 (error) -
@EnableLog
붙인 클래스 혹은 메소드 요청, 응답 출력 (debug)
-
@ExceptionHandler
붙이기 - 첫 번째 인자는
Exception
@ExceptionHandler({RuntimeException.class})
public ResponseEntity<ApiErrorResponse> handleException(final Exception exception) {
ApiErrorResponse apiErrorResponse = ApiErrorResponse.builder()
.message(exception.getMessage())
.status(HttpStatus.BAD_REQUEST.value())
.build();
return ResponseEntity.badRequest().body(apiErrorResponse);
}
@Around("@annotation(org.springframework.web.bind.annotation.ExceptionHandler) && args (exception, ..)")
public Object restControllerAdviceLogging(final ProceedingJoinPoint pjp, Exception exception) throws Throwable {
Logger log = getLog(pjp.getSignature().getDeclaringType());
Object result = pjp.proceed();
log.error("errorMessage: {}", exception.getMessage());
return result;
}
-
@EnableLog
를 클래스 혹은 메소드에 붙인다. (클래스에 붙이면 모든 메소드에 적용)
@EnableLog
@RestController
@RequestMapping("/api/v1/users/products")
public class UserProductApiController {
private final UserProductService userProductService;
....
}
@EnableLog
@PostMapping
public ResponseEntity<UserProductResponseDto> create(@RequestBody final UserProductCreateDto userProductCreateDto,
@LoginUser final SessionUser sessionUser) {
Long userId = sessionUser.getId();
UserProductResponseDto responseDto = userProductService.save(userProductCreateDto, userId);
URI uri = linkTo(UserProductApiController.class).slash(responseDto.getId()).toUri();
return ResponseEntity.created(uri).body(responseDto);
}
@Around("@within(com.gaejangmo.apiserver.commons.logging.EnableLog) || @annotation(com.gaejangmo.apiserver.commons.logging.EnableLog)")
public Object methodLogging(final ProceedingJoinPoint pjp) throws Throwable {
Logger log = getLog(pjp.getSignature().getDeclaringType());
log.debug("request by {}, args: {} ", pjp.getSignature().getDeclaringType(), pjp.getArgs());
Object requestResult = pjp.proceed();
log.debug("response {}", requestResult);
return requestResult;
}
-
[회의록]
-
[데일리]
-
[기술 & 이슈 정리]
-
[TIPS]