Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 글로벌 홍보 서포터즈
- 블루 그린
- k8s
- 배포전략
- Docker 명령어
- re2.node
- API MARKETPLACE
- 커밋삭제
- API MARKETPLACE 글로벌 홍보 서포터즈
- LOG
- 젠킨스 설치
- APIMARKETPLACE
- slf4j
- 젠킨스
- Docker
- 네이버
- naver
- Integrations & services
- 대외활동
- k8s 구성요소
- 2022
- identity center
- 특정 커밋 삭제
- log4j2
- spring boot
- 특정커밋
- jenkins
- 서포터즈
- 사용자 추가
- Elk
Archives
- Today
- Total
G코딩 스토리
[Spring Boot] Log4j2 로그를 여러 개의 파일에 저장하는 방법 본문
log4j란
Log4j는 Java 기반의 로깅 프레임워크입니다. Log4j는 다양한 로깅 기능을 제공하여 애플리케이션의 디버깅과 모니터링을 돕습니다. Log4j는 그 유연성과 확장성 덕분에 많은 Java 애플리케이션에서 사용됩니다.
Log4j2는 Log4j의 후속 버전으로, 성능 개선, 동시성 처리 향상, 새로운 기능 추가 등 다양한 면에서 이전 버전보다 발전된 기능을 제공합니다.
SLF4J : 로깅의 추상화 계층으로, 다양한 로깅 구현체(예: Log4j, Logback 등)와 함께 사용할 수 있는 표준 인터페이스를 제공합니다.
log4j2를 통해서 로그를 여러 개의 파일에 저장하는 방법
여러 이벤트의 로그를 서로 다른 파일에 저장하고 싶다면 log4j2.xml
에서 Log를 기록할 서비스와 파일을 지정해주면 된다.
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<!-- 경매 로그를 위한 파일 앱렌더 -->
<File name="AuctionFile" fileName="logs/auction.log">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1} - %m%n"/>
</File>
<!-- 환전 로그를 위한 파일 앱렌더 -->
<File name="ExchangeFile" fileName="logs/exchange.log">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1} - %m%n"/>
</File>
</Appenders>
<Loggers>
<!-- 경매 로그 레벨 -->
<Logger name="com.example.AuctionService" level="info" additivity="false">
<AppenderRef ref="AuctionFile"/>
</Logger>
<!-- 환전 로그 레벨 -->
<Logger name="com.example.ExchangeService" level="info" additivity="false">
<AppenderRef ref="ExchangeFile"/>
</Logger>
<Root level="warn">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
Service Class
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class AuctionService {
public void placeBid(String userId, String auctionItemId, double bidAmount) {
// 입찰 처리 로직
// ...
// 경매 입찰 로그 기록
log.info("User {} placed a bid of {} on auction item {} at {}", userId, bidAmount, auctionItemId, System.currentTimeMillis());
}
}
@Slf4j
@Service
public class ExchangeService {
public void exchangeMoney(String userId, double exchangeAmount) {
// 환전 처리 로직
// ...
// 돈 환전 로그 기록
log.info("User {} exchanged an amount of {} at {}", userId, exchangeAmount, System.currentTimeMillis());
}
}
그러면 아래와 같이 각 서비스에 대한 로그를 다른 파일에 기록할 수 있다.
logs/auction.log
2024-08-02 10:15:30 INFO AuctionService - User user1 placed a bid of 150.0 on auction item item123 at 1690960530000
2024-08-02 10:20:45 INFO AuctionService - User user2 placed a bid of 200.0 on auction item item456 at 1690960645000
logs/exchange.log
2024-08-02 10:30:15 INFO ExchangeService - User user1 exchanged an amount of 1000.0 at 1690960615000
2024-08-02 10:35:50 INFO ExchangeService - User user3 exchanged an amount of 500.0 at 1690960750000
'Develop > Spring' 카테고리의 다른 글
[Spring Boot + ELK] ELK에서 Spring Boot로 데이터 전달하는 방법 (0) | 2024.08.04 |
---|