Skip to content

Commit 4371272

Browse files
committed
FEAT: 로그 ELK LOGSTASH 출력
* LogstashEncoder json 출력
1 parent 5980490 commit 4371272

File tree

21 files changed

+99
-81
lines changed

21 files changed

+99
-81
lines changed

Dockerfile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ ARG JWT_SECRET_KEY
1212
ARG REDIS_HOST
1313
ARG REDIS_PORT
1414
ARG REDIS_PASSWORD
15+
ARG LOGSTASH_HOST
16+
ARG LOGSTASH_PORT
17+
18+
ENV VERSION=${VERSION}
1519

1620
ENV MYSQL_HOST=${MYSQL_HOST}
1721
ENV MYSQL_PORT=${MYSQL_PORT}
@@ -23,7 +27,9 @@ ENV JWT_SECRET_KEY=${JWT_SECRET_KEY}
2327
ENV REDIS_HOST=${REDIS_HOST}
2428
ENV REDIS_PORT=${REDIS_PORT}
2529
ENV REDIS_PASSWORD=${REDIS_PASSWORD}
26-
ENV VERSION=${VERSION}
30+
31+
ENV LOGSTASH_HOST=${LOGSTASH_HOST}
32+
ENV LOGSTASH_PORT=${LOGSTASH_PORT}
2733

