Skip to content

[김현기] sprint7 #198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: 김현기
Choose a base branch
from

Conversation

LZHTK
Copy link
Collaborator

@LZHTK LZHTK commented Jun 29, 2025

기본 요구사항

프로파일 기반 설정 관리

  • application-dev.yaml, application-prod.yaml 파일을 생성하세요.
  • 다음과 같은 설정값을 프로파일별로 분리하세요.
  • 데이터베이스 연결 정보
  • 서버 포트

로그 관리

  • Lombok의 @slf4j 어노테이션을 활용해 로깅을 쉽게 추가할 수 있도록 구성하세요.

  • application.yaml에 기본 로깅 레벨을 설정하세요.
    기본적으로 info 레벨로 설정합니다.

  • 환경 별 적절한 로깅 레벨을 프로파일 별로 설정해보세요.
    SQL 로그를 보기위해 설정했던 레벨은 유지합니다.
    우리가 작성한 프로젝트의 로그는 개발 환경에서 debug, 운영 환경에서는 info 레벨로 설정합니다.

  • Spring Boot의 기본 로깅 구현체인 Logback의 설정 파일을 구성하세요.

  • logback-spring.xml 파일을 생성하세요.

  • 다음 예시와 같은 로그 메시지를 출력하기 위한 로깅 패턴과 출력 방식을 커스터마이징하세요.

  • 로그 출력 예시

패턴

{년}-{월}-{일} {시}:{분}:{초}:{밀리초} [{스레드명}] {로그 레벨(5글자로 맞춤)} {로거 이름(최대 36글자)} - {로그 메시지}{줄바꿈}

예시

25-01-01 10:33:55.740 [main] DEBUG c.s.m.discodeit.DiscodeitApplication - Running with Sp

  • 콘솔과 파일에 동시에 로그를 기록하도록 설정하세요.

  • 파일은 {프로젝트 루트}/.logs 경로에 저장되도록 설정하세요.

  • 로그 파일은 일자별로 롤링되도록 구성하세요.

  • 로그 파일은 30일간 보관하도록 구성하세요.

  • 서비스 레이어와 컨트롤러 레이어의 주요 메소드에 로깅을 추가하세요.

  • 로깅 레벨을 적절히 사용하세요: ERROR, WARN, INFO, DEBUG

  • 다음과 같은 메소드에 로깅을 추가하세요:

  • 사용자 생성/수정/삭제

  • 채널 생성/수정/삭제

  • 메시지 생성/수정/삭제

  • 파일 업로드/다운로드

  • [ ]

예외 처리 고도화

  • 커스텀 예외를 설계하고 구현하세요.

  • 패키지명: com.sprint.mission.discodeit.exception[.{도메인}]

  • ErrorCode Enum 클래스를 통해 예외 코드명과 메시지를 정의하세요.

  • 아래는 예시입니다. 필요하다고 판단되는 다양한 코드를 정의하세요.

image

  • 모든 예외의 기본이 되는 DiscodeitException 클래스를 정의하세요.

  • 클래스 다이어그램

image

  • details는 예외 발생 상황에 대한 추가정보를 저장하기 위한 속성입니다.

  • 예시

    • 조회 시도한 사용자의 ID 정보
    • 업데이트 시도한 PRIVATE 채널의 ID 정보
  • DiscodeitException을 상속하는 주요 도메인 별 메인 예외 클래스를 정의하세요.

  • UserException, ChannelException 등

  • 실제로 활용되는 클래스라기보다는 예외 클래스의 계층 구조를 명확하게 하기 위한 클래스 입니다.

  • 도메인 메인 예외 클래스를 상속하는 구체적인 예외 클래스를 정의하세요.

  • UserNotFoundException, UserAlreadyExistException 등 필요한 예외를 정의하세요.

  • 예시

image

  • 기존에 구현했던 예외를 커스텀 예외로 대체하세요.

  • NoSuchElementException

  • IllegalArgumentException

  • ErrorResponse를 통해 일관된 예외 응답을 정의하세요.

  • 클래스 다이어그램

image

  • int status: HTTP 상태코드

  • String exceptionType: 발생한 예외의 클래스 이름

  • 앞서 정의한 ErrorResponse와 @RestControllerAdvice를 활용해 예외를 처리하는 예외 핸들러를 구현하세요.

  • 모든 핸들러는 일관된 응답(ErrorResponse)을 가져야 합니다.

유효성 검사

  • Spring Validation 의존성을 추가하세요.
  • 주요 Request DTO에 제약 조건 관련 어노테이션을 추구하세요.
    @NotNull, @notblank, @SiZe, @Email 등
  • 컨트롤러에 @Valid 를 사용해 요청 데이터를 검증하세요.
  • 검증 실패 시 발생하는 MethodArgumentNotValidException을 전역 예외 핸들러에서 처리하세요.
  • 유효성 검증 실패 시 상세한 오류 메시지를 포함한 응답을 반환하세요.

