사용자 도구

사이트 도구


java:slf4j

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
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 =  
 +  LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
 </code> </code>
  
줄 63: 줄 66:
         configuration.exclude group: 'commons-logging', module: 'commons-logging'         configuration.exclude group: 'commons-logging', module: 'commons-logging'
         configuration.exclude group: 'log4j', module: 'log4j'         configuration.exclude group: 'log4j', module: 'log4j'
 +        configuration.exclude group: 'org.apache.logging.log4j'
         configuration.exclude group: 'org.slf4j', module: 'slf4j-log4j12'         configuration.exclude group: 'org.slf4j', module: 'slf4j-log4j12'
         configuration.exclude group: 'org.slf4j', module: 'slf4j-jcl'         configuration.exclude group: 'org.slf4j', module: 'slf4j-jcl'
줄 120: 줄 124:
 handlers = org.slf4j.bridge.SLF4JBridgeHandler handlers = org.slf4j.bridge.SLF4JBridgeHandler
 </code> </code>
-=== Logback 설청 ===+=== Logback 설정 ===
   * ''LevelChangePropagator'' 필요(http://logback.qos.ch/manual/configuration.html)   * ''LevelChangePropagator'' 필요(http://logback.qos.ch/manual/configuration.html)
     * groovy 설정<code groovy>     * groovy 설정<code groovy>
줄 169: 줄 173:
 System.setProperty("org.slf4j.simpleLogger.log.com.microsoft.sqlserver", "trace"); // 특정 logger의 레벨 지정 System.setProperty("org.slf4j.simpleLogger.log.com.microsoft.sqlserver", "trace"); // 특정 logger의 레벨 지정
 </code> </code>
 +
 +===== Test Slf4j - Reflection 사용 =====
 +  * [[java:reflection|Java Reflection]] 에서 ''private static final'' 필드에 관한 사항 참조
 +
 +<code java>
 +TestTargetClass testTarget = new TestTarget();
 +
 +private Field logField;
 +private Logger originalLogger;
 +private Logger mockedLogger;
 +
 +@BeforeEach
 +void setUp() throws NoSuchFieldException, IllegalAccessException {
 +    mockedLogger = mock(Logger.class);
 +
 +    Field logField = TestTargetClass.class.getDeclaredField("log");
 +    logField.setAccessible(true);
 +
 +    Field modifiersField = Field.class.getDeclaredField("modifiers");
 +    modifiersField.setAccessible(true);
 +    modifiersField.setInt(logField, logField.getModifiers() & ~Modifier.FINAL);
 +
 +    originalLogger = (Logger) logField.get(null);
 +    logField.set(null, mockedLogger);
 +}
 +
 +@AfterEach
 +void tearDown() throws IllegalAccessException {
 +    logField.set(null, originalLogger);
 +}
 +
 +
 +@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("로그 메시지. 로그 인자 {}", "로그 인자");
 +    }
 +}
 +</code>
 +
  
 ===== Test Slf4j ===== ===== Test Slf4j =====
 +  * [[https://www.simplify4u.org/slf4j-mock/|SLF4J mock]] : slf4j 2.x 까지 반영된 최신 라이브러리
   * [[http://projects.lidalia.org.uk/slf4j-test/|SLF4J Test]]   * [[http://projects.lidalia.org.uk/slf4j-test/|SLF4J Test]]
 +  * [[https://github.com/portingle/slf4jtesting|portingle/slf4jtesting: SLF4J Testing library optimised for test concurrency and dependency injection]]
 +  * http://www.spf4j.org/spf4j-slf4j-test/index.html
  
 ===== 참조 ===== ===== 참조 =====
   * [[https://examples.javacodegeeks.com/enterprise-java/slf4j/slf4j-tutorial-beginners/|SLF4J Tutorial for Beginners | Examples Java Code Geeks - 2018]]   * [[https://examples.javacodegeeks.com/enterprise-java/slf4j/slf4j-tutorial-beginners/|SLF4J Tutorial for Beginners | Examples Java Code Geeks - 2018]]
  
java/slf4j.1540255044.txt.gz · 마지막으로 수정됨: 2018/10/23 09:37 저자 kwon37xi