2834
RUN microdnf install findutils
2935

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ subprojects {
4141
// spring
4242

4343
// utils
44+
implementation 'net.logstash.logback:logstash-logback-encoder:8.0'
4445
compileOnly 'org.projectlombok:lombok'
4546
annotationProcessor 'org.projectlombok:lombok'
4647
implementation 'com.fasterxml.jackson.core:jackson-databind'

core/src/main/java/jshop/core/config/P6SpyConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.springframework.context.annotation.Configuration;
1010
import org.springframework.context.annotation.Profile;
1111

12+
@Profile("dev")
1213
@Configuration
1314
public class P6SpyConfig implements MessageFormattingStrategy {
1415

core/src/main/java/jshop/core/domain/address/service/AddressService.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import jshop.core.domain.user.repository.UserRepository;
1212
import lombok.RequiredArgsConstructor;
1313
import lombok.extern.slf4j.Slf4j;
14+
import org.slf4j.MDC;
1415
import org.springframework.stereotype.Service;
1516
import org.springframework.transaction.annotation.Transactional;
1617

@@ -46,7 +47,9 @@ public void updateAddress(UpdateAddressRequest updateAddressRequest, Long addres
4647
public Address getAddress(Long addressId) {
4748
Optional<Address> optionalAddress = addressRepository.findById(addressId);
4849
return optionalAddress.orElseThrow(() -> {
50+
MDC.put("error_code", String.valueOf(ErrorCode.ADDRESSID_NOT_FOUND.getCode()));
4951
log.error(ErrorCode.ADDRESSID_NOT_FOUND.getLogMessage(), addressId);
52+
MDC.clear();
5053
throw JshopException.of(ErrorCode.ADDRESSID_NOT_FOUND);
5154
});
5255
}

core/src/main/java/jshop/core/domain/cart/entity/Cart.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import lombok.Getter;
1919
import lombok.NoArgsConstructor;
2020
import lombok.extern.slf4j.Slf4j;
21+
import org.slf4j.MDC;
2122

2223
@Slf4j
2324
@Entity
@@ -47,7 +48,9 @@ public static Cart create() {
4748

4849
public void addCart(ProductDetail productDetail, int quantity) {
4950
if (quantity <= 0) {
51+
MDC.put("error_code", String.valueOf(ErrorCode.ILLEGAL_CART_QUANTITY_REQUEST_EXCEPTION.getCode()));
5052
log.error(ErrorCode.ILLEGAL_CART_QUANTITY_REQUEST_EXCEPTION.getLogMessage(), quantity);
53+
MDC.clear();
5154
throw JshopException.of(ErrorCode.ILLEGAL_CART_QUANTITY_REQUEST_EXCEPTION);
5255
}
5356

core/src/main/java/jshop/core/domain/cart/entity/CartProductDetail.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import lombok.NoArgsConstructor;
1919
import lombok.ToString;
2020
import lombok.extern.slf4j.Slf4j;
21+
import org.slf4j.MDC;
2122

2223
@Slf4j
2324
@Entity
@@ -46,7 +47,9 @@ public class CartProductDetail extends BaseEntity {
4647

4748
public void changeQuantity(int quantity) {
4849
if (this.quantity + quantity <= 0) {
50+
MDC.put("error_code", String.valueOf(ErrorCode.ILLEGAL_CART_QUANTITY_REQUEST_EXCEPTION.getCode()));
4951
log.error(ErrorCode.ILLEGAL_CART_QUANTITY_REQUEST_EXCEPTION.getLogMessage(), this.quantity + quantity);
52+
MDC.clear();
5053
throw JshopException.of(ErrorCode.ILLEGAL_CART_QUANTITY_REQUEST_EXCEPTION);
5154
}
5255
this.quantity += quantity;

core/src/main/java/jshop/core/domain/cart/repository/TestRepository.java

Lines changed: 0 additions & 20 deletions
This file was deleted.

core/src/main/java/jshop/core/domain/cart/service/CartService.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import jshop.common.exception.JshopException;
1616
import lombok.RequiredArgsConstructor;
1717
import lombok.extern.slf4j.Slf4j;
18+
import org.slf4j.MDC;
1819
import org.springframework.data.domain.Page;
1920
import org.springframework.data.domain.PageRequest;
2021
import org.springframework.data.domain.Sort;
@@ -48,7 +49,9 @@ public void addCart(AddCartRequest addCartRequest, Long userId) {
4849
Cart cart = getCart(userId);
4950

5051
if (!productDetailRepository.existsByIdAndIsDeletedFalse(detailId)) {
52+
MDC.put("error_code", String.valueOf(ErrorCode.PRODUCTDETAIL_ID_NOT_FOUND.getCode()));
5153
log.error(ErrorCode.PRODUCTDETAIL_ID_NOT_FOUND.getLogMessage(), detailId);
54+
MDC.clear();
5255
throw JshopException.of(ErrorCode.PRODUCTDETAIL_ID_NOT_FOUND);
5356
}
5457

@@ -72,15 +75,19 @@ public void updateCart(Long cartProductDetailId, UpdateCartRequest updateCartReq
7275
public CartProductDetail getCartProductDetail(Long id) {
7376
Optional<CartProductDetail> optionalCartProductDetail = cartProductDetailRepository.findById(id);
7477
return optionalCartProductDetail.orElseThrow(() -> {
78+
MDC.put("error_code", String.valueOf(ErrorCode.CART_PRODUCTDETAIL_ID_NOT_FOUND.getCode()));
7579
log.error(ErrorCode.CART_PRODUCTDETAIL_ID_NOT_FOUND.getLogMessage(), id);
80+
MDC.clear();
7681
throw JshopException.of(ErrorCode.CART_PRODUCTDETAIL_ID_NOT_FOUND);
7782
});
7883
}
7984

8085
public Cart getCart(Long userId) {
8186
Optional<Cart> optionalCart = cartRepository.findCartByUserId(userId);
8287
return optionalCart.orElseThrow(() -> {
88+
MDC.put("error_code", String.valueOf(ErrorCode.CART_NOT_FOUND.getCode()));
8389
log.error(ErrorCode.CART_NOT_FOUND.getLogMessage(), userId);
90+
MDC.clear();
8491
throw JshopException.of(ErrorCode.CART_NOT_FOUND);
8592
});
8693
}

core/src/main/java/jshop/core/domain/coupon/entity/Coupon.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import lombok.NoArgsConstructor;
1818
import lombok.experimental.SuperBuilder;
1919
import lombok.extern.slf4j.Slf4j;
20+
import org.slf4j.MDC;
2021
import org.springframework.data.annotation.CreatedDate;
2122
import org.springframework.data.annotation.LastModifiedDate;
2223

@@ -72,7 +73,9 @@ public abstract class Coupon {
7273
protected boolean checkCouponUsagePeriod() {
7374
LocalDateTime now = LocalDateTime.now();
7475
if (now.isBefore(useStartDate) || now.isAfter(useEndDate)) {
76+
MDC.put("error_code", String.valueOf(ErrorCode.COUPON_USAGE_PERIOD_EXCEPTION.getCode()));
7577
log.error(ErrorCode.COUPON_USAGE_PERIOD_EXCEPTION.getLogMessage(), useStartDate, useEndDate);
78+
MDC.clear();
7679
throw JshopException.of(ErrorCode.COUPON_USAGE_PERIOD_EXCEPTION);
7780
}
7881

@@ -82,7 +85,9 @@ protected boolean checkCouponUsagePeriod() {
8285
protected boolean checkCouponIssuePeriod() {
8386
LocalDateTime now = LocalDateTime.now();
8487
if (now.isBefore(issueStartDate) || now.isAfter(issueEndDate)) {
88+
MDC.put("error_code", String.valueOf(ErrorCode.COUPON_ISSUE_PERIOD_EXCEPTION.getCode()));
8589
log.error(ErrorCode.COUPON_ISSUE_PERIOD_EXCEPTION.getLogMessage(), issueStartDate, issueEndDate);
90+
MDC.clear();
8691
throw JshopException.of(ErrorCode.COUPON_ISSUE_PERIOD_EXCEPTION);
8792
}
8893

@@ -93,7 +98,9 @@ public UserCoupon issueCoupon(User user) {
9398
checkCouponIssuePeriod();
9499

95100
if (remainingQuantity <= 0) {
101+
MDC.put("error_code", String.valueOf(ErrorCode.COUPON_OUT_OF_STOCK_EXCEPTION.getCode()));
96102
log.error(ErrorCode.COUPON_OUT_OF_STOCK_EXCEPTION.getLogMessage(), id, remainingQuantity);
103+
MDC.clear();
97104
throw JshopException.of(ErrorCode.COUPON_OUT_OF_STOCK_EXCEPTION);
98105
}
99106

core/src/main/java/jshop/core/domain/coupon/entity/FixedPriceCoupon.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import lombok.NoArgsConstructor;
1313
import lombok.experimental.SuperBuilder;
1414
import lombok.extern.slf4j.Slf4j;
15+
import org.slf4j.MDC;
1516

1617
@Slf4j
1718
@Getter
@@ -32,7 +33,9 @@ public long discount(long originPrice) {
3233
checkCouponUsagePeriod();
3334

3435
if (originPrice < minOriginPrice) {
36+
MDC.put("error_code", String.valueOf(ErrorCode.COUPON_MIN_PRICE_EXCEPTION.getCode()));
3537
log.error(ErrorCode.COUPON_MIN_PRICE_EXCEPTION.getLogMessage(), minOriginPrice, originPrice);
38+
MDC.clear();
3639
JshopException.of(ErrorCode.COUPON_MIN_PRICE_EXCEPTION);
3740
}
3841

0 commit comments

Comments
 (0)