Issue

Microservice pattern: Log aggregration · Issue #2690 · iluwatar/java-design-patterns
https://microservices.io/patterns/observability/application-logging.html
github.com
PR

feat: Add Microservice Pattern, Log aggregation (#2690) by hwan33 · Pull Request #2719 · iluwatar/java-design-patterns
Related: #2690 Pull request description sample java code with javadoc test code UML diagram README.md file
github.com
Changes
기능 추가

LogProducer
- 로그를 생성하는 서비스
- 특정 행위 혹은 이벤트를 통해서 로그가 생성되면, 처리를 위해
LogAggregator
로 전달
LogAggregator
- 서로 다른 서비스로부터 생성된 로그를 수집하고 버퍼에 모아두는 서비스
- 버퍼에 담긴 로그의 양이 threshold를 넘거나 정해진 시간이 지나면,
CentralLogStore
로 flush - 비동기 처리되어 로그는 안전하게 수집된다
CentralLogStore
- 중앙 집중화된 로그 저장소
- 스레드 안전 (다양한 서비스에서 concurrent하게 생성된 로그를 안전하게 저장)
LogEntry
- 로그의 정보를 담은 클래스
LogLevel
- 로그의 레벨을 나타내는 enum 클래스
테스트 코드 작성
@ExtendWith(MockitoExtension.class)
class LogAggregatorTest {
@Mock
private CentralLogStore centralLogStore;
private LogAggregator logAggregator;
@BeforeEach
void setUp() {
logAggregator = new LogAggregator(centralLogStore, LogLevel.INFO);
}
@Test
void whenThreeInfoLogsAreCollected_thenCentralLogStoreShouldStoreAllOfThem() {
logAggregator.collectLog(createLogEntry(LogLevel.INFO, "Sample log message 1"));
logAggregator.collectLog(createLogEntry(LogLevel.INFO, "Sample log message 2"));
verifyNoInteractionsWithCentralLogStore();
logAggregator.collectLog(createLogEntry(LogLevel.INFO, "Sample log message 3"));
verifyCentralLogStoreInvokedTimes(3);
}
@Test
void whenDebugLogIsCollected_thenNoLogsShouldBeStored() {
logAggregator.collectLog(createLogEntry(LogLevel.DEBUG, "Sample debug log message"));
verifyNoInteractionsWithCentralLogStore();
}
private static LogEntry createLogEntry(LogLevel logLevel, String message) {
return new LogEntry("ServiceA", logLevel, message, LocalDateTime.now());
}
private void verifyNoInteractionsWithCentralLogStore() {
verify(centralLogStore, times(0)).storeLog(any());
}
private void verifyCentralLogStoreInvokedTimes(int times) {
verify(centralLogStore, times(times)).storeLog(any());
}
}
- 특정 로그 레벨 이상의 로그 발생 시, 수집 저장이 정상적으로 작동하는가?
- 특정 로그 레벨 이하의 로그 발생 시, 적절히 필터링 되는가?
Merge

최종 머지되었습니다. (2달 정도의 기다림이 필요했습니다)
분석
로그 집계 시스템을 설계할 때 버퍼, 비동기 처리, 스레드 안전성은 중요한 요소들입니다. 이러한 특성들은 로그 데이터를 효과적으로 수집하고, 저장하며, 동시에 여러 서비스나 스레드 간의 데이터 무결성을 보장하는 데 필수적입니다.
느낀점
추후 Logstash와 같은 로그 집계에 사용되는 오픈소스를 살펴보는 것도 재밌겠다는 생각이 듭니다.
Log aggregation
Intent Centralize, streamline, and optimize the process of log management so that insights can be quickly derived, problems can be swiftly identified and resolved, and the system's overall health can be monitored efficiently. Explanation Real-world example
java-design-patterns.com