문서의 선택한 두 판 사이의 차이를 보여줍니다.
양쪽 이전 판 이전 판 다음 판 | 이전 판 다음 판 양쪽 다음 판 | ||
java:slf4j [2015/11/27 17:30] kwon37xi [Eclipse Slf4j Template] |
java:slf4j [2023/11/30 14:13] kwon37xi [Test Slf4j] |
||
---|---|---|---|
줄 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 ===== | ||
줄 29: | 줄 37: | ||
{{: | {{: | ||
+ | |||
===== Legacy Logging Framework 제거하고 LogBack 사용 ===== | ===== Legacy Logging Framework 제거하고 LogBack 사용 ===== | ||
* http:// | * http:// | ||
줄 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: ' | ||
줄 95: | 줄 105: | ||
</ | </ | ||
</ | </ | ||
- | |||
==== java.util.logging ==== | ==== java.util.logging ==== | ||
* 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 | ||
줄 126: | 줄 139: | ||
</ | </ | ||
</ | </ | ||
+ | |||
+ | ===== Logger Wrapping ===== | ||
+ | * slf4j Logger를 사용할 때 특정 포맷으로 호출할 수 있게 Logger 호출을 감싸는 경우가 있는데, 이 때 log에 항상 로깅 위치가 Wrapper 코드로 남아서 실조 로그를 남기고자하는 코드 위치를 보기 불편해진다. | ||
+ | * '' | ||
+ | |||
+ | <code java> | ||
+ | private static final String FQCN = Logger.class.getName(); | ||
+ | |||
+ | |||
+ | public static void debug(String clazz, String message) { | ||
+ | org.slf4j.Logger logger = LoggerFactory.getLogger(clazz); | ||
+ | if (logger instanceof LocationAwareLogger) { | ||
+ | ((LocationAwareLogger) logger).log(null, | ||
+ | } else { | ||
+ | logger.debug(message); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Marker ===== | ||
+ | <code java> | ||
+ | Marker marker = MarkerFactory.getMarker(" | ||
+ | </ | ||
+ | |||
+ | * [[https:// | ||
+ | |||
+ | ===== SimpleLogger ===== | ||
+ | * [[http:// | ||
+ | <code java> | ||
+ | // 버전에 따라 프라퍼티 명이 simpleLogger가 아닌 simplelogger일 수도 있음. | ||
+ | // 버전에 따라 defaultLogLevel -> defaultlog 일 수도 있음. | ||
+ | System.setProperty(" | ||
+ | System.setProperty(" | ||
+ | </ | ||
+ | |||
+ | ===== Test Slf4j ===== | ||
+ | * [[https:// | ||
+ | * [[http:// | ||
+ | * [[https:// | ||
+ | * http:// | ||
+ | |||
+ | ===== 참조 ===== | ||
+ | * [[https:// | ||
+ |