Spring 핵심원리 고급편 - Logging Filter

목차

Spring 핵심원리 고급편 - Logging Filter

@RestController
@RequiredArgsConstructor
public class OrderControllerV1 {

private final OrderServiceV1 orderService;
private final HelloTraceV1 trace;

@GetMapping("/v1/request")
public String request(String itemId){

TraceStatus status = null;
try {
status = trace.begin("OrderController.request()");
orderService.orderItem(itemId);
trace.end(status);

return "ok";
}catch (Exception e){
trace.exception(status, e);
throw e; // 예외를 반드시 던져 줘야 한다.
}
}
}
@Service
@RequiredArgsConstructor
public class OrderServiceV1 {

private final OrderRepositoryV1 orderRepository;
private final HelloTraceV1 trace;

public void orderItem(String itemId){
TraceStatus status = null;
try {
status = trace.begin("OrderService.orderItem()");
orderRepository.save(itemId);
trace.end(status);

}catch (Exception e){
trace.exception(status, e);
throw e; // 예외를 반드시 던져 줘야 한다.
}
}
}
@Repository
@RequiredArgsConstructor
public class OrderRepositoryV1 {

private final HelloTraceV1 trace;

public void save(String itemId){

TraceStatus status = null;
try {
status = trace.begin("OrderRepository.save()");

// 저장 로직
if(itemId.equals("ex")){
throw new IllegalStateException("예외 발생!");
}
sleep(1000);

trace.end(status);
}catch (Exception e){
trace.exception(status, e);
throw e; // 예외를 반드시 던져 줘야 한다.
}
}

private void sleep(int millis) {
try{
Thread.sleep(millis);
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
Share