문서의 선택한 두 판 사이의 차이를 보여줍니다.
양쪽 이전 판 이전 판 다음 판 | 이전 판 | ||
java:slf4j [2017/08/11 22:59] kwon37xi |
java:slf4j [2023/12/07 10:32] (현재) kwon37xi [Test Slf4j - Reflection 사용] |
||
---|---|---|---|
줄 4: | 줄 4: | ||
* Jakarta Commons Logging 과 비슷한 기능을 하지만 Slf4j가 더 좋다. | * Jakarta Commons Logging 과 비슷한 기능을 하지만 Slf4j가 더 좋다. | ||
* [[java: | * [[java: | ||
- | * [[http:// | ||
===== 의존성 ===== | ===== 의존성 ===== | ||
* '' | * '' | ||
* '' | * '' | ||
+ | |||
+ | ===== 로거 객체 생성 ===== | ||
+ | <code java> | ||
+ | // 자동으로 현재 클래스를 찾아낸다. | ||
+ | import java.lang.invoke.MethodHandles; | ||
+ | |||
+ | private static final Logger logger = | ||
+ | LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); | ||
+ | </ | ||
===== Eclipse Slf4j Template ===== | ===== Eclipse Slf4j Template ===== | ||
줄 58: | 줄 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: ' | ||
줄 100: | 줄 109: | ||
* http:// | * http:// | ||
* [[http:// | * [[http:// | ||
+ | * [[springframework: | ||
+ | |||
+ | === SLF4JBridgeHandler 설정 === | ||
* [[http:// | * [[http:// | ||
* 애플리케이션 시작 지점에 코드로 설정< | * 애플리케이션 시작 지점에 코드로 설정< | ||
줄 109: | 줄 121: | ||
| | ||
</ | </ | ||
- | * '' | + | * '' |
handlers = org.slf4j.bridge.SLF4JBridgeHandler | handlers = org.slf4j.bridge.SLF4JBridgeHandler | ||
</ | </ | ||
- | * logback | + | === Logback |
+ | * '' | ||
* groovy 설정< | * groovy 설정< | ||
import ch.qos.logback.classic.jul.LevelChangePropagator | import ch.qos.logback.classic.jul.LevelChangePropagator | ||
줄 149: | 줄 162: | ||
Marker marker = MarkerFactory.getMarker(" | Marker marker = MarkerFactory.getMarker(" | ||
</ | </ | ||
+ | |||
+ | * [[https:// | ||
+ | |||
+ | ===== SimpleLogger ===== | ||
+ | * [[http:// | ||
+ | <code java> | ||
+ | // 버전에 따라 프라퍼티 명이 simpleLogger가 아닌 simplelogger일 수도 있음. | ||
+ | // 버전에 따라 defaultLogLevel -> defaultlog 일 수도 있음. | ||
+ | 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:// | ||