logging.level:
root: info
com.example.log: info
com.example.log.controller: error
com.example.log.service: warn
@RestController
public class TestController {
@Autowired
TestService testService;
Logger log = LoggerFactory.getLogger(TestController.class);
@GetMapping("/log")
public String test() {
log.trace("controller trace log");
log.debug("controller debug log");
log.info("controller info log");
log.warn("controller warn log");
log.error("controller error log");
testService.test();
return "success";
}
}
@Service
public class TestService {
Logger log = LoggerFactory.getLogger(TestService.class);
public void test() {
log.trace("service trace log");
log.debug("service debug log");
log.info("service info log");
log.warn("service warn log");
log.error("service error log");
}
}
logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
... 설정 내용
</configuration>
<!-- 로그 설정 -->
<property name="LOG_DIR" value="./logs"/>
<property name="LOG_PATH_NAME" value="${LOG_DIR}/data/"/>
<property name="ERROR_LOG_PATH_NAME" value="${LOG_DIR}/error/"/>
<property name="LOG_PATTERN_CONSOLE" value="%d{yyyy-MM-dd HH:mm:ss}:%-3relative][%thread] %highlight([ %-5level]) | %cyan(%logger{35}) - %msg%n" />
<property name="LOG_PATTERN_FILE" value="[%d{yyyy-MM-dd HH:mm:ss}:%-3relative][%thread] %-5level %logger{35} - %msg%n" />
<property name="MAX_HISTORY" value="365" />
<!-- 콘솔 출력 -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${LOG_PATTERN_CONSOLE}</pattern>
</encoder>
</appender>
<!-- 파일로 저장-->
<appender name="DATA" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 일자별로 로그파일 적용하기 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH_NAME}data_%d{yyyyMMdd}.log</fileNamePattern>
<maxHistory>${MAX_HISTORY}</maxHistory> <!-- 일자별 백업파일의 보관기간 -->
</rollingPolicy>
<encoder>
<pattern>${LOG_PATTERN_FILE}</pattern>
</encoder>
</appender>
<!-- 에러의 경우는 별도 파일 저장 -->
<appender name="ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- .gz,.zip 등을 넣으면 자동 일자별 로그파일 압축가능 -->
<fileNamePattern>${ERROR_LOG_PATH_NAME}error_%d{yyyyMMdd}.log</fileNamePattern>
<maxHistory>${MAX_HISTORY}</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${LOG_PATTERN_FILE}</pattern>
</encoder>
</appender>
<!-- logger 설정 -->
<springProfile name="dev, prod">
<logger name="com.example.log" level="INFO" additivity="false">
<appender-ref ref="STDOUT"/>
<appender-ref ref="DATA"/>
</logger>
<logger name="com.example.log" level="ERROR" additivity="false">
<appender-ref ref="ERROR"/>
</logger>
</springProfile>
<!-- 위의 logger 에 해당하지 않으면 기본 설정 -->
<root level="INFO">
<appender-ref ref="DATA"/>
<appender-ref ref="STDOUT"/>
</root>
<root level="error">
<appender-ref ref="ERROR" />
</root>
개발 서버와 운영 서버 별로 따로 로그를 남기기 위해 슬랙 채널 두개 생성했다.
서버와 연동하기 위해 웹 훅 설치가 필요하다.
로그를 외부에서 슬랙으로 가져오는 것이니 Incomming WebHooks를 설치 하도록 한다.
이렇게 웹훅이 생성 된다.
밑으로 내려보면 여러 설정을 할 수 있다.
두 채널 각각 추가 하도록 한다.
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'com.github.maricn:logback-slack-appender:1.4.0'
slack-logback.yml(dev)
webhook-uri: {dev 슬랙 webhook-uri}
channel: test_dev
username: dev TEST ERROR LOG
emoji: eyes
slack-logback.yml(prod)
webhook-uri: {prod 슬랙 webhook-uri}
channel: test_prod
username: prod TEST ERROR LOG
emoji: eyes
logback-spring.xml 추가
...
<!-- profile 에 따라 slack 에 전송 -->
<springProfile name="dev, prod">
<property resource="slack-logback.yml" />
<appender name="SLACK_ERROR" class="com.github.maricn.logback.SlackAppender">
<webhookUri>${webhook-uri}</webhookUri>
<channel>#${channel}</channel>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>${LOG_PATTERN_FILE}</pattern>
</layout>
<username>${username}</username>
<iconEmoji>:${emoji}:</iconEmoji>
<colorCoding>true</colorCoding>
</appender>
<appender name="ASYNC_SLACK_ERROR" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="SLACK_ERROR"/>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
</appender>
</springProfile>
<!-- logger 설정 -->
<springProfile name="dev, prod">
<logger name="com.example.log" level="INFO" additivity="false">
<appender-ref ref="STDOUT"/>
<appender-ref ref="DATA"/>
</logger>
<logger name="com.example.log" level="ERROR" additivity="false">
<appender-ref ref="ERROR"/>
<appender-ref ref="ASYNC_SLACK_ERROR"/> <!-- 추가 -->
</logger>
</springProfile>
...
./gradlew clean bootjar -Pprofile=dev
java -jar build/libs/log-0.0.1-SNAPSHOT.jar
./gradlew clean bootjar -Pprofile=prod
java -jar build/libs/log-0.0.1-SNAPSHOT.jar