문서의 선택한 두 판 사이의 차이를 보여줍니다.
양쪽 이전 판 이전 판 다음 판 | 이전 판 | ||
java:slf4j [2018/10/23 09:37] kwon37xi |
java:slf4j [2023/12/07 10:32] kwon37xi [Test Slf4j - Reflection 사용] |
||
---|---|---|---|
줄 12: | 줄 12: | ||
<code java> | <code java> | ||
// 자동으로 현재 클래스를 찾아낸다. | // 자동으로 현재 클래스를 찾아낸다. | ||
- | private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); | + | import java.lang.invoke.MethodHandles; |
+ | |||
+ | private static final Logger logger = | ||
+ | | ||
</ | </ | ||
줄 63: | 줄 66: | ||
configuration.exclude group: ' | configuration.exclude group: ' | ||
configuration.exclude group: ' | configuration.exclude group: ' | ||
+ | configuration.exclude group: ' | ||
configuration.exclude group: ' | configuration.exclude group: ' | ||
configuration.exclude group: ' | configuration.exclude group: ' | ||
줄 120: | 줄 124: | ||
handlers = org.slf4j.bridge.SLF4JBridgeHandler | handlers = org.slf4j.bridge.SLF4JBridgeHandler | ||
</ | </ | ||
- | === Logback 설청 === | + | === Logback 설정 === |
* '' | * '' | ||
* groovy 설정< | * groovy 설정< | ||
줄 169: | 줄 173: | ||
System.setProperty(" | System.setProperty(" | ||
</ | </ | ||
+ | |||
+ | ===== Test Slf4j - Reflection 사용 ===== | ||
+ | * [[java: | ||
+ | |||
+ | <code java> | ||
+ | TestTargetClass testTarget = new TestTarget(); | ||
+ | |||
+ | private Field logField; | ||
+ | private Logger originalLogger; | ||
+ | private Logger mockedLogger; | ||
+ | |||
+ | @BeforeEach | ||
+ | void setUp() throws NoSuchFieldException, | ||
+ | mockedLogger = mock(Logger.class); | ||
+ | |||
+ | Field logField = TestTargetClass.class.getDeclaredField(" | ||
+ | logField.setAccessible(true); | ||
+ | |||
+ | Field modifiersField = Field.class.getDeclaredField(" | ||
+ | modifiersField.setAccessible(true); | ||
+ | modifiersField.setInt(logField, | ||
+ | |||
+ | originalLogger = (Logger) logField.get(null); | ||
+ | logField.set(null, | ||
+ | } | ||
+ | |||
+ | @AfterEach | ||
+ | void tearDown() throws IllegalAccessException { | ||
+ | logField.set(null, | ||
+ | } | ||
+ | |||
+ | |||
+ | @Test | ||
+ | void testMethod() { | ||
+ | testTarget.callMethod() | ||
+ | |||
+ | verify(mockedLogger).info(" | ||
+ | |||
+ | } | ||
+ | |||
+ | // Lombok @Slf4j 로 private static final Logger log 필드 주입 | ||
+ | @Slf4j | ||
+ | public static class TestTargetClass { | ||
+ | public void callMethod() { | ||
+ | log.info(" | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
===== Test Slf4j ===== | ===== Test Slf4j ===== | ||
+ | * [[https:// | ||
* [[http:// | * [[http:// | ||
+ | * [[https:// | ||
+ | * http:// | ||
===== 참조 ===== | ===== 참조 ===== | ||
* [[https:// | * [[https:// | ||