feat: @Test log util add

dev
Lim Jonguk 3 years ago
parent f7116730c4
commit 7e54779ea8

@ -1,5 +1,10 @@
package com.xit.core.util;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.read.ListAppender;
import org.slf4j.LoggerFactory;
/**
* Log Util Class
*/
@ -28,4 +33,32 @@ public class LogUtil {
StackTraceElement[] stacks = throwable.getStackTrace();
return stacks[0].getClassName() + "." + stacks[0].getMethodName();
}
/**
* <pre>
* Log List
*
* ListAppender<ILoggingEvent> listAppender = LoggerTestUtil.getListAppenderForClass(SomeService.class);
*
* someService.requestJobId(jobId); //메서드 실행
*
* List<ILoggingEvent> logsList = listAppender.list; //저장한 데이터를 가져온다
* log.info("전체 logsList : {}", new ObjectMapper().writerWithDefaultPrettyPrinter() //JSON 포멧을 pretty하게 정렬한다
* .writeValueAsString(logsList));
* assertThat(logsList.get(0).getMessage()).contains("[servicedebug] error occurred : jobId : ");
* </pre>
* @Test
* @param clazz
* @return ListAppender
*/
public static ListAppender<ILoggingEvent> getListAppenderForClass(Class clazz) {
Logger logger = (Logger) LoggerFactory.getLogger(clazz);
ListAppender<ILoggingEvent> listAppender = new ListAppender<>();
listAppender.start(); //기록을 시작한다
logger.addAppender(listAppender); //로거에 ListAppender를 추가하여 발생하는 로그를 List에 저장하도록 한다
return listAppender;
}
}

Loading…
Cancel
Save