Actuator

  • Spring Boot Actuator 의존성을 추가하세요.

  • 기본 Actuator 엔트포인트를 설정하세요.

  • health, info, metrics, loggers

  • Actuator info를 위한 애플리케이션 정보를 추가하세요.

  • 애플리케이션 이름: Discodeit

  • 애플리케이션 버전: 1.7.0

  • 자바 버전: 17

  • 스프링 부트 버전: 3.4.0

  • 주요 설정 정보

  • 데이터소스: url, 드라이버 클래스 이름

  • jpa: ddl-auto

  • storage 설정: type, path

  • multipart 설정: max-file-size, max-request-size

  • Spring Boot 서버를 실행 후 각종 정보를 확인해보세요.

  • /actuator/info

  • /actuator/metrics

  • /actuator/health

  • /actuator/loggers

단위 테스트

  • 서비스 레이어의 주요 메소드에 대한 단위 테스트를 작성하세요.
  • 다음 서비스의 핵심 메소드에 대해 각각 최소 2개 이상(성공, 실패)의 테스트 케이스를 작성하세요.
  • UserService: create, update, delete 메소드
  • ChannelService: create(PUBLIC, PRIVATE), update, delete, findByUserId 메소드
  • MessageService: create, update, delete, findByChannelId 메소드
  • Mockito를 활용해 Repository 의존성을 모의(mock)하세요.
  • BDDMockito를 활용해 테스트 가독성을 높이세요.

###슬라이스 테스트

  • 레포지토리 레이어의 슬라이스 테스트를 작성하세요.
  • @DataJpaTest를 활용해 테스트를 구현하세요.
  • 테스트 환경을 구성하는 프로파일을 구성하세요.
  • application-test.yaml을 생성하세요.
  • 데이터소스는 H2 인메모리 데이터 베이스를 사용하고, PostgreSQL 호환 모드로 설정하세요.
  • H2 데이터베이스를 위해 필요한 의존성을 추가하세요.
  • 테스트 시작 시 스키마를 새로 생성하도록 설정하세요.
  • 디버깅에 용이하도록 로그 레벨을 적절히 설정하세요.
  • 테스트 실행 간 test 프로파일을 활성화 하세요.
  • JPA Audit 기능을 활성화 하기 위해 테스트 클래스에 @EnableJpaAuditing을 추가하세요.
  • 주요 레포지토리(User, Channel, Message)의 주요 쿼리 메소드에 대해 각각 최소 2개 이상(성공, 실패)의 테스트 케이스를 작성하세요.
  • 커스텀 쿼리 메소드
  • 페이징 및 정렬 메소드
  • 컨트롤러 레이어의 슬라이스 테스트를 작성하세요.
  • @WebMvcTest를 활용해 테스트를 구현하세요.
  • WebMvcTest에서 자동으로 등록되지 않는 유형의 Bean이 필요하다면 @import를 활용해 추가하세요.
  • 주요 컨트롤러(User, Channel, Message)에 대해 최소 2개 이상(성공, 실패)의 테스트 케이스를 작성하세요.
  • MockMvc를 활용해 컨트롤러를 테스트하세요.
  • 서비스 레이어를 모의(mock)하여 컨트롤러 로직만 테스트하세요.
  • JSON 응답을 검증하는 테스트를 포함하세요.

통합 테스트

  • 통합 테스트 환경을 구성하세요.
  • @SpringBootTest를 활용해 Spring 애플리케이션 컨텍스트를 로드하세요.
  • H2 인메모리 데이터베이스를 활용하세요.
  • 테스트용 프로파일을 구성하세요.
  • 주요 API 엔드포인트에 대한 통합 테스트를 작성하세요.
  • 주요 API에 대해 최소 2개 이상의 테스트 케이스를 작성하세요.
  • 사용자 관련 API (생성, 수정, 삭제, 목록 조회)
  • 채널 관련 API (생성, 수정, 삭제)
  • 메시지 관련 API (생성, 수정, 삭제, 목록 조회)
  • 각 테스트는 @transactional을 활용해 독립적으로 실행하세요.

심화 요구사항

MDC를 활용한 로깅 고도화

  • 요청 ID, 요청 URL, 요청 방식 등의 정보를 MDC에 추가하는 인터셉터를 구현하세요.
  • 클래스명: MDCLoggingInterceptor
  • 패키지명: com.**.discodeit.config
  • 요청 ID는 랜덤한 문자열로 생성합니다. (UUID)
  • 요청 ID는 응답 헤더에 포함시켜 더 많은 분석이 가능하도록 합니다.
  • 헤더 이름: Discodeit-Request-ID
  • WebMvcConfigurer를 통해 MDCLoggingInterceptor를 등록하세요.
  • 클래스명: WebMvcConfig
  • 패키지명: com.**.discodeit.config
  • Logback 패턴에 MDC 값을 포함시키세요.

