Developer(s) | Ceki Gülcü |
---|---|
Stable release | 1.6.4 / October 31, 2011 |
Written in | Java |
Operating system | Cross-platform |
Type | Logging Tool |
License | MIT License |
Website | http://www.slf4j.org |
Simple Logging Facade for Java (SLF4J) provides a Java logging API by means of a simple facade pattern. The underlying logging backend is determined at deployment time[clarification needed] and may be java.util.logging
,log4j or logback.
The separation of the client API from the logging backend reduces the coupling between an application and any particular logging framework. This can make it easier to integrate with existing or third-party code or to deliver code into other projects that have already made a choice of logging backend.
SLF4J was created by Ceki Gülcü as a more reliable alternative to Jakarta Commons Logging framework.[citation needed]
Contents[hide] |
[edit]Similarities and Differences with Log4j
- Five of Log4j's six logging levels are used. FATAL has been dropped on the basis that inside the logging framework is not the place to decide when an application should terminate and therefore there is no difference between ERROR and FATAL from the logger's point of view.
- Logger instances are created via the LoggerFactory, which is very similar in Log4j. For example,
private static final Logger LOG = LoggerFactory.getLogger(Wombat.class);
- In Logger, the logging methods are overloaded with forms that accept one, two or more values.[1] Occurrences of the simple pattern {} in the log message is replaced in turn with the values. This is simple to use yet provides a performance benefit when the values have expensive toString() methods. When logging is disabled at the DEBUG level, the logging framework does not need to evaluate the string representation of the values. In the following example, the valuescount or userAccountList only need to be evaluated when DEBUG is enabled; otherwise the overhead of the debug call is trivial.
LOG.debug("There are now " + count + " user accounts: " + userAccountList); // slow LOG.debug("There are now {} user accounts: {}", count, userAccountList); // faster
- Similar methods exist in Logger for isDebugEnabled() etc. to allow more complex logging calls to be wrapped so that they are disabled when the corresponding level is disabled, avoiding unnecessary processing.
- Unlike Log4j, SLF4J offers logging methods that accept markers. These are special objects that enrich the log messages and are an idea that SLF4J has borrowed from logback.
[edit]See also
[edit]External links
[edit]References
================================================================================================
Log4J는 다음과 같은 String Concatenation 문제가 있다.
이를 해결하기 위해 다음과 같이 할 수 있다.
LOGGER.debug("Hello " + name);
}
하지만 코드가 지저분해진다.
SLF4J는 다음과 같은 방법으로 코드를 깔끔하게 유지하면서,
String Concatenation 문제도 해결하였다.
SLF4J는 API와 구현 (implementation)을 decouple한다.
SLF4J API를 사용하고, 구현은 Log4J를 비롯한 다른 Logging Framework를 활용할 수 있다.
SLF4J는 Bridging을 지원한다.
SLF4J가 Log4J, commons-logging 등의 API를 mimic해서 API를 제공함으로써
호출이 SLF4J로 reroute되도록 한다.
따라서, Log4J, commons-logging 등의 depedencies를 제거하고,
SLF4J만을 사용할 수 있다.
다음과 같은 조합이 추천된다.
이를 위해 추가할 라이브러리는 다음과 같다.
slf4j-log4j.jar // SLF4J Log4J implementation
예제 코드는 다음과 같다.
package learningtest.slf4j;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Slf4jTest {
@Test
public void test() {
Logger log = LoggerFactory.getLogger(Slf4jTest.class);
log.debug("Hello, world!");
log.debug("Hello, {}!", "world");
}
}
Reference:
http://blog.frankel.ch/thoughts-on-java-logging-and-slf4j
출처 - http://devday.tistory.com/2017
===================================================================================
출처 - http://avatar72.tistory.com/17
'Development > Java' 카테고리의 다른 글
SHA1 해쉬 구하는 클래스 (0) | 2012.04.12 |
---|---|
SpringMVC에서 log4j 사용 (0) | 2012.04.01 |
서블릿 load-on-startup (0) | 2012.03.21 |
java 바이트코드 컨트롤 및 분석 - ASM (0) | 2012.03.19 |
패키지 이름 정의 (0) | 2012.03.18 |