com.xiaoleilu.hutool.log.dialect.slf4j.Slf4jLogFactory Maven / Gradle / Ivy
package com.xiaoleilu.hutool.log.dialect.slf4j;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import org.slf4j.LoggerFactory;
import org.slf4j.helpers.NOPLoggerFactory;
import com.xiaoleilu.hutool.log.Log;
import com.xiaoleilu.hutool.log.LogFactory;
/**
* SLF4J log.
* 同样无缝支持 LogBack
*
* @author Looly
*
*/
public class Slf4jLogFactory extends LogFactory {
public Slf4jLogFactory() {
this(false);
}
/**
* 构造
*
* @param failIfNOP 如果未找到桥接包是否报错
*/
public Slf4jLogFactory(boolean failIfNOP) {
super("Slf4j");
checkLogExist(org.slf4j.LoggerFactory.class);
if(false == failIfNOP){
return;
}
// SFL4J writes it error messages to System.err. Capture them so that the user does not see such a message on
// the console during automatic detection.
final StringBuilder buf = new StringBuilder();
final PrintStream err = System.err;
try {
System.setErr(new PrintStream(new OutputStream(){
@Override
public void write(int b) {
buf.append((char) b);
}
}, true, "US-ASCII"));
} catch (UnsupportedEncodingException e) {
throw new Error(e);
}
try {
if (LoggerFactory.getILoggerFactory() instanceof NOPLoggerFactory) {
throw new NoClassDefFoundError(buf.toString());
} else {
err.print(buf);
err.flush();
}
} finally {
System.setErr(err);
}
}
@Override
public Log createLog(String name) {
return new Slf4jLog(name);
}
@Override
public Log createLog(Class> clazz) {
return new Slf4jLog(clazz);
}
}