Spring Boot Admin을 활용한 메트릭 가시화

  • Spring Boot Admin 서버를 구현할 모듈을 생성하세요.
  • IntelliJ 화면 참고

image

  • 모듈 정보는 다음과 같습니다.

image

  • 의존성

image

  • admin 모듈의 메인 클래스에 @EnableAdminServer 어노테이션을 추가하고, 서버는 9090번 포트로 설정합니다.

  • admin 서버 실행 후 localhost:9090/applications 에 접속해봅니다.

  • discodeit 프로젝트에 Spring Boot Admin Client를 적용합니다.

  • 의존성을 추가합니다.

dependencies {
...
implementation 'de.codecentric:spring-boot-admin-starter-client:3.4.5
}

  • admin 서버에 등록될 수 있도록 설정 정보를 추가합니다.

application.yml

spring:
application:
name: discodeit
...
boot:
admin:
client:
instance:
name: discodeit
...

application-dev.yml

spring:
application:
name: discodeit
...
boot:
admin:
client:
url: http://localhost:9090
...

application-prod.yml

spring:
application:
name: discodeit
...
boot:
admin:
client:
url: ${SPRING_BOOT_ADMIN_CLIENT_URL}
...

  • discodeit 서버를 실행하고, admin 대시보드에 discodeit 인스턴스가 추가되었는지 확인합니다.

  • admin 대시보드 화면을 조작해보면서 각종 메트릭 정보를 확인해보세요.

  • 주요 API의 요청 횟수, 응답시간 등

  • 서비스 정보

테스트 커버리지 관리

  • JaCoCo 플러그인을 추가하세요.
  • [] 테스트 실행 후 생성된 리포트를 분석해보세요.
  • 리포트는 build/reports/jacoco 경로에서 확인할 수 있습니다.
  • [] com.sprint.mission.discodeit.service.basic 패키지에 대해서 60% 이상의 코드 커버리지를 달성하세요.

스크린 샷

2
캡처


멘토님에게

이번 미션은 base 코드를 기반으로 0-sprint-mission-s7-base에 작성하였습니다!

@LZHTK LZHTK requested a review from ssjf409 June 29, 2025 04:59
@LZHTK LZHTK added the 순한맛🐑 마음이 많이 여립니다.. label Jun 29, 2025

### 숨김 파일 ###
.*
!.gitignore
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log 파일들이나 관련 디렉토리는 커밋에 불필요하게 추가되는걸 막아야될 것 같아요.

희준님이 잘하셔서 링크 남깁니다.
https://github.com/codeit-bootcamp-spring/3-sprint-mission/pull/193/files#diff-bc37d034bad564583790a46f19d807abfe519c5671395fd494d8cce506c42947R29

}

