准确的说,slf4j并不是一种具体的日志系统,而是一个用户日志系统的facade,允许用户在部署最终应用时方便的变更其日志系统。
使用方式:
使用场景:
应用举例
- package chb.test.slf4j;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- /**
- * @author chb
- *
- */
- public class TestSlf4j {
- Logger log = LoggerFactory.getLogger(TestSlf4j.class);
- public void testLog(){
- log.info("this is a test log");
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- TestSlf4j slf = new TestSlf4j();
- slf.testLog();
- }
- }
- 2010-1-5 21:44:47 chb.test.slf4j.TestSlf4j testLog
- 信息: this is a test log
- 0 [main] INFO chb.test.slf4j.TestSlf4j - this is a test log
- log4j.debug=true
- log4j.rootLogger=DEBUG,stdout
- log4j.appender.stdout=org.apache.log4j.ConsoleAppender
- log4j.appender.stdout.Target=System.out
- log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
- log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p - %m%n
- public static void main(String[] args) {
- System.setProperty("log4j.configuration", "log4j.properties");
- TestSlf4j slf = new TestSlf4j();
- slf.testLog();
- }
- log4j: Parsing for [root] with value=[DEBUG,stdout].
- log4j: Level token is [DEBUG].
- log4j: Category root set to DEBUG
- log4j: Parsing appender named "stdout".
- log4j: Parsing layout options for "stdout".
- log4j: Setting property [conversionPattern] to [%d{ABSOLUTE} %5p - %m%n ].
- log4j: End of parsing for "stdout".
- log4j: Setting property [target] to [System.out].
- log4j: Parsed "stdout" options.
- log4j: Finished configuring.
- 22:01:40,831 INFO - this is a test log
- LoggerFactory通过StaticLoggerBinder.getSingleton().getLoggerFactory()获得LogFactory,然后再通过该LogFactory来获取logger的
- 但是StaticLoggerBinder类并不在slf4j-api-1.5.10.jar中,分析与具体日志系统相关的jar包,会发现每个 jar包都有一个StaticLoggerBinder类的实现(如slf4j-log4j12-1.5.10.jar、slf4j-simple- 1.5.10.jar、slf4j-jdk14-1.5.10.jar均有StaticLoggerBinder类实现),这就很明白了,slf4j在启 动时会动态到classpath中查找StaticLoggerBinder类,找到之后就可以生成对应日志系统的日志文件了。
- /**
- * Return a logger named corresponding to the class passed as parameter, using
- * the statically bound {@link ILoggerFactory} instance.
- *
- * @param clazz
- * the returned logger will be named after clazz
- * @return logger
- */
- public static Logger getLogger(Class clazz) {
- return getLogger(clazz.getName());
- }
- public static Logger getLogger(String name) {
- ILoggerFactory iLoggerFactory = getILoggerFactory();
- return iLoggerFactory.getLogger(name);
- }
- /**
- * Return the {@link ILoggerFactory} instance in use.
- *
- * <p>
- * ILoggerFactory instance is bound with this class at compile time.
- *
- * @return the ILoggerFactory instance in use
- */
- public static ILoggerFactory getILoggerFactory() {
- if (INITIALIZATION_STATE == UNINITIALIZED) {
- INITIALIZATION_STATE = ONGOING_INITILIZATION;
- performInitialization();
- }
- switch (INITIALIZATION_STATE) {
- case SUCCESSFUL_INITILIZATION:
- return getSingleton().getLoggerFactory();
- case FAILED_INITILIZATION:
- throw new IllegalStateException(UNSUCCESSFUL_INIT_MSG);
- case ONGOING_INITILIZATION:
- // support re-entrant behavior.
- // See also http://bugzilla.slf4j.org/show_bug.cgi?id=106
- return TEMP_FACTORY;
- }
- throw new IllegalStateException("Unreachable code");
- }
- private final static void performInitialization() {
- bind();
- versionSanityCheck();
- singleImplementationSanityCheck();
- }
- private final static void bind() {
- try {
- // the next line does the binding
- getSingleton();
- INITIALIZATION_STATE = SUCCESSFUL_INITILIZATION;
- emitSubstituteLoggerWarning();
- } catch (NoClassDefFoundError ncde) {
- INITIALIZATION_STATE = FAILED_INITILIZATION;
- String msg = ncde.getMessage();
- if (msg != null && msg.indexOf("org/slf4j/impl/StaticLoggerBinder") != -1) {
- Util
- .reportFailure("Failed to load class \"org.slf4j.impl.StaticLoggerBinder\".");
- Util.reportFailure("See " + NO_STATICLOGGERBINDER_URL
- + " for further details.");
- }
- throw ncde;
- } catch (Exception e) {
- INITIALIZATION_STATE = FAILED_INITILIZATION;
- // we should never get here
- Util.reportFailure("Failed to instantiate logger ["
- + getSingleton().getLoggerFactoryClassStr() + "]", e);
- }
- }
- private final static StaticLoggerBinder getSingleton() {
- if (GET_SINGLETON_METHOD == GET_SINGLETON_INEXISTENT) {
- return StaticLoggerBinder.getSingleton();
- }
- if (GET_SINGLETON_METHOD == GET_SINGLETON_EXISTS) {
- return StaticLoggerBinder.getSingleton();
- }
- try {
- StaticLoggerBinder singleton = StaticLoggerBinder.getSingleton();
- GET_SINGLETON_METHOD = GET_SINGLETON_EXISTS;
- return singleton;
- } catch (NoSuchMethodError nsme) {
- GET_SINGLETON_METHOD = GET_SINGLETON_INEXISTENT;
- return StaticLoggerBinder.getSingleton();
- }
- }