문서의 선택한 두 판 사이의 차이를 보여줍니다.
| 양쪽 이전 판 이전 판 다음 판 | 이전 판 | ||
|
java:slf4j [2018/08/25 10:38] kwon37xi [java.util.logging] |
java:slf4j [2023/12/07 10:32] (현재) kwon37xi [Test Slf4j - Reflection 사용] |
||
|---|---|---|---|
| 줄 8: | 줄 8: | ||
| * '' | * '' | ||
| * '' | * '' | ||
| + | |||
| + | ===== 로거 객체 생성 ===== | ||
| + | <code java> | ||
| + | // 자동으로 현재 클래스를 찾아낸다. | ||
| + | import java.lang.invoke.MethodHandles; | ||
| + | |||
| + | private static final Logger logger = | ||
| + | LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); | ||
| + | </ | ||
| ===== Eclipse Slf4j Template ===== | ===== Eclipse Slf4j Template ===== | ||
| 줄 57: | 줄 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: ' | ||
| 줄 114: | 줄 124: | ||
| handlers = org.slf4j.bridge.SLF4JBridgeHandler | handlers = org.slf4j.bridge.SLF4JBridgeHandler | ||
| </ | </ | ||
| - | === Logback 설청 === | + | === Logback 설정 === |
| * '' | * '' | ||
| * groovy 설정< | * groovy 설정< | ||
| 줄 163: | 줄 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:// | ||