private Optional<BinaryContentCreateRequest> resolveProfileRequest(MultipartFile profileFile) {
if (profileFile.isEmpty()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolveProfileRequest를 호출하는 쪽에서 Optional.ofNullable에서 꺼내서 호출해서요.
MultipartFile profileFile가 null인 경우도 고려는 해야할 것 같아요.


public record LoginRequest(
@NotBlank
@NotNull
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NotBlank 내부에 이미 @NotNull이 포함되어 있어요. 그래서 @NotNull은 붙이실 필요 없으십니다.

@RestControllerAdvice
public class GlobalExceptionHandler {

private final View error;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이게 쓰이는 부분이 없는것 같은데.. 왜 있는건가요?

Comment on lines +27 to +29
@Mapping(target = "participants", expression = "java(resolveParticipants(channel))")
@Mapping(target = "lastMessageAt", expression = "java(resolveLastMessageAt(channel))")
abstract public ChannelDto toDto(Channel channel);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SpEL은 컴파일 시점에 정적 분석의 대상이 아니라서 코드에 문제가 있어도 발견되지 않아요. 그 상태로 배포가 나가서 실제 코드가 동작하는 순간에 확인이 되는데, 너무 리스크가 크다 생각합니다.

@Mapping을 저는 써본적이 없어서 리뷰 달면서 알게되었는데.. 잘 쓰면 유용한 면도 있을것 같다고 생각하는데요. 그만큼 리스크도 커 보이네요.

개인적으로는 이걸 쓰는 것보다 그냥 변환하는 코드를 작성하는게 훨씬 더 나아보입니다.

+ "FROM Message m "
+ "WHERE m.channel.id = :channelId "
+ "ORDER BY m.createdAt DESC LIMIT 1")
Optional<Instant> findLastMessageAtByChannelId(@Param("channelId") UUID channelId);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이것도

Optional<Message> findTopByChannelIdOrderByCreatedAtDesc(UUID channelId);

이렇게 작성하고 사용하는 쪽에서 아래와 같이 사용하는걸 권장드려요.

messageRepository.findTopByChannelIdOrderByCreatedAtDesc(channelId)
                 .map(Message::getCreatedAt);

단일 row의 경우, 전체 컬럼을 가져오는 것과 특정 컬럼만 가져오는 것 간의 성능 차이는 미미하고, JPA의 영속성 컨텍스트를 고려하면 전체 엔티티를 가져오는 것이 재사용성과 캐시 활용 측면에서 더 유리할 수 있어요.


channelRepository.save(channel);
log.info("[Public 채널 생성 성공] 채널 이름 : {} ", name);
return channelMapper.toDto(channel);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

channel의 id를 알수가 없어서 channel로 반환하면 안될거 같아요.
channelRepository.save(channel);에서 반환하는 값을 channelMapper.toDto()에 넣어서 반환해야해요. 그래야 id가 있을거에요.

Comment on lines +28 to +30
org.hibernate.SQL: debug
org.hibernate.orm.jdbc.bind: trace
com.sprint.mission.discodeit: info
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

운영 환경에서는 Debug랑 bind 옵션은 꺼야돼요.
성능상 문제가 되기도 하고 보안상 문제가 될 수 있습니다.

Comment on lines +16 to +27
<appender name="RollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/discodeit.log</file>

<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/discodeit.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>

<encoder>
<pattern>${LOG_PATTERN}</pattern>
</encoder>
</appender>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

로깅 파일을 시간단위로 롤링하는 정책은 현업에서도 많이 쓰고 굉장히 잘 하신것 같아요.
하지만 파일이 너무 커져서 문제가 되는 경우도 있거든요.
그래서 보통은 최대 파일크기도 같이 설정으로 걸어둬요.

아래 파일 크기 + 시간 주기로 롤링하는 정책입니다.

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
  <file>./logs/app.log</file>
  <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
    <fileNamePattern>./logs/app.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
    <maxFileSize>100MB</maxFileSize> <!-- 파일 하나가 100MB 넘으면 새로 만듦 -->
    <maxHistory>30</maxHistory>       <!-- 최대 30일 보관 -->
    <totalSizeCap>5GB</totalSizeCap>  <!-- 전체 합이 5GB 넘으면 오래된 것부터 삭제 -->
  </rollingPolicy>
  <encoder>
    <pattern>${ROLLING_PATTERN}</pattern>
  </encoder>
</appender>


@Test
@DisplayName("POST /channels - case : success")
void createChannelSuccess() throws Exception {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

고치시야할게 몇개 보여서 여기에 한번에 다 남기겠습니다.

  1. 테스트 코드를 작성하실때, // given, // when, // then을 주석으로 달아서 명확하게 구분해주세요. 코드 가독성에 도움이 됩니다.
  2. given, when, then 절을 구분할때는 빈 공백 문자로 해줍니다. 그리고 각 절 내부에서는 불필요한 빈 공백 문자는 사용을 자제합니다. 마찬가지로 코드 가독성에 영향을 줍니다. 아래처럼 바꿔주세요.
  3. 아래 코드 부분은 when절과 then절이 섞여 있는걸로 보입니다.

    // when
    ResultActions result = mockMvc.perform(post("/api/channels/private")
        .contentType(MediaType.APPLICATION_JSON)
        .content(requestBody));

    // then
    result.andExpect(status().isCreated())
          .andExpect(jsonPath("$.type").value(ChannelType.PRIVATE.name()));
  1. 여기에서 when() 때문에 이부분을 when절로 착각하신것 같습니다. 하지만 제가 읽기론 이 테스트코드는 controller를 테스트하는 코드로 보입니다. 그렇다면 테스트 대상은 mockMvc.perform(post("/api/channels/private") 이 코드 부분이어야합니다. when()은 오히려 테스트를 위한 준비단계에 해당하므로 given절에 있는게 적절합니다.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

다른 코드 부분에도 비슷한 문제들이 있는데 굳이 코멘트 안 남기겠습니다. 확인 후 변경 부탁드립니다.

Copy link
Collaborator Author

@LZHTK LZHTK Jul 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵 피드백 해주신 내용 최대한 spritn8에 반영해보도록 하겠습니다!

@ssjf409
Copy link
Collaborator

ssjf409 commented Jul 2, 2025

전체적으로는 잘 작성해주셨습니다.
다만 테스트 코드 부분은 왜 given, when, then을 사용하는지 고민해시고 개선해주시면 감사드립니다.

given: 테스트를 위한 준비 단계
when: 테스트의 대상이되는 코드 실행 단계
then: 실행 결과를 검증하는 단계

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
순한맛🐑 마음이 많이 여립니다..
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants