From 7e54779ea8f2bb0ffaac54302289f5415b2c8fba Mon Sep 17 00:00:00 2001 From: Lim Jonguk Date: Tue, 15 Mar 2022 15:22:19 +0900 Subject: [PATCH] feat: @Test log util add --- src/main/java/com/xit/core/util/LogUtil.java | 33 ++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/main/java/com/xit/core/util/LogUtil.java b/src/main/java/com/xit/core/util/LogUtil.java index e6d9299..98af515 100644 --- a/src/main/java/com/xit/core/util/LogUtil.java +++ b/src/main/java/com/xit/core/util/LogUtil.java @@ -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(); } + + /** + *
+	 * 발생하는 Log 이벤트를 List에 저장하여 나중에 조회 가능하도록 해주는 클래스
+	 *
+	 * ListAppender listAppender = LoggerTestUtil.getListAppenderForClass(SomeService.class);
+	 *
+	 * someService.requestJobId(jobId); //메서드 실행
+	 *
+	 * List logsList = listAppender.list; //저장한 데이터를 가져온다
+	 * log.info("전체 logsList : {}", new ObjectMapper().writerWithDefaultPrettyPrinter() //JSON 포멧을 pretty하게 정렬한다
+	 * 	 .writeValueAsString(logsList));
+	 *  assertThat(logsList.get(0).getMessage()).contains("[servicedebug] error occurred : jobId : ");
+	 * 
+ * @Test 에서 사 + * @param clazz + * @return ListAppender + */ + public static ListAppender getListAppenderForClass(Class clazz) { + Logger logger = (Logger) LoggerFactory.getLogger(clazz); + + ListAppender listAppender = new ListAppender<>(); + listAppender.start(); //기록을 시작한다 + + logger.addAppender(listAppender); //로거에 ListAppender를 추가하여 발생하는 로그를 List에 저장하도록 한다 + + return listAppender; + } }